Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting delayed_job at startup

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?

like image 994
map7 Avatar asked Nov 12 '09 01:11

map7


People also ask

How do you start a delayed job?

Delayed Job uses your database as a queue to process background jobs. If your app has a high database load using DelayedJob may not be a good background queueing library for your app. To get started using Delayed Job you need to configure your application and then run a worker process in your app.

How does delayed job work?

Delayed::Job works by serializing a Ruby object as YAML and storing it in a database table so that a worker in a different process can retrieve, deserialize, and perform the requested work. Some operations that we use Delayed::Job for are longer-running data processing, sending emails, and updating search indicies.

How do I know if my job is running late?

The most simple way to check whether delayed_job is running or not, is to check at locked_by field. This field will contain the worker or process locking/processing the job. Running Delayed::Job. where('locked_by is not null') will give you some results, if there are jobs running.


1 Answers

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.

like image 101
Mattias Wadman Avatar answered Oct 10 '22 17:10

Mattias Wadman