Enqueuing a Job AT MOST once

I have a job that kicks off, does some work, and then schedules another instance. The interval isn’t consistent, the job logic knows how long to defer when scheduling the next job.

I want this job to be enqueued in hangfire AT MOST once. In other words, if a job with this method signature is already enqueued (running, scheduled, pending, etc), I don’t want to enqueue another instance, if however, this job hasn’t already been enqueued (like the first time this code runs on a server), I’d like the job enqueued when my service starts up.

I haven’t found a “AddOrUpdate” interface for anything other than chron-based recurring jobs.

Is this pattern supported in Hangfire?

Hi. What if you use the backgruoudjob.enqeue at the end of your method? I mean, this way you can enqeue another job when the first one is finished.

so If the job has already been enqueued, I don’t want to enqueue another instance. If it hasn’t been enqueued yet, I want to enqueue it.

Doesn’t that work for what you want? It doesn’t enqueue a new job until the last one has finished its job.

Or do you want to, in the dashboard instance, enqueue a job which will verify if exists or not?

Imagine the first time the code is deployed somewhere, there won’t be any jobs,
therefore the code to schedule the next job will never run.

I’m looking for a solution to bootstrap the first job. If the job doesn’t already exist, enqueue it, if it does exist, don’t enqueue it.

You are correct that when the job does run, it will queue up the next job. The problem is how to get the first job enqueued exactly 1 time.

Oh, I see. So, try to get there through JobStorage.Current.GetMonitoringApi()

For instance, to obtain the queue list would be like

This wasn’t tested as I don’t have any queues right know. But test it and let me know how it turns out.