两个工作,同一个工人使用带 Sidekiq 的 ActiveJob
Two Jobs, Same Worker using ActiveJob w/ Sidekiq
对于不同的工作,是否有支持或反对使用相同工作 class 的推荐惯例?我正在努力避免为从父级生成的子作业创建单独的作业 class。
我知道 Sidekiq::Batch 可能会处理来自父项的跟踪派生作业,但它无法解决对不同作业使用相同 class 的问题。
例如:
# 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 的最佳实践,这是惯例。 SRP 等等。
对于不同的工作,是否有支持或反对使用相同工作 class 的推荐惯例?我正在努力避免为从父级生成的子作业创建单独的作业 class。
我知道 Sidekiq::Batch 可能会处理来自父项的跟踪派生作业,但它无法解决对不同作业使用相同 class 的问题。
例如:
# 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 的最佳实践,这是惯例。 SRP 等等。