我正在使用 Quartz 为 Windows 服务 .net core 创建作业调度程序。
我的代码完全遵循石英实现,如下面的链接所述。
https://www.quartz-scheduler.net/documentation/quartz-3.x/packages/aspnet-core-integration.html
https://andrewlock.net/using-quartz-net-with-asp-net-core-and-worker-services/
来自 Program.cs 的代码片段
builder.Services.AddQuartz(q => {
JobKey jobkey = new JobKey("QuartzDemoKey");
q.UseMicrosoftDependencyInjectionJobFactory();
q.AddJob < Worker > (config => config.WithIdentity(jobkey));
q.AddTrigger(config =>
config.WithIdentity("QuartzDemoTrigger").WithCronSchedule("0
0/15 * * * ?").ForJob(jobkey));
});
直到昨天,我上面的代码都可以在本地和开发服务器上运行。现在它突然停止工作了。当我检查日志时,它给了我以下消息(不是错误)
我已经尝试了一切,但该作业不再触发,无论是在本地还是开发服务器中
我的工作(Worker.cs)代码片段如下:
public class Worker : IJob
{
private readonly ScannerService _scannerService;
private readonly ILogger<Worker> _logger;
private readonly TimeSpan _interval = TimeSpan.FromDays(7);
private readonly Dictionary<string, SanctionContext> _dbDictionary;
private readonly IConfiguration _configuration;
public Worker(
ScannerService scannerService,
ILogger<Worker> logger,
Dictionary<string, SanctionContext> dbDictionary,
IConfiguration configuration) =>
(_scannerService, _logger, _dbDictionary, _configuration) = (scannerService, logger, dbDictionary, configuration);
public Task Execute(IJobExecutionContext context)
{
Console.WriteLine("Quartz is running now");
//_logger.LogInformation("Quartz running at: {time}", DateTimeOffset.Now);
try
{
var counter = 0;
_logger.LogWarning("Starting service");
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
_scannerService.InitiateScanner(_dbDictionary, _logger, _configuration);
//_scannerService.InitiateScanner(_dbDictionary);
//_logger.LogWarning("{MDP}", "Msg success");
_logger.LogInformation("Service ends at: {time} {runCounter}", DateTimeOffset.Now, counter);
counter++;
//await Task.Delay(_interval, stoppingToken);
}
//catch (TaskCanceledException)
//{
// // When the stopping token is canceled, for example, a call made from services.msc,
// // we shouldn't exit with a non-zero exit code. In other words, this is expected...
//}
catch (Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
// Terminates this process and returns an exit code to the operating system.
// This is required to avoid the 'BackgroundServiceExceptionBehavior', which
// performs one of two scenarios:
// 1. When set to "Ignore": will do nothing at all, errors cause zombie services.
// 2. When set to "StopHost": will cleanly stop the host, and log errors.
//
// In order for the Windows Service Management system to leverage configured
// recovery options, we need to terminate the process with a non-zero exit code.
//Environment.Exit(1);
}
return Task.CompletedTask;
}
谢谢大家的支持,不过上述问题毕竟不是问题。当我们将 Cron 表达式设置为当天晚些时候的某个时间并且当我们进行调试时,它不会调用该函数,直到达到确切的日期和时间,这一事实让我分心。例如;如果当前是中午 12:21,并且我已将 Cron 设置为每天下午 6 点运行,直到下午 6 点,该作业将不会被执行(调用),并且它将始终在日志中给我一条消息像这样 - “批量获取 0 个触发器”,一旦到达下午 6 点,作业将被执行,现在它会显示“批量获取 1 个触发器”。我认为这是一个错误或错误,这是非常愚蠢的。我很抱歉浪费了任何人的时间。这是我第一次使用 Quartz,在这里发布问题之前我会更加小心。如果有人想将它用于 Quartz .Net Core Windows 服务实现,上面的设置对我来说效果很好(到目前为止,仍在测试)