A simple and easy way to implement worker services scheduled by a CRON expression
Installing is as easy as: dotnet add package SimpleCronWorkerService
or Install-Package SimpleCronWorkerService
depending on your setup.
-
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;
}
}
}
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
.
Please fork this repo then create a PR from the fork into the original one.