RandomSkunk.Hosting.Cron 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RandomSkunk.Hosting.Cron --version 1.1.0                
NuGet\Install-Package RandomSkunk.Hosting.Cron -Version 1.1.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="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RandomSkunk.Hosting.Cron --version 1.1.0                
#r "nuget: RandomSkunk.Hosting.Cron, 1.1.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=1.1.0

// Install RandomSkunk.Hosting.Cron as a Cake Tool
#tool nuget:?package=RandomSkunk.Hosting.Cron&version=1.1.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, inherit from the CronJob abstract class:

using RandomSkunk.Hosting.Cron;

public class MyCronJob : CronJob
{
    public MyCronJob()
        : base("*/30 * * * * *") // Run every 30 seconds.
    {
    }

    protected override async Task DoWork(CancellationToken stoppingToken)
    {
        Console.WriteLine("Starting cron job at: {DateTimeOffset.Now:G}");

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

        Console.WriteLine("Cron job finished at: {DateTimeOffset.Now:G}");
    }
}

Add the cron job to an application like any other hosted service:

services.AddHostedService<MyCronJob>();

The next example shows a more involved cron job, which is initialized with configuration and has logging.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using RandomSkunk.Hosting.Cron;

public class AnotherCronJob : CronJob
{
    private const string _cronExpressionSettingName = "AnotherCronJob.CronExpression";

    private readonly ILogger<AnotherCronJob> _logger;

    public AnotherCronJob(IConfiguration configuration, ILogger<AnotherCronJob> logger)
        : base(GetCronExpression(configuration), logger)
    {
        _logger = logger;
    }

    public override Task StartAsync(CancellationToken cancellationToken)
    {
        _logger.LogInformation("Cron job starting at: {time:G}", DateTimeOffset.Now);
        return base.StartAsync(cancellationToken);
    }

    public override Task StopAsync(CancellationToken cancellationToken)
    {
        _logger.LogInformation("Cron job stopping at: {time:G}", DateTimeOffset.Now);
        return base.StopAsync(cancellationToken);
    }

    public override void Dispose()
    {
        _logger.LogInformation("Disposing cron job at: {time:G}", DateTimeOffset.Now);
        base.Dispose();
    }

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

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

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

    private static string GetCronExpression(IConfiguration configuration)
    {
        ArgumentNullException.ThrowIfNull(configuration);

        return configuration[_cronExpressionSettingName]
            ?? throw new ArgumentException($"Must contain a value for '{_cronExpressionSettingName}' setting.", nameof(configuration));
    }
}

An application running this cron job would need to have a configuration that looked something like this (this example is configured to run nightly at 2 am):

{
  "AnotherCronJob": {
    "CronExpression": "0 2 * * *"
  }
}
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.1.0 98 7/12/2024
2.0.0 98 6/21/2024
1.1.0 110 6/14/2024
1.0.0 199 12/11/2023