1

I'm deploying a node web application as an upstart service using grunt and monitoring it using monit. However:

  1. My upstart and monit configuration duplicate each other a little bit
  2. Upstart doesn't do variable expansion inside env stanzas
  3. I can't find a way to configure monit dynamically (ala upstart .override files)

My question

This means I'm looking for a grunt plugin or other tool that I can use to generate the uptstart .conf and monit conf.d/ files. Can you please help me find one (or suggest a better way of robustly running my node web app)?

A rather crude solution?

To be honest an underscore template of the upstart and monit files would probably be sufficient, and that's what I'll wrap up into a grunt plugin if there isn't a ready-made solution, but this feels like a problem that other people must have run into as well so I imagine there's a solution out there, I just can't find it.

Detail

A bit more detail to illustrate the three problems. My upstart conf file looks like this:

setuid node
setgid node

# ...

script
    mkdir -p /home/node/.my-app
    echo $$ > /home/node/.my-app/upstart.pid
    /usr/local/bin/node /var/node/my-app/server.js >> /var/node/my-app/logs/console.log 2>&1
end script

# ...

And my monit configuration looks like this:

check process node with pidfile /home/node/.my-app/upstart.pid
    start program = "/sbin/start my-app" with timeout 60 seconds
    stop program  = "/sbin/stop my-app"
    if failed host localhost port 17394 protocol http 
        and request "/monit.html"
        then restart
    if 3 restarts within 5 cycles then timeout

As you can see, the PID file path and config directory is duplicated across the two (problem 1) and I'd love to parameterise the host, port and request URL in the monit file (problem 3).

For problem 2 (no variable expansion in upstart's env stanza, bug report here) there are a couple of workarounds that work for variables used inside *script blocks, which are interpreted as bash scripts, but they don't seem to work in the conf file itself. I think this makes it impossible to specify the user id the app should run as in a configuration file?

Those techniques I just mentioned:

  • Method 1: Don't use env - echo the variables in the pre-script to a file and then source it later
  • Method 2: Duplicate the variable expansion in all the script bodies where it is needed
  • Method 3: Store the variables in a file and cat them in using command substitution
Community
  • 1
  • 1
cfogelberg
  • 1,468
  • 19
  • 26

1 Answers1

3

...or suggest a better way of robustly running my node web app

Use PM2 by Unitech instead.

I run all my node apps using PM2. It works perfectly and is easy to configure. It has built-in functionality for autogenerating of startup script. It gives you logging, monitoring and easy maintenance.

Here is a good article showing off the highlights.

Install

npm install -g pm2

Start app

pm2 start app.js

Show running apps

pm2 list

Make a startup script

pm2 startup [ubuntu|centos|systemd]

More details in the readme on their github page

aludvigsen
  • 5,893
  • 3
  • 26
  • 37
  • Thanks for suggesting pm_2 @display_name. One of the reasons I particularly like monit is that I can monitor via actual connection attempts as well as via system and process parameters like memory used. Does pm2 offer this functionality as well? – cfogelberg Apr 23 '14 at 11:01
  • Yes it does! :) It even has its one API to get all the information and [display it in a dashboard](https://github.com/Unitech/pm2#monitoring-dashboard) if you want! I really recommend giving it a try. @cfogelberg – aludvigsen Apr 23 '14 at 11:27
  • 2
    Thanks @display_name but as far as I can tell pm2 doesn't let me specify the test in my monit example above or an analogue: Connect to the server localhost:17394/monit.html every 2 minutes and restart the server application if the response is bad. I like the look of pm2 for managing the node application itself though so I'm going to add substituting pm2 for upstart to my todo list. – cfogelberg Apr 23 '14 at 19:47
  • If you are running a self-hosted server for fun, PM2 is probably ok to experiment with. In production, this has serious memory leak problem. Not the best process manager you want to run in linux. – activars Jul 18 '15 at 07:11