Rails 4.2从活动作业获取延迟的作业ID

编程入门 行业动态 更新时间:2024-10-21 05:35:52
本文介绍了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.

==编辑

好像我不是唯一的一个( github/rails/rails/issues/18821 )

Looks like I'm not the only one (github/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

本文发布于:2023-11-27 09:19:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637479.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   Rails   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!