Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

dotnetbots/CronBackgroundServices

Open more actions menu

Repository files navigation

main NuGet

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.

public interface IRecurringAction
{

     /// <summary>
    /// The job to be executed at intervals defined by the Cron expression
    /// </summary>
    /// <returns></returns>
    Task Process(CancellationToken stoppingToken);

    /// <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>
    string Cron { 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>
    string GetTimeZoneId()
    {
        return !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Europe/London" : "GMT Standard Time";
    }
}

About

`IHostedService` implementation triggered by CRON expressions

Topics

Resources

Stars

Watchers

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.