开发者

Simple task scheduling inside of a Rails app

开发者 https://www.devze.com 2022-12-19 01:57 出处:网络
Is there anyway to setup simple task s开发者_Python百科cheduling inside of a rails app? There are pieces of code that I want to run every hour, or every day or every week. I don\'t want to break them

Is there anyway to setup simple task s开发者_Python百科cheduling inside of a rails app? There are pieces of code that I want to run every hour, or every day or every week. I don't want to break them out into separate scripts that I then have to schedule via cron jobs. If I did that, then I'd have to remember to backup the scripts, and if I rebuild a server, I have to go and add back all the cron jobs, it just seems a little bit messy for what I need.

I wish I could just schedule a the jobs somewhere in my rails app and have them magically run when I want them to! Any ideas?


There are gems/plugins for this, like rufus-scheduler or the more popular 'whenever' gem. I do like @douglassellers' solution though, I hadn't heard about it.


I use delayed job, a "database backed asynchronous priority queue" to accomplish something similar. Have a look at http://github.com/tobi/delayed_job

You will still need to have something that kicks off 'rake jobs:work' but it is better than having lots of different cron jobs. Notice in the documentation the :run_at attribute – that looks like it might solve the "schedule a the jobs somewhere in my rails app and have them magically run when I want them to" problem.


You can use something like Rails Cron (gem install rails_cron) to do this, which spins off another rails process dedicated to doing scheduled background tasks. I would recommend against doing it this way, as the extra rails instance will have to be monitored and stopped and started along with the rest of your app and, in my experience, is very prone to falling down.

A better solution is to just use Craken, http://github.com/latimes/craken, to manage crontabs through yaml files in your rails app. Each scheduled task gets defined in a yaml file and then on deployment Craken decides whether or not the System Level CronTab file needs to be updated or not. It plugs right into your capistrano deploy file and is reasonably turn-key. This gives the advantage of still managing your scheduled tasks in your rails app while actually executing them with the OS level cron.

0

精彩评论

暂无评论...
验证码 换一张
取 消