Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 700
ace Avatar asked Sep 05 '26 13:09

ace


1 Answers

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.

like image 129
Bruno Peres Avatar answered Sep 08 '26 03:09

Bruno Peres