Created
February 14, 2012 11:09
-
-
Save zenlor/1825834 to your computer and use it in GitHub Desktop.
daemonizing wrapper for node apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express') | |
| , app = module.exports = express.createServer(); | |
| // you express app resides here ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var daemon = require('daemon') | |
| , app = require('./app') | |
| , db = require('mongoose'); | |
| // `QUIT` Signal handling | |
| process.on('QUIT', function() { | |
| console.log("SIGQUIT received! exiting"); | |
| // Close the db connection | |
| db.close(function(e) { | |
| if (e) console.log('ERROR closing the DB connection:\n', e); | |
| // Delete the pid file | |
| fs.unlink('./tmp/node.pid', function(e) { | |
| if (e) console.log(e); | |
| // Exit | |
| process.exit(); | |
| }); | |
| }); | |
| }); | |
| // I Expect a $PORT environment variable | |
| app.listen( parseInt(process.env.PORT, 10) || 3001, function(err){ | |
| if ( err ) { | |
| throw err; | |
| } | |
| // Daemonize the Application once started logging to our `./log/production.log` and `./log/production.error.log` | |
| daemon.daemonize({ | |
| stdout: './log/' +process.env.ENV+ '.log' | |
| , stderr: './log/'+process.env.ENV +'.error.log' | |
| } | |
| , './tmp/node.pid' | |
| , function (err, pid) { | |
| if (err) { | |
| console.log('Error starting daemon: '); | |
| throw err; | |
| } | |
| console.log('Daemonized successfully with pid: ' + pid); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment