本文介绍了Rails 如何判断一个 sidekiq 工作者是否使用 perform_async 完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一项广泛的后台任务外推到 sidekiq 工作人员(第一次使用 sidekiq).

I'm working on extrapolating an extensive background task to a sidekiq worker (first time working with sidekiq).

我已经能够让它正确运行.

I've been able to get this to run correctly.

但我不确定如何检查 sidekiq 工作器的进度 - 检查工作器是否使用 perform_async 函数完成的最佳方法是什么?

But I'm not sure how to check on the progress of the sidekiq worker - what's the best way to check to see if the worker is done with the perform_async function?

AutoWorker sidekiq 任务:

AutoWorker sidekiq task:

class AutoWorker
    include Sidekiq::Worker

    def perform(lead_id, cars)
        logger.info "WORKER CREATED"
        lead = Lead.find(lead_id)
        response = ZipCodeCheck.new(cars: cars, lead: lead).execute
    end
end

从我的控制器调用:

def update
    respond_to do |format|
      if @lead.update(lead_params)
          cars = params[:car_array]

          AutoWorker.perform_async(@lead.id, cars)
          format.json { render action: 'show', status: :created, location: @lead }
        else
          format.html { redirect_to @lead, notice: 'Lead was successfully updated.' }
          format.json { render action: 'show', status: :created, location: @lead}
        end
      else
        format.html { render action: 'edit' }
        format.json { render json: @lead.errors, status: :unprocessable_entity }
      end
    end
  end

推荐答案

将以下扩展 gem 签出到 sidekiq:https://github.com/utgarda/sidekiq-status/blob/master/README.md

Checkout the following extension gem to sidekiq: https://github.com/utgarda/sidekiq-status/blob/master/README.md

这是自述:

job_id = MyJob.perform_async(*args)
data = Sidekiq::Status::get_all job_id
data # => {status: 'complete', update_time: 1360006573, vino: 'veritas'}
Sidekiq::Status::get     job_id, :vino #=> 'veritas'
Sidekiq::Status::at      job_id #=> 5
Sidekiq::Status::total   job_id #=> 100
Sidekiq::Status::message job_id #=> "Almost done"

这篇关于Rails 如何判断一个 sidekiq 工作者是否使用 perform_async 完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:25