34

I'm using delayed_job with capistrano and would like a way to start delayed_job on startup of the web application using the 'script/delayed_job start'. This way capistrano can restart it on deploy. If the server gets rebooted then my delayed_jobs should start up with the project.

How can I do this? Should I be looking at doing this in the environment files or as an initializer?

map7
  • 5,096
  • 6
  • 65
  • 128

5 Answers5

34

In combination with the capistrano restart recipe it's quite convenient to use cron to also start the delayed_job daemon at startup using the special @reboot time in a crontab:

@reboot /bin/bash -l -c 'cd /path/to/app && RAILS_ENV=production script/delayed_job restart'

And it's even more convenient together with whenever to configure a scheduled task:

job_type :envcommand, 'cd :path && RAILS_ENV=:environment :task'

every :reboot do
  envcommand 'script/delayed_job restart'
end

Not sure if all implementation of cron actually only run @reboot at system startup but at least Ubuntu seams to only run them at start up and not whenever the cron daemon start or restart. If you pass restart to script/delayed_job it will probably work in either case.

Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57
26

You should create one recipe with the restart command.

namespace :delayed_job do 
    desc "Restart the delayed_job process"
    task :restart, :roles => :app do
        run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job restart"
    end
end

Then you add it to be executed at the end of your deployment.

after "deploy:update_code", "delayed_job:restart"
Damien MATHIEU
  • 31,924
  • 13
  • 86
  • 94
  • I've already got this recipe in my capistrano and it's working. My question was more related to rebooting the server. Currently I've done a work around where on boot I just run a script in my init.d and start the delayed_job daemon. It would be nice to have that as part of my application though. – map7 Nov 17 '09 at 04:47
  • 3
    You could use [god](http://god.rubyforge.org/) to manage the process and boot it automatically when the machine boots. – Damien MATHIEU Nov 17 '09 at 08:24
  • 3
    delayed_job has its own recipes.rb file that has the above task pretty much exactly, plus delayed_job:start and delayed_job:stop tasks. – Ryan McCuaig Jan 12 '10 at 18:33
  • 1
    Hi @Ryan: i noticed this too, but i am stuck as to how to include/require those inside my `deploy.rb`. Just `require `delayed_job'` does not work for me (i am using the version from git). – nathanvda Sep 14 '10 at 11:59
  • 2
    Using rails 3 I found that I needed to do require 'bundler/setup' in deploy.rb before require 'delayed_job/recipes' – opsb Nov 16 '10 at 21:12
20

A bit late to this question, but version 2.1.4 of delayed_job has capistrano recipes:

https://github.com/collectiveidea/delayed_job/wiki/Rails-3-and-Capistrano

It's Damien Mathieu's answer, but already part of the delayed_job gem.

alvin
  • 701
  • 8
  • 10
7

This now works with both Rails 2 and 3:

In deploy.rb, add the following lines:

require "delayed/recipes"
set :rails_env, 'production' #added for delayed_job
before "deploy:restart", "delayed_job:stop"
after  "deploy:restart", "delayed_job:start"
after "deploy:stop",  "delayed_job:stop"
after "deploy:start", "delayed_job:start"
Edward Anderson
  • 13,591
  • 4
  • 52
  • 48
1

It's possible to boot & monitor delayed_job with monit:
How to monitor delayed_job with monit

Community
  • 1
  • 1
Laurynas
  • 3,829
  • 2
  • 32
  • 22