Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sgbj/crontimer
Like PeriodicTimer but for cron jobs.
https://github.com/sgbj/crontimer
Last synced: 8 days ago
JSON representation
Like PeriodicTimer but for cron jobs.
- Host: GitHub
- URL: https://github.com/sgbj/crontimer
- Owner: sgbj
- License: mit
- Created: 2022-01-15T22:10:27.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-25T19:34:27.000Z (over 1 year ago)
- Last Synced: 2024-10-02T11:10:20.004Z (about 2 months ago)
- Language: C#
- Homepage:
- Size: 17.6 KB
- Stars: 39
- Watchers: 3
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sgbj.Cron.CronTimer
Provides a cron timer similar to [`System.Threading.PeriodicTimer`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.periodictimer?view=net-6.0) that enables waiting asynchronously for timer ticks.Available on [NuGet](https://www.nuget.org/packages/Sgbj.Cron.CronTimer).
## Usage
Normal usage:
```c#
// Every minute
using var timer = new CronTimer("* * * * *");while (await timer.WaitForNextTickAsync())
{
// Do work
}
```Example [hosted service](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio):
```c#
public class CronJob : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Every day at 8am local time
using var timer = new CronTimer("0 8 * * *", TimeZoneInfo.Local);while (await timer.WaitForNextTickAsync(stoppingToken))
{
// Do work
}
}
}
```Non-standard cron expression:
```c#
// Every 30 seconds
using var timer = new CronTimer(CronExpression.Parse("*/30 * * * * *", CronFormat.IncludeSeconds));
```## Resources
* [Hangfire/Cronos](https://github.com/HangfireIO/Cronos) - Library for working with cron expressions.
* [Crontab.guru](https://crontab.guru/) - The cron schedule expression editor.