How to avoid having a recurring job start if a manually started version of it is already running?

I’m getting a bit confused trying to hangfire to do what I want and i’m not sure of the best approach to the problem.

I have a bunch of recurring jobs that are queued up when the application is started with the RecurringJob.AddOrUpdate() function and that works well. They happily execute when they are supposed to and I have found the bits of the api the get out job information, so I can display it all on an angular front end.

I do however have the need to run a recurring job on demand if a user wants to. I’ve managed to get this to work using the BackgroundJob.Enqueue() method.

Where i’ve run into a problem is that if I have a long running job that is currently being executed after a manual start it will be started by the Recurring job scheduler when it is due to start. I had expected Hangfire to see it was already running and not try to start it. I can handle the reverse case easily by just greying out the run button when the task is already running.

What I am wondering is, what is the best way to prevent the recurring job from starting if the other one is still running? Add some locking in my job that is being started to check if it is already running and just exit with success? That seems like i’m working around rather than working with hangfire so doesn’t seem like the best approach.

Maybe delete the recurring job and have the manually started version requeue it when it completes? This seems like it would work but feels like working around hangfire again, and would also require making the tasks aware of hangfire in a way i’d prefer they don’t need to be if possible.

What am I missing? Is there a way to do this in hangfire that doesn’t require the task to know anything abou it?

Jason

I found the JobFilters and the OnState Applied function and that looks like it will do what I want and let me check if a job is running and abort if it is.