问题
I want to run a task every minute on Heroku to check if conditions are met to time-out certain user tasks. I can only run a Heroku cron job every hour, so what's the best way to set up a timed task like this. I am using Rails 3.1 on Heroku.
回答1:
You could use delayed_job with a self-restarting job with a :run_at
. Something sort of like this:
class YourJob
def do_interesting_things
#... Do what needs to be done.
self.delay(:run_at => 1.minute.from_now).do_interesting_things
end
def self.start_me_up
new.do_interesting_things
end
end
And then somewhere during your application's initialization:
YourJob.start_me_up
回答2:
As of today, you can the scheduling gem, clockwork. Heroku officially supports this and you can find their documentation here.
来源:https://stackoverflow.com/questions/7315998/scheduling-a-job-every-minute-rails-3-1-on-heroku