# How to use prevent overlapping jobs? DistributedMutex?

**URL:** https://meta.discourse.org/t/how-to-use-prevent-overlapping-jobs-distributedmutex/130393
**Category:** Development
**Created:** [October 7, 2019, 9:23am UTC](https://meta.discourse.org/t/how-to-use-prevent-overlapping-jobs-distributedmutex/130393 "2019-10-07T09:23:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mark\_Schmucker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mark_schmucker/32/124810_2.png) [@Mark\_Schmucker](https://meta.discourse.org/u/Mark_Schmucker)
#### Post date: [October 7, 2019, 9:23am UTC](https://meta.discourse.org/t/how-to-use-prevent-overlapping-jobs-distributedmutex/130393/1 "2019-10-07T09:23:17Z")

</div>

Plugin newbie here. I’m trying to adapt a plugin which takes about an hour to run. Looking at /sidekiq, I see the job is running every 30 minutes. So before the first job is finished, another instance of the same job starts, creating duplicate results. How can I prevent this?

One option of course is to make the job run in its allowed 30 minutes, but there are other constraints, and I would rather let it run as long as it wants.

I tried this, but it doesn’t seem to prevent a second instance of the job from running:

`DistributedMutex.synchronize("custom_digest", validity: 180.minutes)`

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 7, 2019, 10:08am UTC](https://meta.discourse.org/t/how-to-use-prevent-overlapping-jobs-distributedmutex/130393/2 "2019-10-07T10:08:42Z")

</div>

I think that the summary email job does such a test that was added early this year. You might have a look at that.

---

<div class="post-metadata">

### Author: ![Mark\_Schmucker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mark_schmucker/32/124810_2.png) [@Mark\_Schmucker](https://meta.discourse.org/u/Mark_Schmucker)
#### Post date: [October 7, 2019, 11:44pm UTC](https://meta.discourse.org/t/how-to-use-prevent-overlapping-jobs-distributedmutex/130393/3 "2019-10-07T23:44:36Z")

</div>

I think I am doing the same as [https://review.discourse.org/t/feature-allow-post-process-mutex-to-be-held-longer/5017](https://review.discourse.org/t/feature-allow-post-process-mutex-to-be-held-longer/5017) and [https://review.discourse.org/t/fix-post-and-topic-creation-race-condition/5247](https://review.discourse.org/t/fix-post-and-topic-creation-race-condition/5247), with the exception of using curly braces instead of do/end, but I don’t think that matters.

DistributedMutex.synchronize(“custom\_digest”, validity: 180.minutes) {  
do\_stuff  
}

Yet do\_stuff is running multiple times concurrently, and well within the 180 minutes.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [October 8, 2019, 1:47am UTC](https://meta.discourse.org/t/how-to-use-prevent-overlapping-jobs-distributedmutex/130393/4 "2019-10-08T01:47:07Z")

</div>

If the mutex is inside the `execute` block it just means it will block there waiting for it, meaning that you will see two jobs running, one actually running and another waiting for the mutex.

Maybe you want to check for it and return early if there is another instance running? Hard to guess knowing so little about the exact use case.
