Coravel is a .NET Standard library designed specifically for .NET Core. In addition to task scheduling, it also provides other advanced functions such as queues, caching, emails, etc. The characteristic is that it is very friendly to developers, the access is very simple, elegant and smooth, and it is close to zero configuration.
First install the Coravel package
1 |
|
ConfigureServicesConfiguration
// Coravel定时
services.AddScheduler();
Configure configuration
// 执行定时任务调度
app.ApplicationServices.UseScheduler(scheduler =>
{
scheduler.Schedule<FirstService>().EverySecond(); //每秒执行一次
});
Invocable
It is the core concept in Coravel and represents an independent task. Coravel schedules these directly Invocable
.
To create your own Invocable
, just implement IInvocable
the interface and Invoke
encode your tasks in methods.
public class FirstService : IInvocable
{
private readonly ILogger<FirstService> _logger;
public FirstService(ILogger<FirstService> logger)
{
_logger = logger;
}
public async Task Invoke()
{
_logger.LogInformation("启动Coravel任务调度"+DateTime.Now.ToString());
}
}