3

I have a sidekiq worker class. I currently implemented it this way. It works when I call PROCESS, and it will queue the method called PERFORM. But i would like to have more than one method that I can queue.

As a side note, is there a difference doing this and simply doing SocialSharer.delay.perform?

# I trigger by using SocialSharer.process("xxx")

class SocialSharer

  include Sidekiq::Worker

  def perform(user_id)
    # does things
  end

  def perform_other_things
    #i do not know how to trigger this
  end

  class << self
    def process(user_id)
      Sidekiq::Client.enqueue(SocialSharer,user_id)
    end
  end

end
4

2 回答 2

7

SocialSharer.delay.perform 会延迟一个名为 perform 的类方法。您的 perform 方法是一个实例方法。

工人被设计为每个工作一个班级。作业通过 perform 方法启动。您可以使用延迟在一个类上启动任意数量的不同类方法,如下所示:

class Foo
  def self.a(count)
  end
  def self.b(name)
  end
end
Foo.delay.a(10)
Foo.delay.b('bob')
于 2013-03-13T20:56:11.297 回答
-3

好吧,如果您真的想在一个类中包含所有“可执行”方法,我建议您将perform方法重命名为不同的名称(例如,perform_something),并创建一个可以调度控制流的新perform方法:

class SocialSharer
  include Sidekiq::Worker

  # the 3 lines below may be replaced with `alias_method :perform, :public_send`
  def perform(method, *args)
    self.public_send(method, *args)
  end

  def perform_something(user_id)
    # does things
  end

  def perform_other_things
    # does other things
  end

  def self.process(user_id)
    Sidekiq::Client.enqueue(SocialSharer, :perform_something, user_id)
    Sidekiq::Client.enqueue(SocialSharer, :perform_other_things)
  end
end
于 2013-03-13T21:08:43.167 回答