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