March 26, 2015

Crash or… Monit!

By Andreas Fast

As an application grows it starts demanding more and more work. At some point of the ongoing project, processing a task in the background becomes a necessity. One of the most popular tools to accomplish this in Ruby is Sidekiq.

Now, did it ever happen to you that you get it all up and running on the server and the next thing you know is the server is down and you get a colorful variety of error reports yelling that stuff is not working?

Meet Monit, a tool that’ll let you get ahead of trouble. It automatically restarts programs when they crash and can send email reports on failures!

In case you want to try it out –and make yourself a favour– here’s some aid on installing, configuring and get it up and running.

Install Monit

Depending on your package manager this may differ:

 

Configure monit

  1. Edit /etc/monit.conf  or  /etc/monitrc
  1. Uncomment (or add) these lines:

 

Add Sidekiq entry

Create the file /etc/monit.d/sidekiq.monitrc  with:

 

Start Monit

Let’s not forget to start Monit:  /etc/init.d/monit start

Now a sudo monit status should show the sidekiq_application_production0  entry.

If you use capistrano you will notice the current and shared directories. If you don’t use capistrano just make sure the PID is stored in some place where the path doesn’t change to let Monit find it.

This configuration will also be useful if you use capistrano-sidekiq, just make sure the check process name in the config matches what is used during deploy.

This configuration includes a memory protection that will restart Sidekiq before it eats up all the memory in your server. Yes, I’ve got a server down more than once due to this issue 🙁

 

Configure email server

To receive Monit’s status via email just add the following to /etc/monit.conf  or  /etc/monitrc

Add the email server, e.g.: for gmail’s SMTP add in the following line to the config file

set mailserver smtp.gmail.com port 587 username "notifier@gmail.com" password "********" using tlsv1 with timeout 30 seconds

 

Subscribe

To subscribe to all emails, including non-error emails, add the following line to the config file:

set alert my_app_notifier@gmail.com

 

If everything is set up correctly, executing sudo monit reload  should trigger an email.

If you want to get emails for specific events or have more questions regarding Monit’s config check out http://mmonit.com/monit/documentation/monit.html.

A word of warning, the doc is huge. If you are looking for something specific like email, alert or whatever you need, just search it.

 

That’s pretty much it. Of course Monit doesn’t take care of starting the processes if the server restarts. You’ll have to look for another tool to do that.

Reposted from http://andisxp.blogspot.com/2015/03/keep-sidekiq-running-using-monit.html