Home > Enterprise >  Avoid duplicate sidekiq job
Avoid duplicate sidekiq job

Time:10-08

I have a worker that runs when the user selects a time. If the user selects a time twice, the worker runs twice. How do I avoid it from being executed multiple times? I mean, If the user selects to be executed after 10 minutes, then deletes this request and again selects to be executed after 10 minutes, the worker executed twice.

class EnableWorker
  include Sidekiq::Worker
  sidekiq_options queue: :general, retry: 0

  def perform(enable_at)
    puts enable_at
  end
end

CodePudding user response:

You could use sidekiq-unique-jobs gem and use it somewhat like this. (For Rails 3 probably you can refer this version - 4.0.18)

class EnableWorker
  include Sidekiq::Worker
  sidekiq_options queue: :general,
                  retry: 0
                  unique: :until_executed,
                  unique_args: ->(args) { args }

  def perform(enable_at)
    puts enable_at
  end
end

CodePudding user response:

From your description, it looks like you can handle it on application level. Store in the database that user has selected time. Check database and do not schedule if time was already selected. This is not super precise but might work for some normal use-cases.

For more accurate implementation, Sidekiq enterprise has unique jobs feature. For the free version you can either go with sidekiq-unique-jobs as mentioned in another answer.

  • Related