检查触发特定工作ID的工作人员姓名

时间:2019-05-14 18:55:14

标签: ruby-on-rails redis sidekiq

我正在研究Ruby on Rails项目。我想知道是否可以使用作业ID获取触发特定作业的工人名称,以便在完成后显示适当的消息。这可能吗?还是应该在模型中保存工作人员姓名?

#I have a method that creates the job

def generate

   my_model.job_id = HardWorker.perform_async()
   my_model.save!
end

def check_status
   if job_id && Sidekiq::Status::complete?(my_model.job_id)
      # if HardWorker
      #    "HardWorker is done!"
      # elseif AnotherWorker
      #    "AnotherWorker is Done!"
      # end
   end
end

1 个答案:

答案 0 :(得分:-1)

您可以使用以下代码通过作业ID获取Worker Class:

queue = Sidekiq::Queue.new
job = queue.detect { |job| job.jid == job_id //<-- the job id you have from perfrom_async }

您的代码将如下所示:

def check_status
  queue = Sidekiq::Queue.new
  job = queue.detect { |job| job.jid == my_model.job_id }

  if job_id && Sidekiq::Status::complete?(my_model.job_id)
    puts "#{job.klass} is done" <-- The worker that triggered the job
  end
end

有关队列here的更多信息