Search code examples
rubyruby-on-rails-3rakemonitoringmonit

How can I create a monit process for a Ruby program?


I have these rake tasks that will occasionally fail. I want to use monit to monitor them and to restart them if necessary.

I have read the other ruby/monit threads on StackOverflow. My case is different in that these programs require my Rails environment in order to work. That's why I have them as rake tasks now.

Here is one of the tasks I need to monitor, in it's entirety:

task(process_updates: :environment) do
  `echo "#{Process.pid}" > #{Rails.root}/log/process_alerts.pid`
  `echo "#{Process.ppid}" > #{Rails.root}/log/process_alerts.ppid`
  SynchronizationService::process_alerts
end

My question is, do I leave this as a rake task, since SynchronizationService::process_alerts requires the Rails environment to work? Or is there some other wrapper I should invoke and then just run some *.rb file?


Solution

  • Monit can check for running pid, since you're creating pid when you run task you can create a monit config which should look something like this:

    check process alerts with pidfile RAILSROOT/log/process_alerts.pid
      start program = "cd PATH_TO_APP; rake YOURTASK" with timeout 120 seconds
      alert your@mail.com on { nonexist, timeout }
    

    Of course RAILSROOT, PATH_TO_APP, YOURTASK should correspond to your paths/rake task.

    Monit then will check for running process in system using the pidfile value and will start the process using start program command if it can't find running process.