-1

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;
    }
Mihir
  • 45
  • 8
  • There is no error in the log file. Can you step through code to see where the failure is occurring? You may want to check the Event Viewer to see if there are errors. – jdweng Aug 11 '23 at 19:28
  • I checked event viewer, and found nothing – Mihir Aug 11 '23 at 19:41
  • 1
    Everything is working. You are not getting any errors. The code is terminating after one task. Is process still running in Task Manager? Did you comment out any code? the code needs to block to continue running after one task. You could be getting an exception from place in code where there is no exception handler. – jdweng Aug 11 '23 at 20:01
  • FYI, I will modify the query with actual code and remove screenshots...give me some more time...I think the problem is with setting up Cron schedule(WithCronSchedule), when I removed CronJobSchedule, it started working, of course, i need to figure out a correct way to set the job schedule – Mihir Aug 11 '23 at 20:20
  • Yes, I have that registered, the problem is something mentioned in the above comment, once I find the complete solution I will post it – Mihir Aug 11 '23 at 20:39
  • How come few Cron expressions are working and few are not working? very weird – Mihir Aug 11 '23 at 21:45
  • Thank you all for your support, but the above-mentioned problem was not a problem after all. I have posted the clarification in the "Post the answer" section. – Mihir Aug 14 '23 at 17:27

1 Answers1

0

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

Mihir
  • 45
  • 8