Search code examples
c#asp.netazure-webjobsquartz.netbackground-service

Azure WebJobs vs Quartz.NET


If I have a WebJob project in my application and set the CRON expression in Azure Cloud like in the photo below then why would I need the package Quartz.NET?
The WebJob project has a class inheriting BackgroundService and calls AddHostedService.

enter image description here

My findings

1. Possible disadvantage of Quartz.NET
a. Can I use Quartz.NET with Azure Storage Queue, Tables, etc.? If not, this would be a disadvantage.
b. Quartz.NET seems focused only on time scheduling. If I want a background task firing not from a time trigger, then Quartz.NET has no use?

2. Possible advantage of Quartz.NET
I know you can create multiple jobs with Quartz. In a simple WebJob I would have to repetitively add HostedService for each one.

for (int i = 0; i < workerConfiguration.WorkerCount; i++)
{
   services.AddHostedService<BackgroundTaskService>();
}

Solution

  • If you have a WebJob project in your application and your use case is to set the CRON expression in Azure Cloud, then you might not need the package Quartz_.NET and the BackgroundService class is a part of the .NET Core framework and provides a way to run background tasks in a hosted service.

    b. Quartz.NET seems focused only on time scheduling. If I want a background task firing not from a time trigger, then Quartz.NET has no use?

    a. Can I use Quartz.NET with Azure Storage Queue, Tables, etc.? If not, this would be a disadvantage.

    • Quartz_.NET provides a way to integrate with different job stores, including Azure Storage. You can use the Quartz.Plugins.AzureTable.AdoJobStore package to store job data in Azure Table Storage, As-well-as Quartz.Plugins.AzureQueue.AdoJobStore package can use to store job data in Azure Storage Queue.

    Use IScheduler.ScheduleJob method to schedule multiple jobs with different triggers.

    Here’s an example:

    using Microsoft.Extensions.Hosting;
    using Quartz;
    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    public class QuartzHostedService : IHostedService
    {
        private readonly IScheduler _scheduler;
    
        public QuartzHostedService(IScheduler scheduler)
        {
            _scheduler = scheduler;
        }
    
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await _scheduler.Start(cancellationToken);
    
            // Schedule Job1 every 5 minutes
            var job1 = JobBuilder.Create<Job1>()
                .WithIdentity("job1", "group1")
                .Build();
    
            var trigger1 = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .WithCronSchedule("0 0/5 * * * ?")
                .Build();
    
            await _scheduler.ScheduleJob(job1, trigger1, cancellationToken);
    
            // Schedule Job2 every 10 minutes
            var job2 = JobBuilder.Create<Job2>()
                .WithIdentity("job2", "group1")
                .Build();
    
            var trigger2 = TriggerBuilder.Create()
                .WithIdentity("trigger2", "group1")
                .WithCronSchedule("0 0/10 * * * ?")
                .Build();
    
            await _scheduler.ScheduleJob(job2, trigger2, cancellationToken);
        }
    
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await _scheduler.Shutdown(cancellationToken);
        }
    }