From 5b301772bb0a1b6c9cc4fd5ba72371a23a9b5cf8 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 27 Feb 2014 10:06:31 -0500 Subject: [PATCH] added daemon capability to ./nodebb start, npm start/stop scripts --- loader.js | 75 ++++++++++++++++++++++++++++++++++++------------ logs/.gitignore | 1 + nodebb | 14 ++++++++- package.json | 5 +++- src/webserver.js | 2 +- 5 files changed, 76 insertions(+), 21 deletions(-) create mode 100644 logs/.gitignore diff --git a/loader.js b/loader.js index 2263955ef9..3bb103d417 100644 --- a/loader.js +++ b/loader.js @@ -1,26 +1,65 @@ -var fork = require('child_process').fork, +"use strict"; + +var nconf = require('nconf'), + fs = require('fs'), + pidFilePath = __dirname + '/pidfile', start = function() { - nbb = fork('./app', process.argv.slice(2), { - env: { - 'NODE_ENV': process.env.NODE_ENV - } - }); + var fork = require('child_process').fork, + nbb_start = function() { + nbb = fork('./app', process.argv.slice(2), { + env: { + 'NODE_ENV': process.env.NODE_ENV + } + }); - nbb.on('message', function(cmd) { - if (cmd === 'nodebb:restart') { - nbb.on('exit', function() { - start(); + nbb.on('message', function(cmd) { + if (cmd === 'nodebb:restart') { + nbb.on('exit', function() { + nbb_start(); + }); + nbb.kill(); + } }); + }, + nbb_stop = function() { nbb.kill(); - } - }); - }, - stop = function() { - nbb.kill(); + if (fs.existsSync(pidFilePath)) { + var pid = parseInt(fs.readFileSync(pidFilePath, { encoding: 'utf-8' }), 10); + if (process.pid === pid) { + fs.unlinkSync(pidFilePath); + } + } + }; + + process.on('SIGINT', nbb_stop); + process.on('SIGTERM', nbb_stop); + + nbb_start(); }, nbb; -process.on('SIGINT', stop); -process.on('SIGTERM', stop); +nconf.argv(); + +if (nconf.get('d')) { + // Check for a still-active NodeBB process + if (fs.existsSync(pidFilePath)) { + console.log('\n Error: Another NodeBB is already running!'); + process.exit(); + } + + // Initialise logging streams + var outputStream = fs.createWriteStream(__dirname + '/logs/output.log'); + outputStream.on('open', function(fd) { + // Daemonize + require('daemon')({ + stdout: fd + }); + + // Write its pid to a pidfile + fs.writeFile(__dirname + '/pidfile', process.pid); -start(); \ No newline at end of file + start(); + }); +} else { + start(); +} \ No newline at end of file diff --git a/logs/.gitignore b/logs/.gitignore new file mode 100644 index 0000000000..397b4a7624 --- /dev/null +++ b/logs/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/nodebb b/nodebb index 0ea67b0a69..3e4d0e20a7 100755 --- a/nodebb +++ b/nodebb @@ -6,7 +6,19 @@ case "$1" in start) - node loader "$@" + echo "Starting NodeBB"; + echo " \"./nodebb stop\" to stop the NodeBB server"; + echo " \"./nodebb log\" to view server output"; + node loader -d "$@" + ;; + + stop) + echo "Stopping NodeBB. Goodbye!"; + kill `cat pidfile`; + ;; + + log) + tail -F ./logs/output.log; ;; upgrade) diff --git a/package.json b/package.json index 221a6a781a..fec5bc2bab 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,8 @@ }, "main": "app.js", "scripts": { + "start": "./nodebb start", + "stop": "./nodebb stop", "test": "mocha ./tests" }, "dependencies": { @@ -43,7 +45,8 @@ "nodebb-theme-vanilla": "~0.0.14", "nodebb-theme-cerulean": "~0.0.13", "nodebb-theme-lavender": "~0.0.22", - "less": "^1.6.3" + "less": "^1.6.3", + "daemon": "~1.1.0" }, "optionalDependencies": { "redis": "0.8.3", diff --git a/src/webserver.js b/src/webserver.js index b7028efc5c..b4d5afe7b6 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -52,7 +52,7 @@ var shutdown = function(code) { db.close(); winston.info('[app] Database connection closed.'); - winston.info('[app] Goodbye!'); + winston.info('[app] Shutdown complete.'); process.exit(); }, restart = function() {