gpt4 book ai didi

ruby-on-rails - 如何在实际作业中引用事件的延迟作业

转载 作者:行者123 更新时间:2023-12-04 17:37:11 25 4
gpt4 key购买 nike

我正在研究显示延迟作业完成百分比的解决方案(使用 delay_job gem)。目前,我有一个数据库迁移,对于我的 delay_jobs 表,如下所示:

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

我在延迟作业的 Controller 方法中使用排队进程,并在 lib/build_detail.rb 中引用一个类:
Delayed::Job.enqueue(BuildDetail.new(@object, @com))

lib/build_detail.rb 文件如下:
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 不起作用。我看到了 this posting 上提出的 Delayed::Job.current 方法,但是看起来该方法从未包含在主要的delayed_jobs github项目中。

我如何访问当前工作(从实际工作中),以在每次我的工作通过循环时更新进度字段?

最佳答案

现在回答为时已晚,但我遇到了同样的要求,因此可能会对某人有所帮助。
您需要做的就是实现自定义作业和 before -hook 您将在当前工作中存储引用的位置:

class MyTestJob
def before(job)
@job = job
end

def perform
...
@job.update_attributes({ progress: your_progress_var }, without_protection: true)
end
end

关于ruby-on-rails - 如何在实际作业中引用事件的延迟作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14613122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com