vendredi 11 mars 2016

Sending a flash message when a Sidekiq job finishes in Rails

I have a Rails app, where the user uploads a file and receives a flash[:notice] that his job is under process. So the analysis of this file is done in the background (I use Sidekiq) and what I am trying to do is to return a flash[:success] when the job completes (I use Sidekiq-status).

My thought was to check every x seconds the status of the job and when the completed flag is true, send the message.

Controller:

require 'thread'

job_id = SomeWorker.perform_async(file)
flash[:notice] = "Job submitted."

Thread.new do
    sleep(5)
    finished = Sidekiq::Status::complete? job_id
    case finished
    when true
        flash[:success] = "Job finished."
    when false
        flash[:notice] = "Almost there.."
    end
end

However it doesn't seem to get in the Thread loop, the last thing it sends is "Job submitted.", why is that? Is there a better practice to approach this? Thanks!

Aucun commentaire:

Enregistrer un commentaire