问题
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...
回答1:
Just had to do a lot of hunting and experimenting but I think I figured out how to accomplish this. Create separate config files for each role/server that you need a crontab on. Then only define the desired tasks in each config file. Finally, something like this in your deploy.rb:
namespace :deploy do
desc "Update the db crontab file"
task :update_db_crontab, :roles => :db do
run "cd #{current_path} && whenever -fconfig/schedule_db.rb --set environment=#{rails_env} --update-crontab #{application}_db"
end
desc "Update the app crontab file"
task :update_app_crontab, :roles => :app do
run "cd #{current_path} && whenever -fconfig/schedule_app.rb --set environment=#{rails_env} --update-crontab #{application}_app"
end
end
after "deploy:symlink", "deploy:update_db_crontab"
after "deploy:symlink", "deploy:update_app_crontab"
回答2:
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'
来源:https://stackoverflow.com/questions/3550770/finding-out-deployment-machine-you-are-on-in-code-rails