Rails如何判断sidekiq worker是否完成了perform_async

时间:2015-10-05 01:07:35

标签: ruby-on-rails ruby redis sidekiq

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

我能够让它正常运行。

但我不确定如何检查sidekiq工作人员的进度 - 检查工作人员是否完成了perform_async功能的最佳方法是什么?

AutoWorker sidekiq任务:

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

1 个答案:

答案 0 :(得分:4)

将以下扩展gem检出到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"