Invoke delayed_job capistrano tasks only on specific servers
I have a dedicated server for delayed_job tasks. I want开发者_开发知识库 to start, stop, and restart delayed_job workers on only this server. I am using the capistrano recipes provided by delayed_job.
When I only had 1 server, this was my config:
before "deploy:restart", "delayed_job:stop"
after "deploy:restart", "delayed_job:start"
after "deploy:stop", "delayed_job:stop"
after "deploy:start", "delayed_job:start"
Now I want to have those hooks only apply to a separate delayed_job server (role :delayed_job <ip address>
). Is this possible to do elegantly? Do I have to wrap each delayed_job tasks in a meta task? Or write my own tasks and not use the ones provided by delayed job?
When you define a task in Capistrano you can restrict the execution of the task to specific role(s). The way you do this is by passing the :role
option.
It seems the default delayed_job Capistrano recipe does this.
desc "Stop the delayed_job process"
task :stop, :roles => lambda { roles } do
run "cd #{current_path};#{rails_env} script/delayed_job stop"
end
According to the source code, the task fetches the list of roles from the :delayed_job_server_role
configuration variable.
Back to your problem, to narrow the execution of the tasks to a specific group of servers, define a new role (for example worker) in your deploy.rb
role :worker, "192.168.1.1" # Assign the IP of your machine
Then set the :delayed_job_server_role
to that name
set :delayed_job_server_role, :worker
That's all. Now the tasks will be executed, but only to the servers listed in the :worker
role.
精彩评论