【问题标题】:Two Jobs, Same Worker using ActiveJob w/ Sidekiq两个作业,同一个工人使用带有 Sidekiq 的 ActiveJob
【发布时间】:2018-02-23 20:49:47
【问题描述】:

是否有推荐的惯例来支持或反对为不同的工作使用相同的工作类别?我试图避免为从父级生成的子作业创建单独的作业类。

我意识到 Sidekiq::Batch 可以处理来自父级的跟踪派生作业,但它不能解决对不同作业使用同一个类的问题。

例如:

# not ideal: requires separate child job class
class ParentJob < ApplicationJob
  def perform(*args)
    MyModel.all.each { |f| ChildJob.perform_later(f) }
  end
end

# ideal - allows re-use of same class
class ParentJob < ApplicationJob
  def perform(*args)
    # doesn't work, but shown here conceptually
    MyModel.all.each { |f| self.class.perform(:foo, f) }
  end

  def foo(f); end
end

我可以查看父作业的参数,如果它包含一个特殊的键,分支并运行不同的方法,但我不喜欢这种方法的模棱两可的合同。

【问题讨论】:

    标签: sidekiq rails-activejob


    【解决方案1】:

    使用两个类。这是 Sidekiq 的最佳实践,这就是惯例。 SRP 等等。

    【讨论】:

      猜你喜欢
      • 2017-02-19
      • 2015-03-29
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多