如何在实际作业中引用活动的delay_job [英] How to reference active delayed_job within the actual job
问题描述
我正在研究一种解决方案,以显示延迟工作的完成百分比(使用delay_job gem)。目前,我的delay_jobs表的数据库迁移如下所示:
I am working on a solution to display the percentage completion of a delayed job (using the delayed_job gem). At the present, I have a database migration that looks like the following for my delayed_jobs table:
class CreateDelayedJobs < ActiveRecord::Migration
def self.up
create_table :delayed_jobs, :force => true do |table|
table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
table.text :handler # YAML-encoded string of the object that will do work
table.text :last_error # reason for last failure (See Note below)
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
table.datetime :locked_at # Set when a client is working on this object
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
table.string :locked_by # Who is working on this object (if locked)
table.string :queue # The name of the queue this job is in
table.integer :progress
table.timestamps
end
add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
end
def self.down
drop_table :delayed_jobs
end
end
我正在控制器方法中使用排队过程来处理延迟的工作,并引用lib / build_detail.rb中的类:
I am using an enqueue process within a controller method for a delayed job, and referencing a class in lib/build_detail.rb :
Delayed::Job.enqueue(BuildDetail.new(@object, @com))
lib / build_detail.rb文件如下:
The lib/build_detail.rb file is as follows:
class BuildDetail < Struct.new(:object, :com)
def perform
total_count = object.person_ids.length
progress_count = 0
people = com.person object.person_ids do |abc|
progress_count += abc.size
Delayed::Job.current.update_attribute :progress, (progress_count/total_count)
end
end
end
Delayed :: Job.current不起作用。我看到了此帖子上建议的Delayed :: Job.current方法,但是看来该方法从未包含在main delay_jobs github项目中。
Delayed::Job.current doesn't work. I see the Delayed::Job.current method proposed on this posting, however it looks like the method was never included in the main delayed_jobs github project.
如何从当前作业中访问当前作业每次工作循环时更新进度字段吗?
How can I access the current job (from within the actual job), to update the progress field each time my job goes through the loop?
推荐答案
答案来得太晚了,但是我面临着同样的要求,因此可能会对某人有所帮助。
您需要做的就是实现自定义作业,并在之前
挂接,您将在其中存储当前作业的引用:
It's to late to answer but I've faced with the same requirement so may be it will help someone.
All you need to do is to implement custom job and before
-hook where you will store reference on current job:
class MyTestJob
def before(job)
@job = job
end
def perform
...
@job.update_attributes({ progress: your_progress_var }, without_protection: true)
end
end
这篇关于如何在实际作业中引用活动的delay_job的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!