加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何在实际作业中引用活动的delayed_job

发布时间:2020-12-17 03:39:30 所属栏目:百科 来源:网络整理
导读:我正在研究一种解决方案,以显示延迟作业的完成百分比(使用delayed_job gem).目前,我的delayed_jobs表的数据库迁移类似于以下内容: class CreateDelayedJobs ActiveRecord::Migration def self.up create_table :delayed_jobs,:force = true do |table| tabl
我正在研究一种解决方案,以显示延迟作业的完成百分比(使用delayed_job gem).目前,我的delayed_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

我在一个控制器方法中使用一个enqueue进程来处理延迟的作业,并在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

延迟:: Job.current不起作用.我在this posting上看到了Delayed :: Job.current方法,但看起来这个方法从未包含在主delayed_jobs github项目中.

如何在每次作业完成循环时访问当前作业(从实际作业中),更新进度字段?

解决方法

现在回答很晚但是我遇到了同样的要求,所以可能会对某人有所帮助.
您需要做的就是实现自定义作业和挂钩,您将在当前作业中存储引用:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读