Search code examples
chef-infraresquechef-recipecookbook

How to Start resque workers through Chef cookbook?


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?


Solution

  • 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.