用sidekiq调用另一名工人的工人,

时间:2015-10-29 22:56:42

标签: ruby-on-rails ruby multithreading sidekiq

我在使用sidekiq在另一名工人内部调用worker时遇到问题。 我有两个看起来像这样的工人:

class UserExportWorker
  include Sidekiq::Worker

  sidekiq_options queue: :users, unique: true, retry: false

  def perform(user_id)
    # code that exports user to external service via API
    UserExportStatusWorker.perform_in(1.minute, user_export_id, user_id)
  end
end

class UserExportStatusWorker
  include Sidekiq::Worker

  sidekiq_options queue: :users, unique: true, retry: false

  def perform(user_export_id, user_id)
    # code that check if exporting user to external service via API is finished
    if export.completed?
      user.update(status: 'exported')
    else
      UserExportStatusWorker.perform_in(1.minute, user_export_id, user_id)
    end
  end
end

UserExportWorker通过API将用户导出到某个外部服务并运行UserExportStatusWorker,它将检查外部API中的导出是否已完成,如果不是,那么它将再次启动相同的工作程序,当导出完成时,它将更新用户状态列数据库。问题是即使导出到外部API成功,UserExportStatusWorker也不会更新数据库中的用户。但是,当我将代码更改为:

class UserExportWorker
  include Sidekiq::Worker

  sidekiq_options queue: :users, unique: true, retry: false

  def perform(user_id)
    # code that exports user to external service via API
    UserExportStatusWorker.new.perform(user_export_id, user_id)
  end
end

class UserExportStatusWorker
  include Sidekiq::Worker

  sidekiq_options queue: :users, unique: true, retry: false

  def perform(user_export_id, user_id)
    # code that check if exporting user to external service via API is finished
    if export.completed?
      user.update(status: 'exported')
    else
      UserExportStatusWorker.new.perform(user_export_id, user_id)
    end
  end
end

我完全没有想法......为什么这不起作用?

1 个答案:

答案 0 :(得分:0)

理想情况下,要在Worker上重新启动,您应该引发错误,并让sidekiq重试它,如docs中所述。

因此,在您的情况下,在您的UserExportWorker类中,您可以使用一种方法来检查导出是否成功运行,如果没有,则引发自定义ExportNotSuccessfulError