using Furion.TaskScheduler;
using YBDevice.WorkerService.Services;

namespace YBDevice.WorkerService
{
    public class Worker : BackgroundService
    {
        private readonly ILogger<Worker> _logger;
        private readonly IServiceScopeFactory _scopeFactory;

        public Worker(ILogger<Worker> logger, IServiceScopeFactory scopeFactory)
        {
            _logger = logger;
            _scopeFactory = scopeFactory;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                //每天10点发送,延迟10秒
                await SpareTime.DoAsync("10 00 10 * * *", async () =>
                {
                    using (var scope = _scopeFactory.CreateScope())
                    {
                        var services = scope.ServiceProvider;
                        var spService = services.GetService<IMessageService>();
                        //订阅消息处理
                        await spService.GetMessageListAsync();
                    }
                }, stoppingToken, CronFormat.IncludeSeconds);
            }
        }
    }
}