sintasy
sintasy

Reputation: 637

How to run only one job concurrently?

I have one hangfire server with ~50 recurring jobs. Hangfire setting up on IIS like in this example. Recurring jobs added to hangfire in startup.cs like this:

RecurringJob.AddOrUpdate(job.desctiprion,
            () => job.startJob(), 
            job.cron, 
            TimeZoneInfo.Local);

I need to add new recurring job which running every minute. But there is little chance that this job will be running longer minute. My goal - provide to concurrently work only one exemplar of this job.

What solutions are there? As I understand need something like queue with one thread, but queues don't support thread settings.

Upvotes: 10

Views: 10226

Answers (2)

Yogi
Yogi

Reputation: 9739

You can use DisableConcurrentExecution filter to prevent concurrent executions.

[DisableConcurrentExecution(timeoutInSeconds: 60)]
public void SomeJob()
{
  //method body
}

This filter places a distributed lock in the beginning of a method performance and releases it after it was completed using the IServerFilter interface. The type and method name are being used as a locking resource. Each storage uses its own distributed lock implementation. (read more here)

Upvotes: 17

Hitesh Thakor
Hitesh Thakor

Reputation: 471

 var worker = new BackgroundWorker();
 worker.DoWork += worker_DoWork;
 worker.RunWorkerCompleted += worker_RunWorkerCompleted;
 private void worker_DoWork(object sender, DoWorkEventArgs e)
 {
        // do your task here
 }
 private void worker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
 {

       //Do when completed 
 }

Upvotes: -5

Related Questions