RandomSkunk.Hosting.Cron
1.1.0
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
<PackageReference Include="RandomSkunk.Hosting.Cron" Version="1.1.0" />
paket add RandomSkunk.Hosting.Cron --version 1.1.0
#r "nuget: RandomSkunk.Hosting.Cron, 1.1.0"
// 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
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 | Versions 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. |
-
.NETFramework 4.6.2
- Cronos (>= 0.8.4)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- System.ValueTuple (>= 4.5.0)
-
.NETStandard 2.0
- Cronos (>= 0.8.4)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
-
.NETStandard 2.1
- Cronos (>= 0.8.4)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
-
net6.0
- Cronos (>= 0.8.4)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
-
net7.0
- Cronos (>= 0.8.4)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
-
net8.0
- Cronos (>= 0.8.4)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.