4

I want to implement the Quartz.Net in my C#.Net project. I am syncing 1 FTP and downloading all file every 10 minutes. Once I downloaded every file, I am going to process them all. If there are be more files, it will take more time than 10 minutes.

How to run 1 instance of a job and, if the trigger sets off any another job instance, it should not run and dispose it?

My Code:

Quartz Scheduler Class

public class ResponseFileSyncJobScheduler
{
    public static async Task StartResponsefileSyncker()
    {
        ISchedulerFactory sf = new StdSchedulerFactory();
        IScheduler sched = await sf.GetScheduler();

        //DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);

        IJobDetail job = JobBuilder.Create<ResponseFileJob>()
            .WithIdentity("ResponsefileSync", "sgroup")
            .Build();

        ITrigger trigger = TriggerBuilder.Create().WithIdentity("TriggerfileSync", "sgroup")
            .WithDailyTimeIntervalSchedule
            (s => s.WithIntervalInMinutes(10).OnEveryDay())
            .StartAt(DateTime.Now.AddMinutes(1))
            .Build();

        await sched.ScheduleJob(job, trigger);
        await sched.Start();
        await Task.Delay(TimeSpan.FromSeconds(65));
    }
}

Quartz Job Class

public class QuartzJobSOGET : IJob
{
    internal static readonly Task CompletedTask = Task.FromResult(true);

    public virtual Task Execute(IJobExecutionContext context)
    {
        JobKey jobKey = context.JobDetail.Key;

        //Write Date Time to File - to ensure Quartz Scheduler Working   
        WritetoFile(DateTime.Now.ToString());

        //Sync every present file and process 
        //once processed delete file from server and make log, 
        //will take 1 min to process single file (almost)
        SyncResponseFilefromSOGETftp();

        return CompletedTask;
    }
}

1 Answers1

3

After a long time, I found the answer earlier, but could not able to post here.

We can use the DisallowConcurrentExecution Attribute over the class to stop multiple executions of the single job.

Reference : Job State and Concurrency

However, you can also check out this link for Git-Issue: (could be useful) @DisallowConcurrentExecution misinterpretation on clustered Scheduler