Skip to content

Repository files navigation

mainNuGet

CronBackgroundServices

.NET BackgroundService jobs triggered by configured Cron Expressions

Installation

$ dotnet add package CronBackgroundServices

Usage

Jobs are configured during DI registration:

services.AddRecurrer<MyCustomRecurringJob>().AddRecurrer<MySecondJob>()

Each job has to implement IRecurringAction. If you want a different TimeZone than UTC you have to override the default interface method GetTimeZoneId.

publicinterfaceIRecurringAction{/// <summary>/// The job to be executed at intervals defined by the Cron expression/// </summary>/// <returns></returns>TaskProcess(CancellationTokenstoppingToken);/// <summary>/// The cron expression (including seconds) as defined by the Cronos library:/// See https://github.com/HangfireIO/Cronos#cron-format/// Ex: Every second: */1 * * * * */// Ex: Every minute: 0 */1 * * * */// Ex: Every midnight: 0 0 */1 * * */// Ex: First of every month 0 0 0 1 * */// </summary>/// <returns>A valid Cron Expression</returns>stringCron{get;}/// <summary>/// Optional: The TimeZone in which the Cron expression should be based on./// Defaults to UTC (Europe/London or GMT Standard Time)////// NB! When overriding this and targeting versions below .NET 6, use platform specific identifiers/// If your runtime is .NET 6 or above, it's not required. It will handles the conversion:/// See https://github.com/dotnet/runtime/pull/49412/// </summary>/// <returns>timezoneId</returns>stringGetTimeZoneId(){return!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)?"Europe/London":"GMT Standard Time";}}

About

`IHostedService` implementation triggered by CRON expressions

Topics

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages