Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz .NET - Prevent parallel Job Execution

I am using Quartz .NET for job scheduling. So I created one job class (implementing IJob).

public class TransferData : IJob
{
    public Task Execute(IJobExecutionContext context){
       string tableName = context.JobDetail.JobDataMap.Get("table");
       // Transfer the table here.
    }
}

So I want to transfer different and multiple tables. For this purpose I am doing something like this:

foreach (Table table in tables)
{
    IJobDetail job = JobBuilder.Create<TransferData>()
        .WithIdentity(new JobKey(table.Name, "table_transfer"))
        .UsingJobData("table", table.Name)
        .Build();

    ITrigger trigger = TriggerBuilder.Create()
        .WithIdentity(new TriggerKey("trigger_" + table.Name, "table_trigger"))
        .WithCronSchedule("*/5 * * * *")
        .ForJob(job)
        .Build();

    await this.scheduler.ScheduleJob(job, trigger);
}

So every table should be transfered every 5 minutes. To achieve this I create several jobs with different job names. The question is: how to prevent the parallel job execution for the same jobName? (e.g. the previous run takes longer for one table, so I do not want to start the next transfer for the same table.)

I know about the attribute @DisallowConcurrentExecution, but this is used to prevent the parallel execution for the same Job class. I do not want to write an extra Job class per table, because the "main" code for the transfer is always the same, the one and only difference is the table name. So I want to use the same job class for this purpose.

like image 966
Zar Avatar asked Jan 24 '18 14:01

Zar


Video Answer


1 Answers

The Quatz .NET documentation is a little bit confusing.

DisallowConcurrentExecution is an attribute that can be added to the Job class that tells Quartz not to execute multiple instances of a given job definition (that refers to the given job class) concurrently. Notice the wording there, as it was chosen very carefully. In the example from the previous section, if “SalesReportJob” has this attribute, than only one instance of “SalesReportForJoe” can execute at a given time, but it can execute concurrently with an instance of “SalesReportForMike”. The constraint is based upon an instance definition (JobDetail), not on instances of the job class. However, it was decided (during the design of Quartz) to have the attribute carried on the class itself, because it does often make a difference to how the class is coded.

Source: https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/more-about-jobs.html

But if you read the API documentation, it's says: the bold text is important!

An attribute that marks a IJob class as one that must not have multiple instances executed concurrently (where instance is based-upon a IJobDetail definition - or in other words based upon a JobKey).

Source: https://quartznet.sourceforge.io/apidoc/3.0/html/

In other words: the DisallowConcurrentExecution attribute works for my purposes.

like image 56
Zar Avatar answered Sep 19 '22 23:09

Zar