mhenrixon / sidekiq-unique-jobs

Prevents duplicate Sidekiq jobs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Different unique arguments depending on lock type

stanley90 opened this issue · comments

Hi
I want to use the until_and_while_executing option but with different unique arguments. Let's say my worker has two arguments: id, date
I want the jobs to be unique by both arguments in the queue, but only by the id during execution.
Is this possible?
Thanks

I think this could be achieved by sending the lock type to unique_args so the method can decide, but I don't want to hack too much right now, so I think I'll check the caller to determine whether it's in Sidekiq (runtime) or not (queuing).

What solution did you end up with?

I did this:

sidekiq_options unique: :until_and_while_executing, unique_args: :unique_args

def self.unique_args(args)
  if Sidekiq::ProcessSet.new.size > 1
    # sidekiq runtime; uniqueness for the object (first arg)
    args.first
  else
    # queuing from the app; uniqueness for all params
    args
  end
end

Thanks for the quick response @stanley90. Interesting solution, idn't know about ProcessSet! Does it work with the currently performing job somehow or is it more generic?

I don't remember exactly, it's been a while. I was looking for a way to distinguish in which context is the current process running, and found this (there might be other ways too); I think it was 0 in the app context (queuing) and a positive value in the actual background processing. Now looking at it, maybe the condition should actually be > 0, not > 1 :-)

I'm adding some information in the README for this and closing the issue.