socialpandas / sidekiq-superworker

Directed acyclic graphs of Sidekiq jobs
MIT License
438 stars 34 forks source link

How to pass arguments for the same worker? #27

Closed EMRJ closed 10 years ago

EMRJ commented 10 years ago

@tombenner @natemilbee @jmouel, I want to call superworker using another worker not from the initailzer, how do i call superworker without the initializer?

I have a worker called parallel worker in that i have to initialize superworker, Is that possible to call superworker from another worker

e.g class ParallelWorker include Sidekiq::Worker include Sidekiq::Superworker

def perform Superworker.create(:MySuperworker) do parallel do Worker1 'str1', 'str2', 1, 1 Worker2 'str1', 'str2', 1, 1 end end end end

When above worker is called it gives warning like "/home/.../lib/sidekiq/superworker/worker.rb:24: warning: already initialized constant MySuperworker"

tombenner commented 10 years ago

When you call Superworker.create(:MySuperworker), it defines a new class called MySuperworker, so you should only call it once (e.g. in an initializer). You shouldn't call it within a perform method, since it will define the same class multiple times, and you'll get that warning you've mentioned.

If you need to pass arguments to a Superworker from within a regular worker, you can set up the Superworker to take arguments:

# config/initializers/superworkers.rb
Superworker.create(:MySuperworker, :string1, :string2, :int1, :int2) do
  parallel do
    Worker1 :string1, :string2, :int1, :int2
    Worker2 :string1, :string2, :int1, :int2
  end
end

# app/workers/parallel_worker.rb
class ParallelWorker
  include Sidekiq::Worker
  def perform
    MySuperworker.perform_async('str1', 'str2', 1, 1)
  end
end

If this doesn't address what you're asking, please open up a new issue with an example of what you'd like to do.