.Net Core uses Coravel to implement task scheduling

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

dotnet add package coravel

 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  IInvocablethe interface and  Invokeencode 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());
        }
    }

 

 

Guess you like

Origin blog.csdn.net/beautifull001/article/details/127394514