scheduling a job every minute Rails 3.1 on Heroku

前端 未结 3 553
南旧
南旧 2020-12-31 18:14

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

相关标签:
3条回答
  • 2020-12-31 18:24

    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
    
    0 讨论(0)
  • 2020-12-31 18:36

    As of today, you can the scheduling gem, clockwork. Heroku officially supports this and you can find their documentation here.

    0 讨论(0)
  • 2020-12-31 18:39

    Clockwork is the way to go but I will suggest another workaround.

    Use scheduler (every 10min) + delayed jobs to execute the task every minute. This seems a little bit more reliable solution than the one without scheduler.

    MyJob.run!
    
    (1..8).each |i|
       MyJob.delay(run_at: i.minutes.from_now).run! # create 8 more jobs (each start 1 minute later)
    end
    
    0 讨论(0)
提交回复
热议问题