| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var	nconf = require('nconf'), | 
					
						
							|  |  |  | 	fs = require('fs'), | 
					
						
							|  |  |  | 	pidFilePath = __dirname + '/pidfile', | 
					
						
							| 
									
										
										
										
											2014-02-22 02:27:14 -05:00
										 |  |  | 	start = function() { | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | 		var	fork = require('child_process').fork, | 
					
						
							|  |  |  | 			nbb_start = function() { | 
					
						
							|  |  |  | 				nbb = fork('./app', process.argv.slice(2), { | 
					
						
							|  |  |  | 						env: { | 
					
						
							|  |  |  | 							'NODE_ENV': process.env.NODE_ENV | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-02-22 02:27:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-09 13:10:28 -04:00
										 |  |  | 				nbb.on('message', function(message) { | 
					
						
							|  |  |  | 					if (message && typeof message === 'object' && message.action) { | 
					
						
							|  |  |  | 						if (message.action === 'restart') { | 
					
						
							|  |  |  | 							nbb_restart(); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				nbb.on('exit', function(code, signal) { | 
					
						
							|  |  |  | 					if (code) { | 
					
						
							|  |  |  | 						nbb_start(); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						nbb_stop(); | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-02-23 23:08:54 -05:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			nbb_stop = function() { | 
					
						
							| 
									
										
										
										
											2014-02-23 23:08:54 -05:00
										 |  |  | 				nbb.kill(); | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | 				if (fs.existsSync(pidFilePath)) { | 
					
						
							|  |  |  | 					var	pid = parseInt(fs.readFileSync(pidFilePath, { encoding: 'utf-8' }), 10); | 
					
						
							|  |  |  | 					if (process.pid === pid) { | 
					
						
							|  |  |  | 						fs.unlinkSync(pidFilePath); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-02-27 20:36:04 -05:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			nbb_restart = function() { | 
					
						
							|  |  |  | 				nbb.on('exit', function() { | 
					
						
							|  |  |  | 					nbb_start(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				nbb.kill(); | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | 			}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		process.on('SIGINT', nbb_stop); | 
					
						
							|  |  |  | 		process.on('SIGTERM', nbb_stop); | 
					
						
							| 
									
										
										
										
											2014-02-27 20:36:04 -05:00
										 |  |  | 		process.on('SIGHUP', nbb_restart); | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		nbb_start(); | 
					
						
							| 
									
										
										
										
											2014-02-26 13:42:39 -05:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	nbb; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | nconf.argv(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-02 20:37:57 -05:00
										 |  |  | // Start the daemon!
 | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | if (nconf.get('d')) { | 
					
						
							|  |  |  | 	// Check for a still-active NodeBB process
 | 
					
						
							|  |  |  | 	if (fs.existsSync(pidFilePath)) { | 
					
						
							| 
									
										
										
										
											2014-03-02 20:37:57 -05:00
										 |  |  | 		try { | 
					
						
							|  |  |  | 			var	pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' }); | 
					
						
							|  |  |  | 			process.kill(pid, 0); | 
					
						
							|  |  |  | 			console.log('\n  Error: Another NodeBB is already running!'); | 
					
						
							|  |  |  | 			process.exit(); | 
					
						
							|  |  |  | 		} catch (e) { | 
					
						
							|  |  |  | 			fs.unlinkSync(pidFilePath); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 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); | 
					
						
							| 
									
										
										
										
											2014-02-22 02:27:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | 		start(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  | 	start(); | 
					
						
							|  |  |  | } |