The goal of this library was to utilize IHostedService interface for Asp.Net Core 2.1 for scheduled jobs/tasks. It is lighter than Quartz schedular and operates inside of any GenericHost.
This library does not include implementations for seconds in the Crontab format. You can use https://crontab-generator.org/ to generated needed job/task schedule. The following crontab format is supported:
* * * * *
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───── day of week (0 - 6) (Sunday=0 )
│ │ │ └────────── month (1 - 12)
│ │ └─────────────── day of month (1 - 31)
│ └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)
`* * * * *` Every minute.
`0 * * * *` Top of every hour.
`0,1,2 * * * *` Every hour at minutes 0, 1, and 2.
`*/2 * * * *` Every two minutes.
`1-55 * * * *` Every minute through the 55th minute.
`* 1,10,20 * * *` Every 1st, 10th, and 20th hours.
The sample website provides with use-case scenario for this library.
Includes the following sample service:
public class TorahQuoteJob : IScheduledJob
{
public string CronSchedule { get; }
private readonly TorahService _service;
private readonly TorahSettings _options;
public TorahQuoteJob(IOptions<TorahSettings> options, TorahService service)
{
_options = options.Value;
CronSchedule = _options.CronSchedule; //set to 1 min in appsettings.json "* * * * *"
_service = service;
}
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
var index = new Random().Next(_options.Verses.Length);
var exp = _options.Verses[index];
var result = await _service.GetVerses(exp, cancellationToken);
TorahVerses.Current = result;
}
}
- Sample uses Microsoft.Extensions.Http.Polly extension library to make http calls every minute.
- Maarten Balliauw for the Asp.Net Core idea for the background hosted implementation.
- NCrontab
- 3 ways to use HTTPClientFactory in ASP.NET Core 2.1