Rake + Cron = Goodness
Our new site does some crawler tasks that run periodically throughout the day. While in development I just kicked these off using an http request from my browser, but moving to something resembling alpha we needed to get them on a schedule. Since we're running on a Joyent OpenSolaris Accelerator we have access to old-school cron goodness for scheduling, but we needed access to our Rails application environment - simply creating a cron task to execute a .rb file wouldn't cut it.
Fortunately rake is there to save the day.
Step 1 - Create your rake file
Touch a new file called yourname.rake and put it in /yourapp/lib/tasks. I called mine 'update.rake'. Add the following:
namespace :update do
task(:say_hello => :environment) do
puts 'Hello, World!'
end
end
Putting the file in lib/tasks and using :environment makes the script seemingly magically aware of your rails environment, so you can use ActiveRecord and everything else in your application.
Step 2 - Execute and Test
Next navigate to your app's home directory and run using your environment:
rake RAILS_ENV=development update:say_hello
you should see...
Hello, World!
Step 3 - Schedule
Next set up your cron job as you would normally. With Joyent the "Schedule Cron Jobs" section of Webmin makes it a snap, but there are as many ways to use cron as decades since it was created. Set up the following command using the path to rake on your server (use "which rake" if you aren't sure) and your rails app.
cd /your/rails/app && /opt/local/bin/rake RAILS_ENV=development update:say_hello
Now you should be able to set up application-aware rails code which executes whenever you please.
Extra Credit
You can make your rake script more object-oriented by putting real Ruby methods in your rake script. Check it out!
namespace :update do
task(:say_hello => :environment) do
delegate_hello
end
def delegate_hello
puts 'Hello World!'
end
end
Fortunately rake is there to save the day.
Step 1 - Create your rake file
Touch a new file called yourname.rake and put it in /yourapp/lib/tasks. I called mine 'update.rake'. Add the following:
namespace :update do
task(:say_hello => :environment) do
puts 'Hello, World!'
end
end
Putting the file in lib/tasks and using :environment makes the script seemingly magically aware of your rails environment, so you can use ActiveRecord and everything else in your application.
Step 2 - Execute and Test
Next navigate to your app's home directory and run using your environment:
rake RAILS_ENV=development update:say_hello
you should see...
Hello, World!
Step 3 - Schedule
Next set up your cron job as you would normally. With Joyent the "Schedule Cron Jobs" section of Webmin makes it a snap, but there are as many ways to use cron as decades since it was created. Set up the following command using the path to rake on your server (use "which rake" if you aren't sure) and your rails app.
cd /your/rails/app && /opt/local/bin/rake RAILS_ENV=development update:say_hello
Now you should be able to set up application-aware rails code which executes whenever you please.
Extra Credit
You can make your rake script more object-oriented by putting real Ruby methods in your rake script. Check it out!
namespace :update do
task(:say_hello => :environment) do
delegate_hello
end
def delegate_hello
puts 'Hello World!'
end
end

1 Comments:
Thanks for this!
Post a Comment
<< Home