RandomSkunk.Hosting.Cron 2.0.0

dotnet add package RandomSkunk.Hosting.Cron --version 2.0.0
NuGet\Install-Package RandomSkunk.Hosting.Cron -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RandomSkunk.Hosting.Cron" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RandomSkunk.Hosting.Cron --version 2.0.0
#r "nuget: RandomSkunk.Hosting.Cron, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RandomSkunk.Hosting.Cron as a Cake Addin
#addin nuget:?package=RandomSkunk.Hosting.Cron&version=2.0.0

// Install RandomSkunk.Hosting.Cron as a Cake Tool
#tool nuget:?package=RandomSkunk.Hosting.Cron&version=2.0.0

RandomSkunk.Hosting.Cron NuGet

An IHostedService base class that triggers on a cron schedule.


RandomSkunk.Hosting.Cron provides a IHostedService base class that schedules work according to a cron schedule. Cron support is provided by the Cronos nuget package.

Usage

To implement a cron job that loads its settings from configuration, inherit from the CronJob abstract class and create a constructor with an IOptionsMonitor<CronJobOptions> parameter. Pass this through to the base constructor.

Note that a cron job created with options will automatically reload itself when the option's configuration reloads.

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using RandomSkunk.Hosting.Cron;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace MyNamespace;

public class MyCronJob : CronJob
{
    private readonly ILogger<MyCronJob> _logger;

    public MyCronJob(
        IOptionsMonitor<CronJobOptions> optionsMonitor,
        ILogger<MyCronJob> logger)
        : base(optionsMonitor, logger)
    {
        _logger = logger;
    }

    protected override async Task DoWork(CancellationToken cancellationToken)
    {
        _logger.LogDebug("Cron job work starting at: {time:G}", DateTimeOffset.Now);

        // Simulate work.
        await Task.Delay(Random.Shared.Next(250, 1000), cancellationToken);

        _logger.LogInformation("Cron job work finished at: {time:G}", DateTimeOffset.Now);
    }
}

Add a configuration section for the cron job. The section must have a 'CronExpression' setting.

{
  "MyCronJob": {
    "CronExpression": "0 1 * * MON", // Scheduled for 1 AM EST every Monday.
    "TimeZone": "Eastern Standard Time" // Other values are "Local" and "UTC".
  }
}

Add the cron job the an application with the AddCronJob extension method. Pass it the configuration section that defines the cron job.

services.AddCronJob<MyCronJob>(services.GetSection("MyCronJob"));

That's it. When you run your application, the cron job will fire every Monday at 1 AM.


The next example show a cron job that loads its settings directly from constructor parameters.

using Microsoft.Extensions.Logging;
using RandomSkunk.Hosting.Cron;
using System.Threading.Tasks;
using System.Threading;
using System;

namespace MyNamespace;

public class AnotherCronJob : CronJob
{
    private readonly ILogger<AnotherCronJob> _logger;

    public AnotherCronJob(ILogger<AnotherCronJob> logger) // Scheduled for 8 AM PST Monday through Friday.
        : base("0 8 * * MON-FRI", logger, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"))
    {
        _logger = logger;
    }

    protected override async Task DoWork(CancellationToken stoppingToken)
    {
        _logger.LogDebug("Starting cron job at: {StartTime:G}", DateTime.Now);

        // Simulate work.
        await Task.Delay(Random.Shared.Next(250, 1000), stoppingToken);

        _logger.LogInformation("Cron job finished at: {EndTime:G}", DateTime.Now);
    }
}

To add this cron job to an application, use the regular AddHostedService extension method.

builder.Services.AddHostedService<AnotherCronJob>();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 41 6/21/2024
1.1.0 80 6/14/2024
1.0.0 175 12/11/2023