Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent Sidekiq from parallel modifications to database
    primarykey
    data
    text
    <p>I have a worker that tries to find a match for a photo based on some conditions. Each photo as to have a unique match. I coded something like this:</p> <pre><code>class PhotoDeliveryWorker include Sidekiq::Worker def perform(photo_id) photo = Photo.find(photo_id) unless photo.match matches = Photo.where(some_condition: "some_value") match = matches.first if match # Do something to photo photo.match = match if photo.save match.some_condition = "another_value" else schedule photo_id end else # Couldn't find a match schedule photo_id end end end private def schedule(photo_id) PhotoDeliveryWorker.perform_in 1.hours, photo_id end end </code></pre> <p>As you can see, worker gets the first model object that passes the conditions and then changes the <code>match</code> to exclude it from future workers matches list. </p> <p>The issue is when several workers perform at a time, they all get the same <code>matches</code> list and as a result modify the same entity. But I need a unique match for each photo.</p> <p>How can I work it around?</p> <p>Additional info:</p> <p>Q: Why do I use workers?</p> <p>A: If I can't find a match, I need to retry later.</p> <p>Q: Why do I use Sidekiq with multiple threads?</p> <p>A: I need do process photos as fast as I could.</p> <p>Probably I could get count of currently active workers in the beginning of each one and take nth match instead of the first. But that solution smells a bit, doesn't it?</p> <p><strong>UPDATE:</strong></p> <p>Additional question: Could I use something about ActiveRecord locking with this issue? I'm not very familiar with <code>with_lock</code> and all this stuff.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload