开发者

Easiest way to run a batch job every 5 minutes in Rails 3 with Heroku?

开发者 https://www.devze.com 2023-03-17 14:44 出处:网络
Delayed Job doesn\'t seem to suit开发者_Python百科 me as it, well, delays the job. I want to kick off a job every 5 minutes on my Rails app. Is this possible on Heroku?

Delayed Job doesn't seem to suit开发者_Python百科 me as it, well, delays the job. I want to kick off a job every 5 minutes on my Rails app. Is this possible on Heroku?

Thanks.


I suggest a combination of their Free Daily Cron and the above mentioned DelayedJob scheduling. Once a day, kick off all the delayed job tasks (if you're doing 5 minute intervals, you'd need 288 jobs) to get through the day, and space them accordingly.

Edit: You don't actually need the above mentioned module. From this blog post, you can do the following

Delayed::Job.enqueue PollTwitter.new(), 0, 1.minutes.from_now.getutc

Which sets a run_at flag in DelayedJob. Apparently, that's the time at which the job will be run.


In this blog post you can find a module to add some scheduling on DelayedJob.

module Jobs
  module ScheduledJob
    def self.included(base)
      base.extend(ClassMethods)
    end

    def perform_with_schedule
      Delayed::Job.enqueue self, 0, self.class.schedule.from_now.getutc
      perform_without_schedule
    end

    module ClassMethods
      def method_added(name)
        if name.to_s == "perform" && !@redefined
          @redefined = true
          alias_method_chain :perform, :schedule
        end
      end

      def schedule
        @schedule
      end

      def run_every(time)
        @schedule = time
      end  
    end

  end
end

I am using this on one of my projects, works ok!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号