Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Combining all knowledge gathered (Big thanks to <a href="https://stackoverflow.com/users/1309986/julian-knight">Julian Knight</a> for the ideas) and methods tested in the past week, I've decided to settle for the deployment solution described below (I thought I'd be nice to share to help others with comparable questions):</p> <p>Auto-restarting on script errors <strong><em>and</em></strong> automatic reloading on script changes is handled by <a href="https://github.com/nodejitsu/forever/" rel="nofollow noreferrer"><strong>forever</strong></a>, as it also includes a script watch, as long as Forever is spawned from within a node.js script.</p> <p>To do so, I've added a <code>server.js</code> to launch the <code>app.js</code> script we actually want to run:</p> <p><strong>server.js</strong></p> <pre><code>var forever = require('forever'), child = new(forever.Monitor)('app.js', { 'silent': false, 'pidFile': 'pids/app.pid', 'watch': true, 'watchDirectory': '.', // Top-level directory to watch from. 'watchIgnoreDotFiles': true, // whether to ignore dot files 'watchIgnorePatterns': [], // array of glob patterns to ignore, merged with contents of watchDirectory + '/.foreverignore' file 'logFile': 'logs/forever.log', // Path to log output from forever process (when daemonized) 'outFile': 'logs/forever.out', // Path to log output from child stdout 'errFile': 'logs/forever.err' }); child.start(); forever.startServer(child); </code></pre> <p>This watches all files in the application directory for changes and restarts the script running in <code>forever</code>as soon as one changes. Because the logs and pidfile are in subdirectories of the application, those have to be ignored from the file watch, or the script will loop restarts:</p> <p><strong>.foreverignore</strong></p> <pre><code>pids/** logs/** </code></pre> <p>To make this all start on system boot and enabling us to easily control the service using <code>start node-app</code>and <code>stop node-app</code> we use <a href="http://upstart.ubuntu.com/" rel="nofollow noreferrer">Ubuntu's Upstart</a>. I've combined two examples (<a href="http://kevin.vanzonneveld.net/techblog/article/run_nodejs_as_a_service_on_ubuntu_karmic/" rel="nofollow noreferrer">this</a> and <a href="http://geeknme.wordpress.com/2009/10/15/getting-started-with-upstart-in-ubuntu/" rel="nofollow noreferrer">this</a> one) into one that does the job quite well:</p> <p><strong>/etc/init/node-app.conf</strong></p> <pre><code># This is an upstart (http://upstart.ubuntu.com/) script # to run the node.js server on system boot and make it # manageable with commands such as # 'start node-app' and 'stop node-app' # # This script is to be placed in /etc/init to work with upstart. # # Internally the 'initctl' command is used to manage: # initctl help # initctl status node-app # initctl reload node-app # initctl start node-app description "node.js forever server for node-app" author "Remco Overdijk &lt;remco@maxserv.nl&gt;" version "1.0" expect fork # used to be: start on startup # until we found some mounts weren't ready yet while booting: start on started mountall stop on shutdown # Automatically Respawn: respawn respawn limit 99 5 env HOME=/home/user/node-app-dir script # Not sure why $HOME is needed, but we found that it is: export HOME=$HOME chdir $HOME exec /usr/local/bin/node server.js &gt; logs/node.log &amp; end script #post-start script # # Optionally put a script here that will notifiy you node has (re)started # # /root/bin/hoptoad.sh "node.js has started!" #end script </code></pre> <p>As <a href="http://kevin.vanzonneveld.net/techblog/article/run_nodejs_as_a_service_on_ubuntu_karmic/" rel="nofollow noreferrer">Kevin wisely mentions in his article</a> it's unwise to run node as root, so we'll change that to <code>exec sudo -u www-data /usr/local/bin/node</code> when we move to new servers next week.</p> <p>So, <code>forever</code> gets started automatically by <code>node server.js</code> which gets launched by <code>upstart</code>, and monitors for crashes and file changes, keeping the entire setup running as long as we want.</p> <p>I hope this helps anyone.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload