Rails how to tell if a sidekiq worker is done with perform_async

China☆狼群 提交于 2019-12-06 06:07:26

问题


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.

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 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

called from my controller:

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:


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

This is from the read me:

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"


来源:https://stackoverflow.com/questions/32940007/rails-how-to-tell-if-a-sidekiq-worker-is-done-with-perform-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!