Search code examples
c#.netquartz.netquartz

Quartz .Net not working....."Batch acquisition of 0 triggers"


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)

enter image description here

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;
    }

Solution

  • Thank you all for your support, but the above-mentioned problem was not a problem after all. I was distracted by the fact that when we set Cron expression to sometime later in the day and when we debug, it doesn't call the function till the exact date and time is reached. For eg; if it currently, is 12:21 pm, and I have set the Cron to run at 6 pm daily, till it reaches 6 pm, the job won't be executed(called) and it will always be giving me a message in log like this - "Batch acquisition of 0 triggers", once it reaches 6 pm, the job will get executed and it will now show "Batch acquisition of 1 triggers". It was very silly of me to think that it is an error or bug. My apologies for wasting anyone's time. It was my first time working with Quartz and again i will be more careful before posting questions here. The above set up worked fine for me(so far, still testing) if someone wants to use it for their Quartz .Net Core windows service implementation