liveBook · Manning

DisallowConcurrentExecution

As explained in ASP.NET Core in Action, 3e

DisallowConcurrentExecution is a Quartz.NET attribute that prevents overlapping executions of the same job definition.

DisallowConcurrentExecution prevents Quartz.NET from starting another instance of a job while the same job definition is already running. It is especially useful for long-running jobs whose triggers fire frequently. section 34.3.2 section 34.4

Preventing overlapping exchange-rate updates

Decorate the Quartz.NET job with the attribute: [DisallowConcurrentExecution] public class UpdateExchangeRatesJob : IJob. If its trigger fires every five minutes while an update is still running, Quartz.NET waits instead of starting another instance. section 34.3.2 section 34.4

Listing 34.12 Configuring a Quartz.NET IJob and trigger
using Quartz;
 
IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((hostContext, services) =>
    {
        services.AddQuartz(q =>
        {
            q. UseMicrosoftDependencyInjectionJobFactory();
    
            var jobKey = new JobKey("Update exchange rates");   #1
            q.AddJob<UpdateExchangeRatesJob>(opts =>            #2
                opts.WithIdentity(jobKey));                     #2
 
            q.AddTrigger(opts => opts                           #3
                .ForJob(jobKey)                                 #3
                .WithIdentity(jobKey.Name + " trigger")         #4
                .StartNow()                                     #5
                .WithSimpleSchedule(x => x                      #6
                    .WithInterval(TimeSpan.FromMinutes(5))      #6
                    .RepeatForever())                  
            );
        });
 
        services.AddQuartzHostedService(
            q => q.WaitForJobsToComplete = true);
    })
    .Build();
host.Run();
#1 Creates a unique key for the job, used to associate it with a trigger
#2 Adds the IJob to the DI container and associates it with the job key
#3 Registers a trigger for the IJob via the job key
#4 Provides a unique name for the trigger for use in logging and in clustered scenarios
#5 Fires the trigger as soon as the Quartz.NET scheduler runs on app startup
#6 Fires the trigger every 5 minutes until the app shuts down

Shows the Quartz.NET job registration and trigger configuration that determine how often a protected job is requested. section 34.3.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines attribute placement and runtime behavior, but does not explain Quartz.NET concurrency internals.
Examples
Uses an exchange-rate update job with a five-minute trigger as the concrete scheduling scenario.
What it assumes
Assumes a registered Quartz.NET IJob and an associated trigger configured through dependency injection.
What you can do after
Apply it via Quartz.NET to keep a frequently triggered long-running job from running concurrently.
How it connects
Connects Quartz.NET jobs with triggers, dependency injection, database persistence, and clustering.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.