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

编程入门 行业动态 更新时间:2024-10-21 12:42:57
本文介绍了Rails 4.2 从活动作业中获取延迟的作业 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

知道如何从 ActiveJob 排队中获取 Delayed::Job id 吗?当我将作业排入队列时,我会返回一个带有 @job_id 的 ActiveJob::Base 实例,但该作业 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

但这似乎是各种各样的hacky.有点惊讶 ActiveJob 没有从 Delayed::Job 返回 ID,特别是因为这是作业入队时显式返回的 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:20:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637482.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   Rails   ID

发布评论

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

>www.elefans.com

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