rails periodic task
Asked Answered
K

7

13

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).

Katinka answered 12/11, 2012 at 23:7 Comment(0)
D
3

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.

Dumpcart answered 13/11, 2012 at 3:27 Comment(1)
I ended up just creating a simple tool that forks when the rails app starts and periodically runs a list of tasks. Each loop through it checks to see if the parent is still alive and quits if not.Katinka
S
13

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.

Shaky answered 13/11, 2012 at 1:10 Comment(2)
You have to be careful with Whenever, since it has to start up the Rail instance every time for a task. There is a delay between when the task is scheduled to start and when Rail finishes initializing.Decline
I think whenever's smallest time unit is minutes, you can't use 15.seconds with it.Unmannered
D
3

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.

Dumpcart answered 13/11, 2012 at 3:27 Comment(1)
I ended up just creating a simple tool that forks when the rails app starts and periodically runs a list of tasks. Each loop through it checks to see if the parent is still alive and quits if not.Katinka
S
2

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.

Sporogonium answered 13/11, 2012 at 4:1 Comment(0)
N
2

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.

Neumann answered 17/5, 2018 at 16:54 Comment(0)
H
1

Throwing out a solution just because it looks somewhat elegant and answers the question without any extra gems. In my scenario I wanted to run some code, but only after all my Sidekiq workers were done doing their thing.

First I defined a method to check if any workers were working...

def workers_working?
  workers = Sidekiq::Workers.new.map do |_process_id, _thread_id, work|
    work
  end
  workers.size > 0
end

Then we just call the method with a loop which sleeps between calls.

sleep 5 while workers_working?
Hawks answered 15/9, 2021 at 20:22 Comment(0)
R
0

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

Rockling answered 12/11, 2012 at 23:28 Comment(2)
He said he wanted it as part of the application and only be alive when his application is running. Seems like overkill?Longways
The kicker is that it would need to run in a new process, so the web process can continue on serving web requests. So you could build your own forking server... or just reuse existing libraries.Rockling
M
0

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  
Mccreary answered 11/5, 2017 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.