I am working on creating Job scheduler for windows service .net core using Quartz.
My code follows quartz implementation exactly as mentioned in below links.
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/
Code snippet from 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));
});
My code above used to work in both local and Dev server till yesterday. Now all of a sudden it stopped working. When i checked the logs, it gives me the following message(not error)
I have tried everything, but the job is not triggering anymore, neither in local or Dev server
My Job(Worker.cs) code snippet is as below:
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;
}