Example configuration with God – Monitor nodejs

God is a very powerful process monitoring library written in Ruby. This tutorial describes how to setup a basic monitoring configuration file for a NodeJS application, telling God to restart the NodeJS app upon the process being killed (for what ever reason).

To get started, create a god/ directory within your Node app.

mkdir /var/www/sites/nodeapp/god

In this directory, create a god configuration file

vi /var/www/sites/nodeapp/god/nodeapp.god

Update the global God configuration file (Refer to previous tutorial on installing God on Ubuntu 12.04) with the location of your Node app config.

sudo vi /etc/god.conf
God.load '/var/www/sites/nodeapp/god/*.god'

Great, when you reload God it will start using the configuration, but first we must write the configuration file. Open your nodeapp.god config file for editing. We will start off by creating a basic configuration file that will allow you to:

  • Start node
  • Stop node
  • Restart node

APP_SERVICES_DIR = '/var/www/sites/nodeapp'
God.watch do |w|
  w.name     = APP_SERVICES_NAME
  w.log      = APP_SERVICES_GOD_DIR+'/'+APP_SERVICES_NAME+'.log'
  w.start   = 'node '+PROXY_SERVICES_DIR+'/bin/nodeapp.js'
  w.stop    = 'killall node;'
  w.restart = 'killall node; node '+PROXY_SERVICES_DIR+'/bin/nodeapp.js'

Let’s break it down. Firstly, we tell god to initiate a watch on a specific process. We name (`w.name`) the process allowing us to reference it, set an interval to perform “up” checks (`w.interval`) and define the location to log output from the nodeapp (`w.log`). Pretty basic stuff, but this allows us to check errors (through the log) and reference the app through the god service.

Next, we define what god needs to do for starting, stopping and restarting node.

`w.start` tells God to start the app through node when the following command is run:

sudo god start nodeapp

`w.stop` tells God to stop the app through node when the following command is run:

sudo god stop nodeapp

And `w.restart` tells God to kill any existing node processes, and start your node app again. It’s worth bearing in mind that this configuration file is written based on a single node process running on your server. Multiple node apps would require modification to kill only the process God is monitoring. I may write a tutorial on this at a later date.

sudo god restart nodeapp

There we have it. Our basic configuration file is complete. Now restart god to reload the configuration file.

sudo /etc/init.d/god restart

By default, God will not start the nodeapp so you need to call start:

sudo god start nodeapp.

Check your nodeapp is running:

ps aux | grep -i 'node'

If node is not running you can check /var/log/god.log for errors as well as the error log for your app. If the nodeapp is running then fantastic, let’s kill off the node process:

sudo killall node

Give it a second and check to see if node is running again:

ps aux | grep -i 'node'

Running? Great! You now have a basic God process monitoring configuration in place for your node app.

Start NodeJS app when God starts

As previously mentioned, the above configuration file will not start your NodeJS app when God is started. This results in having to call the god process start command. However, 9/10 developers will want God to manage the starting of the process for them. So, the following describes how to set this up:

Before the end of your `watch` block, add the following:

w.transition(:init, { true => :up, false => :start }) do |on|
  on.condition(:process_running) do |c|
    c.running = true

The above code is simple, on initialisation of God, check the nodeapp process is running (:process_running), set running to true. If the process is running, the condition will return true to the transition, the transition in turn will set the state of the process to :up. If the condition returns false (the process is not running) then the condition passes false to the transition, thus telling God to :start the process when it initiates.

Yes, it is that simple. But then most libraries written well in Ruby are just that, simple, easy and somewhat powerful.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>