public async Task Execute(BackgroundProcessContext context) { List <double> nextExecuteDelays = new List <double>(); //记录每个任务的下次执行时间,取最小的等待 try { var lockKey = $"{_options.TopicPrefix}crontab:lock"; await _redisStorage.Lock(lockKey, TimeSpan.FromMinutes(1), async() => { var list = await _redisStorage.GetAllCrontabJobId(); foreach (var jobId in list) { if (context.IsShutdownRequested) { return; } var now = DateTime.Now; var jobData = await _redisStorage.GetCrontabJobData(jobId); if (jobData == null) { continue; } if (jobData.Status == (int)CrontabJobStatus.Disabled) { continue; //如果设置启用时,要把LastExecuteTime设置为当前时间 } //暂未实现 } }, () => Task.CompletedTask); } finally { var minValue = nextExecuteDelays.Any() ? nextExecuteDelays.Min() : TimeSpan.FromSeconds(_options.CrontabIntervalSecond).TotalMilliseconds; var delay = minValue;// Math.Max(minValue, 1000); _redisStorage.WaitForCrontabJob(TimeSpan.FromMilliseconds(delay), context.CancellationToken); } }