ace
ace

Reputation: 305

Sidekiq Unique Job Processing

I need to assure that no more than one job per user_id is worked simultaneosly by any of the 25 workers to avoid deadlocks.

I have tried sidekiq unique jobs but deadlocks keep occoring because it keeps trying to process all pending jobs on the queue without looking for the user_id on the params.

Thank you

class MyWork   
  include Sidekiq::Worker   
  sidekiq_options :queue => "critical", unique: true,
                   unique_args: ->(args) { [ args.user_id ] }


  def perform(work_params)

Upvotes: 3

Views: 7846

Answers (1)

Bruno Peres
Bruno Peres

Reputation: 3256

The sidekiq_option args is an array that has your works_params on the first position. You should then do:

class MyWork
  include Sidekiq::Worker
  sidekiq_options :queue => "critical", unique: until_executed, 
                   unique_args: ->(args) { [ args.first.user_id ] }

  def perform(work_params)

Note that unique: true is deprecated, so you should use unique: until_executed or whatever fits best for your worker. See here for other options.

Upvotes: 4

Related Questions