Matthew Berman
Matthew Berman

Reputation: 8631

Sidekiq: how to pass a queue as a variable into a worker

I have a worker like so:

class MassDispatchServiceWorker
  include Sidekiq::Worker
  sidekiq_options queue: :bulk

  def perform(message_params)
    DispatchService.new(message_params).dispatch!
  end
end

However I have a need for the same exact method but to pass in a queue as a variable. Sometimes I want the queue to be bulk, other times I'll need something else. Is that possible with sidekiq?

Upvotes: 5

Views: 5803

Answers (2)

Drakula2k
Drakula2k

Reputation: 330

There is another way:

MyWorker.set(queue: 'foo').perform_async(1)

From https://github.com/mperham/sidekiq/issues/2152#issuecomment-290376790

Upvotes: 12

Mike Perham
Mike Perham

Reputation: 22228

Use the Sidekiq::Client API.

Sidekiq::Client.push('class' => MassDispatchServiceWorker, 'args' => [], 'queue' => whatever)

Upvotes: 0

Related Questions