问题
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:
- 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.)
- Send them an email at the conclusion of the job's run. Just use the standard ActionMailer stuff for this.
- 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