rails periodic task

前端 未结 6 1405
忘了有多久
忘了有多久 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: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.

提交回复
热议问题