rails periodic task

前端 未结 6 1408
忘了有多久
忘了有多久 2021-02-13 15:19

I have a ruby on rails app in which I\'m trying to find a way to run some code every few seconds.

I\'ve found lots of info and ideas using cron, or cron-like implementat

相关标签:
6条回答
  • 2021-02-13 15:42

    Use something like delayed job, and requeue it every so often?

    0 讨论(0)
  • 2021-02-13 15:43

    Use thin or other server which uses eventmachine, then just use timers that are part of eventmachine. Example: in config/application.rb

    EM.add_periodic_timer(2) do
      do_this_every_2_sec
    end  
    
    0 讨论(0)
  • 2021-02-13 15:44

    Generally speaking, there's no built in way that I know of to create a periodic task within the application. Rails is built on Rack and it expects to receive http requests, do something, and then return. So you just have to manage the periodic task externally yourself.

    I think given the frequency that you need to run the task, a decent solution could be just to write yourself a simple rake task that loops forever, and to kick it off at the same time that you start your application, using something like Foreman. Foreman is often used like this to manage starting up/shutting down background workers along with their apps. On production, you may want to use something else to manage the processes, like Monit.

    0 讨论(0)
  • 2021-02-13 15:45

    I'd suggest the whenever gem https://github.com/javan/whenever

    It allows you to specify a schedule like:

    every 15.minutes do
      MyClass.do_stuff
    end
    

    There's no scheduling cron jobs or monkeying with external services.

    0 讨论(0)
  • 2021-02-13 15:52

    You can either write you own method, something like

    class MyWorker
      def self.work
        #do you work
        sleep 15
      end
    end
    

    run it with rails runner MyWorker.work There will be a separate process running in the background

    Or you can use something like Resque, but that's a different approach. It works like that: something adds a task to the queue, meanwhile a worker is fetching whatever job it is in the queue, and tries to finish it.

    So that depends on your own need.

    0 讨论(0)
  • 2021-02-13 15:53

    I know it is an old question. But maybe for someone this answer could be helpful. There is a gem called crono.

    Crono is a time-based background job scheduler daemon (just like Cron) for Ruby on Rails.

    Crono is pure Ruby. It doesn't use Unix Cron and other platform-dependent things. So you can use it on all platforms supported by Ruby. It persists job states to your database using Active Record. You have full control of jobs performing process. It's Ruby, so you can understand and modify it to fit your needs.

    The awesome thing with crono is that its code is self explained. In order to do a task periodically you can just do:

    Crono.perform(YourJob).every 2.days
    

    Maybe you can also do:

    Crono.perform(YourJob).every 30.seconds
    

    Anyway you really can do a lot of things. Another example could be:

    Crono.perform(TestJob).every 1.week, on: :monday, at: "15:30"
    

    I suggest this gem instead of whenever because whenever uses Unix Cron table which not always is available.

    0 讨论(0)
提交回复
热议问题