mardi 15 mars 2016

message when sidekiq job is finished

Could someone explain in very simple words or example how the Sidekiq worker works and how you can access the status of the background job?

This is my code and what I want to achieve is when the user submits a job, to be able to see its status. Either with a message that automatically changes on the page or by a 'check status' button that will show the current status of the job.

class PagesWorker
    include Sidekiq::Worker
    include Sidekiq::Status::Worker

    def perform(ifile)
        system "rake ..."

        # What do the following do? In what scenario would I need to set these?
        total 100
        at 5, "Almost done"
        store vino: 'veritas'
        vino = retrieve :vino
    end
end

pages_controller:

job_id = SomeWorker.perform_async(file)
flash[:notice] = "#{job_id} is being processed."

To check the status of the job, I am trying to check when the complete status will be true. I use sidekiq-status gem.

loop do
    stat = Sidekiq::Status::complete? job_id
    break if stat == true
end
flash[:notice] = "#{job_id} is finished."

I have tried the following to troubleshoot what is going on on the background:

myrailsapp/sidekiq where you can see the queues and jobs that are being processed. I have been checking the number of processed jobs (the number indeed increments by one every time I submit a job), the queue (I can see the queue, but its size is always 0, which doesn't seem correct), and the jobs in the queue (I can see no jobs listed here).

However the job is being processed. I can check that from a log file and from the database. Could the reason for the job not appearing be that the job finishes in less that a minute?

So after submitting a job, I checked the following as well:

Sidekiq::Status::complete? job_id #returns false
Sidekiq::Status::status(job_id) #returns nothing
Sidekiq::Status::get_all job_id #returns {"update_time"=>"1458063970", "total"=>"100", "at"=>"5", "message"=>"Almost done"}, and after a while {}

How can I in any way inform the user the the background job is finished?

Aucun commentaire:

Enregistrer un commentaire