How to stop a job being created if the recurring job (job) is still processing

aspnetcore
Tags: #<Tag:0x00007f49ac031f68>

#1

Ok… The title is confusing but the problem is fairly simply to understand.

Im Using ASP.NET Core 3.0.

The idea is that I have to “Listen” to different folders, lets say 2 folders and check whether they have files or not. This Recurring job must execute every 5 minutes, BUT the thing is, if the folder have files, I must do a certain amount of checkings, and executions, etc, it doesn’t matter.

What matters is that, the NEXT execution must not execute if I did not finished the processing one.

So in my startup class I create a recurring job for each folder to listen (this is a POC)

public void RecurringJobMonitorFolders()
{
    List<string> Folders = new List<string>()
    {
        Path.Combine("C:\\MCF","20191211"),
        Path.Combine("C:\\MCF","20191212"),
    };

    foreach (var item in Folders)
    {
        RecurringJob.AddOrUpdate<IncomingManager>(item,x => x.VerifyMcfFiles(item), "0 0/5 * 1/1 * *");
    }
    
}

public void VerifyMcfFiles(string path)
{
    DirectoryInfo info = new DirectoryInfo(path);

    if(info.GetFileSystemInfos().Count() != 0)
    {
        //This can take maybe more than 5 minutes
        ProcessFiles(path);
    }
}

The idea is, to check if the new job that the recurring job creates is going to be eliminated or not based if a job with that corresponding recurrent job (id) is processing.