知道如何从 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
发布评论