rails periodic task

浪尽此生 提交于 2019-12-21 03:38:14

问题


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 implementations, but these are only accurate down to the minute, and/or require external tools. I want to kick the task off every 15 seconds or so, and I want it to be entirely self contained within the application (if the app stops, the tasks stop, and no external setup).

This is being used for background generation of cache data. Every few seconds, the task will assemble some data, and then store it in a cache which gets used by all the client requests. The task is pretty slow, so it needs to run in the background and not block client requests.

I'm fairly new to ruby, but have a strong perl background, and the way I'd solve this there would be to create an interval timer & handler which forks, runs the code, and then exits when done.
It might be even nicer to just simulate a client request and have the rails controller fork itself. This way I could kick off the task by hitting the URI for it (though since the task will be running every few seconds, I doubt I'll ever need to, but might have future use). Though it would be trivial to just have the controller call whatever method is being called by the periodic task scheduler (once I have one).


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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




回答6:


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  


来源:https://stackoverflow.com/questions/13353105/rails-periodic-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!