Monitor Delayed Job in rails

Install monit:

sudo apt-get install monit

Edit the /etc/default/monit file and enable the start up flag, which ensures that the configuration is done so that monit can start.

# vim /etc/default/monit
startup=1

Configure monit:

sudo vim /etc/monit/monitrc

Set Monit to check services every two minutes (120 seconds), and log to /var/log/daemon.log

set daemon 120
set logfile syslog facility log_daemon

Setup email alerts:

set mailserver localhost
set mail-format { from: monit@myserver.domain.com }
set alert sysadmin@domain.com

Switch on the HTTP interface, allow access from anywhere, and require a username and password. Make it a decent password, because the HTTP interface allows you to stop and start services.

set httpd port 2812
    use address myserver.domain.com
    allow 0.0.0.0/0.0.0.0
    allow myusername:mypassword

/etc/init.d/delayed_job:

#! /bin/sh
set_path="cd /var/www/app"

case "$1" in
    start)
    echo -n "Starting delayed_job: "
    su - root -c "$set_path; RAILS_ENV=production script/delayed_job start" >> /var/log/delayed_job.log 2>&1
    echo "done."
    ;;
    stop)
    echo -n "Stopping delayed_job: "
    su - root -c "$set_path; RAILS_ENV=production script/delayed_job stop" >> /var/log/delayed_job.log 2>&1
    echo "done."
    ;;
    *)
    echo "Usage: $N {start|stop}" >&2
    exit 1
    ;;
esac

exit 0
sudo chmod +x /etc/init.d/delayed_job

monit delayed_job configuration:

check process delayed_job with pidfile /var/www/app/tmp/pids/delayed_job.pid
    stop program = "/etc/init.d/delayed_job stop"
    start program = "/etc/init.d/delayed_job start"
    if totalmem > 100.0 MB for 3 cycles then restart
    if cpu usage > 95% for 3 cycles then restart

Start monit, and query it. You need the HTTP interface to use the ‘status’ command.

sudo /etc/init.d/monit start
sudo monit status

Suppose you have 2 workers, you’ll monitor all of them with the following:

check process delayed_job.0
   with pidfile /path/to/shared/pids/delayed_job.0.pid
   start program = "/bin/su -c '/usr/bin/env RAILS_ENV=production /path/to/current/script/delayed_job -n 5 start' - user"
   stop program = "/bin/su -c '/usr/bin/env RAILS_ENV=production /path/to/current/script/delayed_job stop' - user"

check process delayed_job.1
   with pidfile /path/to/shared/pids/delayed_job.1.pid
   start program = "/bin/su -c '/usr/bin/env RAILS_ENV=production /path/to/current/script/delayed_job -n 5 start' - user"
   stop program = "/bin/su -c '/usr/bin/env RAILS_ENV=production /path/to/current/script/delayed_job stop' - user"

http://www.darkcoding.net/software/setting-up-monit-on-ubuntu/
http://www.thegeekstuff.com/2010/11/monit-install-config/
http://www.funonrails.com/2011/03/monitor-delayedjob-in-rails.html
http://stackoverflow.com/questions/6071970/monitoring-multiple-delayed-job-workers-with-monit