Pergunta

I have a case scenario where I need to run multiple record updates in the background(using resque) and I want to give user visual indicator of how the task is running(eg started/running/finished).

One way of achieving this(which I can think of) is saving the current state into a table, then showing the state to user by simple page refresh.

Can anyone suggest a better solution of doing it?I want to avoid creating the whole migration, model, controller for this.

Thanks

Foi útil?

Solução

As I've commented, resque-status gem could be useful for you. I am not sure if that is an answer but since you said that you do not want to create migration, model and controller for this. Thus, a gem might be the way to go.

From the job id you can get the status you are looking for, for example:

status = Resque::Plugins::Status::Hash.get(job_id)
status.working? #=> true

There is also a front-end called resque-web, check that out too.

Outras dicas

You may use ruby's global variable $var_name = 'foo'. However I am not sure about it, because they are considered bad practice in rails, but in this case I see them reasonable, as soon as their name is very unique. It can be done like (in case of resque):

class UpdateJob
  @queue = data

  def self.perform
    $my_job_name_is_running = true
    MyJobName.new.run
    $my_job_name_is_running = nil
  end
end

then you can access them from anywhere in the app:

while $my_job_name_is_running
  puts "job is running..." if $my_job_name_is_running
  sleep 3 # important to not overload your processor
end

Ruby global vars are not very popular. Check docs for more info https://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/globalvars.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top