Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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.