0

I am a newbie to Chef and I want to restart resque workers on a cloud node after every deployment through my recipe. There is a Rake task which resque provides to start the workers

QUEUE='*' bundle exec rake environment resque:work >>/log/resque.log 2>&1 &

to stop it, I grep the resque process and manually kill it.

I was not able to find any good examples on how to run rake tasks in Chef services. Can someone help me with a sample service which will do what I intend to?

Infant Dev
  • 1,659
  • 8
  • 24
  • 48

1 Answers1

1

Create an execute resource with action :nothing that will be used to restart the workers (adapt as necessary):

execute "restart_resque_workers" do
  command "pkill resque && QUEUE='*' bundle exec rake environment resque:work >>/log/resque.log 2>&1 &"
  cwd "/path/to/app"
  action :nothing
end

Then, on the deployment resource, add:

application "app" do
  ...
  notifies :run, "execute[restart_resque_workers]"
end

Ideally, the stop, start and restart mechanism would be handled by a proper service, but in any case the overall pattern is the same.

The notifies attribute will only kick in if the application resource is modified (usually that means a new deployment).

More information on notifications in the Chef docs.


A poor man's service could go like this:

service 'resque_workers' do
  start = "QUEUE='*' bundle exec rake environment resque:work >>/log/resque.log 2>&1"
  stop = "pkill resque"
  start_command start
  stop_command stop
  restart_command "#{stop}; #{start}"
  supports [ :start, :stop, :restart ]
end

Then use notifies :restart, "service[resque_workers]" in the application resource.

cassianoleal
  • 2,556
  • 19
  • 22
  • Thanks for writing the service out.. :) – Infant Dev Feb 28 '14 at 07:37
  • NP! I had just done something similar for one of my cookbooks, so I put it here. :) Note that this is not an actual service that you can manage on the OS using `service rescue_workers start/stop`. If for any reason you need to manage it on the box itself, you'll still need to run the commands yourself. – cassianoleal Feb 28 '14 at 11:57