Quartz scheduler not calling the Job class

51 Views Asked by At

I'm pretty new to C# and I was trying to learn about Quartz scheduler. I have been trying to execute the below code but my doubt is why is the scheduler not entering the HelloJob class. Am I doing something wrong?

Apologies if it is a silly mistake but am not able to get any positive result for some time now.

This is my code:

public class Program
{
    public static async Task Main(string[] args)
    {
        StdSchedulerFactory factory = new StdSchedulerFactory();
        IScheduler scheduler = await factory.GetScheduler();
            
        CancellationToken stoppingToken = CancellationToken.None;
            
        await DoSomething(scheduler, stoppingToken);
    }

    public async Task DoSomething(IScheduler scheduler, CancellationToken ct)
    {
        var job = JobBuilder.Create<HelloJob>()
                            .WithIdentity("name", "group")
                            .Build();

        var trigger = TriggerBuilder.Create()
                                    .WithIdentity("name", "group")
                                    .WithSimpleSchedule()
                                    .StartNow()
                                    .Build();

        await Console.WriteLineAsync("Job is still in Main class")
        await scheduler.ScheduleJob(job, trigger, ct);
    }
}

public class HelloJob : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        await Console.WriteLineAsync("Job Entered HelloJob class")
    }
}

1

There are 1 best solutions below

0
Renat On BEST ANSWER

Scheduler has't been started yet, there is a need to call its Start method. From Quartz docs:

IScheduler.Start

Starts the IScheduler's threads that fire Triggers. When a scheduler is first created it is in "stand-by" mode, and will not fire triggers.

like:

public static async Task Main(string[] args)
{
    ...
        
    await DoSomething(scheduler, stoppingToken);

    await scheduler.Start();
    await Task.Delay(TimeSpan.FromSeconds(1));
    await scheduler.Shutdown(true);
}

(example was taken from here)