CronQuery
CronQuery is an open source and lightweight job runner for ASP.NET Core.
# Installation
CronQuery is installed via NuGet package.
dotnet add package CronQuery
Install-Package CronQuery
# Creating jobs
Jobs are created by implementing the interface IJob
.
public class MyJob : IJob
{
public async Task RunAsync()
{
// Do your magic
}
}
# Registering jobs
Jobs are registered in the Program.cs
class.
using CronQuery.Mvc.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCronQuery(builder.Configuration.GetSection("CronQuery"));
builder.Services.AddTransient<MyFirstJob>();
builder.Services.AddTransient<MySecondJob>();
Jobs are registered using the ASP.NET Core dependency injection. This means that is possible to use dependency injection in your jobs.
# Setting up jobs
Schedule your jobs using CRON expressions of six fields to a specific timezone (UTC is default). Save the configuration in your appsettings.json
like the example below:
Job | Configuration |
---|---|
MyFirstJob | Runs every second on every day, except Sunday. |
MySecondJob | Runs every day at 2:00 A.M. |
MyThirdJob | Runs every second between 2:00 P.M. and 6:59 P.M. only on Saturday every 15 days. |
{
"CronQuery": {
"Running": true,
"TimeZone": "E. South America Standard Time",
"Jobs": [
{
"Name": "MyFirstJob",
"Running": true,
"Cron": "* * * * * 1-6"
},
{
"Name": "MySecondJob",
"Running": true,
"Cron": "0 0 2 * * *"
},
{
"Name": "MyThirdJob",
"Running": true,
"Cron": "* * 14-18 * * 6/15"
}
]
}
}
Whenever you save the appsettings.json
CronQuery immediately assumes the new configuration.
Property | Description |
---|---|
CronQuery.Running | Turn on (true ) or turn off (false ) the CronQuery runner. |
CronQuery.TimeZone | System time zone ID or a custom UTC offset, e.g. UTC-03:00 , UTC+01:00 . |
CronQuery.Jobs[].Name | Job class name. |
CronQuery.Jobs[].Running | Turn on (true ) or turn off (false ) the job. |
CronQuery.Jobs[].Cron | CRON expression that triggers the job. |
You can see a full example in the GitHub repo.