Monitoring Rails Applications Using The God Gem
I have been playing around with the God Gem recently, since there have been quite a few times that I have had to restart a Rails application that I thought was running, but wasn't. I'm not the greatest server administrator in the world, but going through the process of deploying and maintaining various Rails applications, some large and some very small, has led me to look for solutions that will help with making sure everything is up and running when it is supposed to be.
First things, first, what is God? God is a Ruby gem that can be installed on a web server that was designed to "watch" over particular processes and respond to changes in their state. This could be useful for many different things, but in my case it comes in handy for monitoring mongrel instances. since the documentation that is provided is pretty good, I figured a small overview of my experience getting God up and running might help someone out there.
Getting Started
One of the things that I like about God, aside from it being a Ruby gem, is that the configuration file is a Ruby file, which keeps everything familiar to someone like me. The first thing to do is install the god gem using the command:
sudo gem install god
Easy enough. Once the installation is complete, you can start up God by running the god command, passing to it the config file that you would like it to read:
sudo god -c /example/god/config.god
Creating the configuration file is explained in detail in the documentation, and they even go into some pretty advanced stuff. In my case, I tend to keep things simple by just monitoring the mongrel instances that I am want to keep running, and also doing the CPU usage and memory checks that are outlined in the configuration example. I have found that for most of the smaller Rails applications that we are monitoring, that is enough.
Stopping the God monitoring is also simple, although you need to pass the unique identifier of each watch process that you want to stop. For example, if I had started a God watch process called my-god-watcher I would stop it by typing in:
sudo god stop my-god-watcher
Keep in mind that based on the example in the documentation, this will only stop one process that is watching one port. I would need to loop through all the ports that are being watched (in the example they watch 3 ports) to stop them. But that usually isn't an issue.
Multiple Applications
Another reason that I like the God gem is that it makes it easy to watch multiple rails applications. While in most cases a larger Rails application will be the only application running on a server or group of servers, there are cases where you would want to monitor a number of smaller Rails applications. Personally, I have many Rails applications running on a couple of servers, and restarting them all by hand when something goes wrong takes time, and is annoying. This is where it is nice to have multiple configuration files.
Let's say that your like me, and you've got a few Rails applications on a server already. You want to watch those applications to make sure that they are running and restarted when needed. However, you also want to be able to put up new applications and monitor them as well when they are launched. I do this by using a master config file that simply loads in other config files when it is started. Use the following command inside the master config file to load in other files:
God.load "/example/god/configs/*.god"
That will make sure that God loads in all the config files from that directory before starting to monitor. So when I want to deploy a new application for production, I create a new God config file that will monitor the ports that our mongrels should be running, and save it to our config directory. This will ensure that the next time we start up God it will begin to monitor our new application, as well as the older ones that were already there.
Finally, we need to load that new config file into the already running instance of God, since restarting it would cause an interruption in monitoring the other applications. To do this we simply type:
sudo god load /example/god/configs/new_app.god
And we are good to go. My personal experience with God is that it has been extremely easy to set up, and works very well out of the box. I expect that in some cases, particularly with larger applications, I will be learning more about advanced monitoring and getting into the state transitions. For now, I'm just glad it works. Now when I need to restart my server, or for whatever reason the processes are down, I simply need to start up the God monitoring and it will take care of starting everything up when it needs to be.
Also, I recommend taking a look at how to set up email notifications for particular events. It is extremely enlightening at first to see when things happen, and how often.
This post is filed under Developers' Corner and has the following keyword tags: rails, ruby, god, gem.