Initialize the Delayed Jobs Gem by Starting the Workers on Application Start

Initialize the Delayed Jobs gem by starting the workers on application start

Try using this code:

system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} stop"
system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} -n 2 start"

Testing it in the console produced this output which indicates it should work:

Loading development environment (Rails 3.0.9)
ruby-1.9.2-p290 :001 > Rails.root.join('script','delayed_job')
=> #<Pathname:/home/devin/testsoapp/script/delayed_job>
ruby-1.9.2-p290 :002 > "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} -n 2 start"
=> "RAILS_ENV=production /home/devin/testsoapp/script/delayed_job -n 2 start"
ruby-1.9.2-p290 :003 >

Setting up the Delay Jobs gem

To start your workers on application start I would just call the proper command from an initalizer. The code to do this would look like:

system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} stop"
system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} -n 2 start"

The path might be a little off and there most likely is a cleaner way to do it but I dont know of anything off of the top of my head.

Starting delayed_job at startup

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"


Related Topics



Leave a reply



Submit