Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Ruby run some task every 10 minutes?

I would like to do a cron job every 10 minutes, but my system only does 1 hour. So I'm looking for a method to do this. I've seen Timer and sleep but I'm not sure how to do this or even better yet a resource for achieving this.

like image 687
thenengah Avatar asked Aug 21 '10 17:08

thenengah


2 Answers

Take a look at http://rufus.rubyforge.org/rufus-scheduler/

rufus-scheduler is a Ruby gem for scheduling pieces of code (jobs). It understands running a job AT a certain time, IN a certain time, EVERY x time or simply via a CRON statement. rufus-scheduler is no replacement for cron/at since it runs inside of Ruby.

like image 126
grm Avatar answered Nov 15 '22 11:11

grm


To do this reliably, invest in a VPS and create the 10-minute cron job as desired. Trying to emulate cron all on your own is very likely to fail in unforeseen ways.

Creating a sleeping process is not the way to go about this; if your server doesn't give you the freedom to make your own cron as you like it, you probably can't create your own background process for this sort of thing, either. You might be able to, on each request, take a look and see how many of the jobs need done (if it was 25 minutes since last request, you might have to do two), and go back and do them retroactively.

But, seriously. You need your own server to do this dependably.

like image 43
Matchu Avatar answered Nov 15 '22 12:11

Matchu