SimpleCronWorkerService 0.0.3
See the version list below for details.
dotnet add package SimpleCronWorkerService --version 0.0.3
NuGet\Install-Package SimpleCronWorkerService -Version 0.0.3
<PackageReference Include="SimpleCronWorkerService" Version="0.0.3" />
paket add SimpleCronWorkerService --version 0.0.3
#r "nuget: SimpleCronWorkerService, 0.0.3"
// Install SimpleCronWorkerService as a Cake Addin #addin nuget:?package=SimpleCronWorkerService&version=0.0.3 // Install SimpleCronWorkerService as a Cake Tool #tool nuget:?package=SimpleCronWorkerService&version=0.0.3
SimpleCronWorkerService
A simple and easy way to implement worker services scheduled by a CRON expression
Getting started
Package Install
Installing is as easy as: dotnet add package SimpleCronWorkerService
or Install-Package SimpleCronWorkerService
depending on your setup.
Create your CronWorkerService
Create your Worker class which must inherit from the abstract class
CronWorkerService
imported fromSimpleCronWorkerService
using SimpleCronWorkerService; namespace WorkerServiceSample { public class Worker : CronWorkerService { } }
In the constructor, as the first parameter, you will receive a
CronWorkerServiceSettings<>
object as the type of your worker class. These settings must be passed to the base constructor: base(cronWorkerServiceSettings)
.using SimpleCronWorkerService; namespace WorkerServiceSample { public class Worker : CronWorkerService { private readonly ILogger<Worker> _logger; public Worker(CronWorkerServiceSettings<Worker> cronWorkerServiceSettings,ILogger<Worker> logger) :base(cronWorkerServiceSettings) { _logger = logger; } } }
You have to override the method
protected override Task DoWork(CancellationToken stoppingToken)
with the logic that you want your Worker to execute.using SimpleCronWorkerService; namespace WorkerServiceSample { public class Worker : CronWorkerService { private readonly ILogger<Worker> _logger; public Worker(CronWorkerServiceSettings<Worker> cronWorkerServiceSettings,ILogger<Worker> logger) :base(cronWorkerServiceSettings) { _logger = logger; } protected override Task DoWork(CancellationToken cancellationToken) { // ... Your logic } } }
Example
using SimpleCronWorkerService;
namespace WorkerServiceSample
{
public class Worker : CronWorkerService
{
private readonly ILogger<Worker> _logger;
public Worker(CronWorkerServiceSettings<Worker> cronWorkerServiceSettings,ILogger<Worker> logger)
:base(cronWorkerServiceSettings)
{
_logger = logger;
}
protected override Task DoWork(CancellationToken cancellationToken)
{
_logger.LogInformation("Running... at {0}", DateTime.UtcNow);
return Task.CompletedTask;
}
}
}
Register your CronWorkerService
In your service container, you can add your worker using SimpleCronWorkerService
with the method Services.AddCronWorkerService<>
The type <>
should be your Worker
class.
using SimpleCronWorkerService;
...
// Add services to the container.
builder.Services.AddCronWorkerService<Worker>(options =>
{
// Run every minute
options.CronExpression = @"* * * * *";
options.TimeZone = TimeZoneInfo.Local;
});
Inside this method, the options are passed, these options are of type CronWorkerServiceSettings<T>
public class CronWorkerServiceSettings<T> : ICronWorkerServiceSettings where T : CronWorkerService
{
public string CronExpression { get; set; } = @"* * * * *";
public TimeZoneInfo TimeZone { get; set; } = TimeZoneInfo.Utc;
public bool CronExpressionIncludeSeconds { get; set; } = false;
}
The CronExpression
is a string and we are using the Cronos library. For more information about this syntax, please visit the Cronos documentation. By default, it is the expression for every minute ("* * * * *"
).
The TimeZone
sets the time zone in which you want your worker to run. By default, it is UTC
.
The CronExpressionIncludeSeconds
is a boolean used if you are going to use the seconds part of the expression (Cronos documentation). By default, it is false
.
Samples
Contributing
Please fork this repo then create a PR from the fork into the original one.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Cronos (>= 0.7.1)
- Microsoft.Extensions.Hosting (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.