Rails 4.2从活动作业获取延迟的作业ID [英] Rails 4.2 get delayed job id from active job

查看:61
本文介绍了Rails 4.2从活动作业获取延迟的作业ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何从ActiveJob排队中获取 Delayed :: Job id吗?当我排队工作时,我得到一个 ActiveJob :: Base 的实例,该实例带有 @job_id ,但是该工作ID似乎在ActiveJob内部。到目前为止,我最好的猜测就是走最近创建的工作:

Any idea how to get the Delayed::Job id from the ActiveJob enqueuing? When I enqueue a job I get back an instance of ActiveJob::Base with a @job_id, but that job id seems to be internal to ActiveJob. My best guess so far is just to walk down the most recently created jobs:

active_job_id = GenerateReportJob.perform_later(self.id).job_id
delayed_job = Delayed::Job.order(id: :desc).limit(5).detect do |job|
  YAML.load(job.handler).job_data['job_id'] == active_job_id
end

但这似乎各种各样。有点惊讶的ActiveJob不会从 Delayed :: Job 返回ID,尤其是因为这是在作业入队时显式返回的。

but that seems all kinds of hacky. Kind of surprised ActiveJob isn't returning the ID from Delayed::Job, especially since that is what is explicitly returned when the job gets enqueued.

==编辑

好像我不是唯一的一个( https://github.com/rails/rails/issues/18821

Looks like I'm not the only one (https://github.com/rails/rails/issues/18821)

推荐答案

以防将来有人发现:Rails刚刚接受了一个补丁,使您可以从Rails 5的provider_job_id中获取此ID。您可以将其与

In case anyone finds this in the future: Rails just accepted a patch to allow you to get this id from provider_job_id in Rails 5. You can get it to work with a patch like

ActiveJob::QueueAdapters::DelayedJobAdapter.singleton_class.prepend(Module.new do
  def enqueue(job)
    provider_job = super
    job.provider_job_id = provider_job.id
    provider_job
  end

  def enqueue_at(job, timestamp)
    provider_job = super
    job.provider_job_id = provider_job.id
    provider_job
  end
end)

这篇关于Rails 4.2从活动作业获取延迟的作业ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆