How to alert user when a resque job finishes

心不动则不痛 提交于 2019-12-08 03:36:32

问题


I have a ruby on rails app using the resque gem to process background jobs. I have users trigger background jobs that can take upwards of 10 minutes to complete (they're processing a ton of data). What is the best way to alert the user when the job has completed?

I've seen the answer here about using resque-status, is that the only solution? Or is there another way to alert the user through normal resque (which I've already implemented)?


回答1:


If you intend to notify a user asynchronously, you have three options:

  1. Set a flag in the database, and then change your UI the next time they log in. (Like popping in an alert view or something.)
  2. Send them an email at the conclusion of the job's run. Just use the standard ActionMailer stuff for this.
  3. If you're using a pubsub framework like Faye or Juggernaut, push the user a notification that the job is done. If they're signed in they should see it immediately.

Those are pretty much the only things I can think of. I would personally go with a combination of 1 and 2; send them an email on completion, and notify them (and provide a link in the notification) the next time they log in.




回答2:


You can use a gem called "resque-status".

Using this gem you can easily get a job's status:

status = Resque::Plugins::Status::Hash.get(job_id)

This Resque::Plugins::Status::Hash object returns:

status.pct_complete #=> 0
status.status #=> 'queued'
status.queued? #=> true
status.working? #=> false
status.time #=> Time object        
status.message #=> "Created at ..."

For more details and getting this gem visit: https://github.com/quirkey/resque-status



来源:https://stackoverflow.com/questions/11780191/how-to-alert-user-when-a-resque-job-finishes

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