views:

30

answers:

1

Hi folks! My rails app is deployed to several machines. I need each machine to run different cron jobs (it will be a disaster if they all run the job). How do i tell my script which machine it is currently on?

I am using the whenever gem, and i am thinking of adding the condition in the schedule.rb

Example:

My deploy/production.rb

role :memcache, "123.compute-1.amazonaws.com"
role :web,      "456.compute-1.amazonaws.com"
role :db,       "789.amazonaws.com"
role :misc,     "789.amazonaws.com"

What I need to do:

if machine is db, email report every day on db capacity

if machine is xxx, do xxx...
A: 

You can set up capistrano tasks to only run for specific roles (I assume you are using capistrano from the role calls).

For example:

task :db_cron_jobs, :only => :db do
  # Use deprec recipes to update crontab
  deprec2.update_user_crontab('cron_user', @daily 'send_out_emails_command')
end
after 'deploy:setup', 'db_cron_jobs' 

task :web_cron_jobs, :only => :web do
  # Other automated tasks
end
after 'deploy:setup', 'web_cron_jobs'
Shadwell
I am using the whenever gem, and i am thinking of adding the condition in the schedule.rbYour solution is very interesting tho, but i was thinking that could be encapsulated in the same file where i am specifying the jobs
ming yeow
Ah, ok, guessed wrong! Haven't used whenever I'm afraid so no bright ideas there.
Shadwell
host = Socket.gethostname gives us the hostname. I am going to try this - define the hostnames in initialization files, instead of only in production.rb. That way, i can use these in schedule.rb
ming yeow
Will update answer if it works. =)
ming yeow