| 
									
										
										
										
											2014-12-02 17:40:48 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | var	nconf = require('nconf'), | 
					
						
							|  |  |  | 	fs = require('fs'), | 
					
						
							| 
									
										
										
										
											2014-12-01 12:36:59 -05:00
										 |  |  | 	url = require('url'), | 
					
						
							| 
									
										
										
										
											2014-09-16 11:08:25 -04:00
										 |  |  | 	path = require('path'), | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	fork = require('child_process').fork, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 15:23:40 -04:00
										 |  |  | 	async = require('async'), | 
					
						
							| 
									
										
										
										
											2014-09-30 21:48:39 -04:00
										 |  |  | 	logrotate = require('logrotate-stream'), | 
					
						
							| 
									
										
										
										
											2015-09-29 18:22:41 -04:00
										 |  |  | 	file = require('./src/file'), | 
					
						
							| 
									
										
										
										
											2015-05-06 21:41:04 -04:00
										 |  |  | 	pkg = require('./package.json'); | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 21:41:04 -04:00
										 |  |  | nconf.argv().env().file({ | 
					
						
							|  |  |  | 	file: path.join(__dirname, '/config.json') | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var	pidFilePath = __dirname + '/pidfile', | 
					
						
							| 
									
										
										
										
											2014-09-30 21:48:39 -04:00
										 |  |  | 	output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }), | 
					
						
							| 
									
										
										
										
											2015-05-20 09:57:04 -04:00
										 |  |  | 	silent = nconf.get('silent') === 'false' ? false : nconf.get('silent') !== false, | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 	numProcs, | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	workers = [], | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-05 13:23:26 -04:00
										 |  |  | 	Loader = { | 
					
						
							|  |  |  | 		timesStarted: 0, | 
					
						
							|  |  |  | 		js: { | 
					
						
							| 
									
										
										
										
											2016-03-03 00:09:52 -05:00
										 |  |  | 			target: {} | 
					
						
							| 
									
										
										
										
											2014-09-29 19:31:27 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		css: { | 
					
						
							|  |  |  | 			cache: undefined, | 
					
						
							|  |  |  | 			acpCache: undefined | 
					
						
							| 
									
										
										
										
											2014-09-05 13:23:26 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-02-27 10:06:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | Loader.init = function(callback) { | 
					
						
							| 
									
										
										
										
											2014-09-30 21:48:39 -04:00
										 |  |  | 	if (silent) { | 
					
						
							| 
									
										
										
										
											2016-03-31 12:26:51 -04:00
										 |  |  | 		console.log = function() { | 
					
						
							|  |  |  | 			var args = Array.prototype.slice.call(arguments); | 
					
						
							|  |  |  | 			output.write(args.join(' ') + '\n'); | 
					
						
							| 
									
										
										
										
											2014-09-30 21:48:39 -04:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 	process.on('SIGHUP', Loader.restart); | 
					
						
							| 
									
										
										
										
											2014-11-04 17:39:13 -05:00
										 |  |  | 	process.on('SIGUSR2', Loader.reload); | 
					
						
							| 
									
										
										
										
											2014-11-17 14:47:36 -05:00
										 |  |  | 	process.on('SIGTERM', Loader.stop); | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 	callback(); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Loader.displayStartupMessages = function(callback) { | 
					
						
							| 
									
										
										
										
											2014-10-22 13:47:05 -04:00
										 |  |  | 	console.log(''); | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 	console.log('NodeBB v' + pkg.version + ' Copyright (C) 2013-2014 NodeBB Inc.'); | 
					
						
							|  |  |  | 	console.log('This program comes with ABSOLUTELY NO WARRANTY.'); | 
					
						
							|  |  |  | 	console.log('This is free software, and you are welcome to redistribute it under certain conditions.'); | 
					
						
							|  |  |  | 	console.log('For the full license, please visit: http://www.gnu.org/copyleft/gpl.html'); | 
					
						
							|  |  |  | 	console.log(''); | 
					
						
							|  |  |  | 	callback(); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | Loader.addWorkerEvents = function(worker) { | 
					
						
							| 
									
										
										
										
											2014-09-03 01:24:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	worker.on('exit', function(code, signal) { | 
					
						
							| 
									
										
										
										
											2014-09-03 15:23:40 -04:00
										 |  |  | 		if (code !== 0) { | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 			if (Loader.timesStarted < numProcs*3) { | 
					
						
							| 
									
										
										
										
											2014-09-03 15:23:40 -04:00
										 |  |  | 				Loader.timesStarted++; | 
					
						
							| 
									
										
										
										
											2014-09-04 00:01:08 -04:00
										 |  |  | 				if (Loader.crashTimer) { | 
					
						
							|  |  |  | 					clearTimeout(Loader.crashTimer); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				Loader.crashTimer = setTimeout(function() { | 
					
						
							|  |  |  | 					Loader.timesStarted = 0; | 
					
						
							| 
									
										
										
										
											2014-12-03 23:16:50 -05:00
										 |  |  | 				}, 10000); | 
					
						
							| 
									
										
										
										
											2014-09-03 15:23:40 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 				console.log(numProcs*3 + ' restarts in 10 seconds, most likely an error on startup. Halting.'); | 
					
						
							| 
									
										
										
										
											2014-09-03 15:23:40 -04:00
										 |  |  | 				process.exit(); | 
					
						
							| 
									
										
										
										
											2014-09-03 01:24:26 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-09-03 15:23:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 		console.log('[cluster] Child Process (' + worker.pid + ') has exited (code: ' + code + ', signal: ' + signal +')'); | 
					
						
							| 
									
										
										
										
											2014-12-10 18:52:38 -05:00
										 |  |  | 		if (!(worker.suicide || code === 0)) { | 
					
						
							| 
									
										
										
										
											2014-12-03 14:17:05 -05:00
										 |  |  | 			console.log('[cluster] Spinning up another process...'); | 
					
						
							| 
									
										
										
										
											2014-09-05 13:44:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:17:05 -05:00
										 |  |  | 			forkWorker(worker.index, worker.isPrimary); | 
					
						
							| 
									
										
										
										
											2014-09-04 17:09:57 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-05-04 16:46:09 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-09-04 00:56:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	worker.on('message', function(message) { | 
					
						
							|  |  |  | 		if (message && typeof message === 'object' && message.action) { | 
					
						
							|  |  |  | 			switch (message.action) { | 
					
						
							|  |  |  | 				case 'ready': | 
					
						
							| 
									
										
										
										
											2016-03-03 07:20:07 +02:00
										 |  |  | 					if (Loader.js.target['nodebb.min.js'] && Loader.js.target['nodebb.min.js'].cache && !worker.isPrimary) { | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 						worker.send({ | 
					
						
							|  |  |  | 							action: 'js-propagate', | 
					
						
							| 
									
										
										
										
											2016-03-03 07:14:35 +02:00
										 |  |  | 							cache: Loader.js.target['nodebb.min.js'].cache, | 
					
						
							|  |  |  | 							map: Loader.js.target['nodebb.min.js'].map, | 
					
						
							|  |  |  | 							target: 'nodebb.min.js' | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-03 07:20:07 +02:00
										 |  |  | 					if (Loader.js.target['acp.min.js'] && Loader.js.target['acp.min.js'].cache && !worker.isPrimary) { | 
					
						
							| 
									
										
										
										
											2016-03-03 07:14:35 +02:00
										 |  |  | 						worker.send({ | 
					
						
							|  |  |  | 							action: 'js-propagate', | 
					
						
							|  |  |  | 							cache: Loader.js.target['acp.min.js'].cache, | 
					
						
							|  |  |  | 							map: Loader.js.target['acp.min.js'].map, | 
					
						
							|  |  |  | 							target: 'acp.min.js' | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 20:26:02 -04:00
										 |  |  | 					if (Loader.css.cache && !worker.isPrimary) { | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 						worker.send({ | 
					
						
							|  |  |  | 							action: 'css-propagate', | 
					
						
							|  |  |  | 							cache: Loader.css.cache, | 
					
						
							| 
									
										
										
										
											2015-09-11 16:18:32 -04:00
										 |  |  | 							acpCache: Loader.css.acpCache | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-04-27 20:26:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 				case 'restart': | 
					
						
							|  |  |  | 					console.log('[cluster] Restarting...'); | 
					
						
							| 
									
										
										
										
											2014-12-03 23:07:00 -05:00
										 |  |  | 					Loader.restart(); | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 				case 'reload': | 
					
						
							|  |  |  | 					console.log('[cluster] Reloading...'); | 
					
						
							|  |  |  | 					Loader.reload(); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 				case 'js-propagate': | 
					
						
							| 
									
										
										
										
											2016-03-30 15:54:45 -04:00
										 |  |  | 					Loader.js.target = message.data; | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					Loader.notifyWorkers({ | 
					
						
							|  |  |  | 						action: 'js-propagate', | 
					
						
							| 
									
										
										
										
											2016-03-30 15:54:45 -04:00
										 |  |  | 						data: message.data | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 					}, worker.pid); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 				case 'css-propagate': | 
					
						
							|  |  |  | 					Loader.css.cache = message.cache; | 
					
						
							|  |  |  | 					Loader.css.acpCache = message.acpCache; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					Loader.notifyWorkers({ | 
					
						
							|  |  |  | 						action: 'css-propagate', | 
					
						
							|  |  |  | 						cache: message.cache, | 
					
						
							| 
									
										
										
										
											2015-09-11 16:18:32 -04:00
										 |  |  | 						acpCache: message.acpCache | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 					}, worker.pid); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2015-04-27 20:26:02 -04:00
										 |  |  | 				case 'templates:compiled': | 
					
						
							|  |  |  | 					Loader.notifyWorkers({ | 
					
						
							|  |  |  | 						action: 'templates:compiled', | 
					
						
							|  |  |  | 					}, worker.pid); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-10-28 16:33:14 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-09-03 15:23:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | Loader.start = function(callback) { | 
					
						
							| 
									
										
										
										
											2014-12-03 15:46:52 -05:00
										 |  |  | 	numProcs = getPorts().length; | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 	console.log('Clustering enabled: Spinning up ' + numProcs + ' process(es).\n'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	for (var x=0; x<numProcs; ++x) { | 
					
						
							|  |  |  | 		forkWorker(x, x === 0); | 
					
						
							| 
									
										
										
										
											2014-09-04 17:09:57 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 16:33:14 -04:00
										 |  |  | 	if (callback) { | 
					
						
							|  |  |  | 		callback(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-08 12:07:33 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-09-03 15:23:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | function forkWorker(index, isPrimary) { | 
					
						
							| 
									
										
										
										
											2014-12-03 15:46:52 -05:00
										 |  |  | 	var ports = getPorts(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(!ports[index]) { | 
					
						
							|  |  |  | 		return console.log('[cluster] invalid port for worker : ' + index + ' ports: ' + ports.length); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-27 14:09:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 17:10:25 -05:00
										 |  |  | 	process.env.isPrimary = isPrimary; | 
					
						
							|  |  |  | 	process.env.isCluster = true; | 
					
						
							|  |  |  | 	process.env.port = ports[index]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	var worker = fork('app.js', [], { | 
					
						
							|  |  |  | 		silent: silent, | 
					
						
							| 
									
										
										
										
											2014-12-03 17:10:25 -05:00
										 |  |  | 		env: process.env | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-11-27 14:09:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	worker.index = index; | 
					
						
							|  |  |  | 	worker.isPrimary = isPrimary; | 
					
						
							| 
									
										
										
										
											2014-11-27 14:09:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	workers[index] = worker; | 
					
						
							| 
									
										
										
										
											2014-10-28 16:33:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	Loader.addWorkerEvents(worker); | 
					
						
							| 
									
										
										
										
											2014-11-26 17:24:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	if (silent) { | 
					
						
							| 
									
										
										
										
											2014-12-03 17:06:19 -05:00
										 |  |  | 		var output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }); | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 		worker.stdout.pipe(output); | 
					
						
							|  |  |  | 		worker.stderr.pipe(output); | 
					
						
							| 
									
										
										
										
											2014-11-26 17:24:49 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 15:46:52 -05:00
										 |  |  | function getPorts() { | 
					
						
							| 
									
										
										
										
											2015-04-27 22:51:44 -04:00
										 |  |  | 	var _url = nconf.get('url'); | 
					
						
							|  |  |  | 	if (!_url) { | 
					
						
							| 
									
										
										
										
											2015-04-27 21:42:04 -04:00
										 |  |  | 		console.log('[cluster] url is undefined, please check your config.json'); | 
					
						
							|  |  |  | 		process.exit(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-27 22:51:44 -04:00
										 |  |  | 	var urlObject = url.parse(_url); | 
					
						
							| 
									
										
										
										
											2014-12-03 15:46:52 -05:00
										 |  |  | 	var port = nconf.get('port') || nconf.get('PORT') || urlObject.port || 4567; | 
					
						
							|  |  |  | 	if (!Array.isArray(port)) { | 
					
						
							|  |  |  | 		port = [port]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return port; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 23:07:00 -05:00
										 |  |  | Loader.restart = function() { | 
					
						
							| 
									
										
										
										
											2014-12-02 16:49:49 -05:00
										 |  |  | 	killWorkers(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	Loader.start(); | 
					
						
							| 
									
										
										
										
											2014-09-04 17:09:57 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Loader.reload = function() { | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	workers.forEach(function(worker) { | 
					
						
							|  |  |  | 		worker.send({ | 
					
						
							| 
									
										
										
										
											2014-09-04 17:39:53 -04:00
										 |  |  | 			action: 'reload' | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-09-04 17:09:57 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-09-04 17:41:00 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-09-03 15:23:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 14:47:36 -05:00
										 |  |  | Loader.stop = function() { | 
					
						
							| 
									
										
										
										
											2014-12-02 16:49:49 -05:00
										 |  |  | 	killWorkers(); | 
					
						
							| 
									
										
										
										
											2014-11-17 14:47:36 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Clean up the pidfile
 | 
					
						
							|  |  |  | 	fs.unlinkSync(__dirname + '/pidfile'); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 16:49:49 -05:00
										 |  |  | function killWorkers() { | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	workers.forEach(function(worker) { | 
					
						
							|  |  |  | 		worker.suicide = true; | 
					
						
							|  |  |  | 		worker.kill(); | 
					
						
							| 
									
										
										
										
											2014-12-02 16:49:49 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-10 19:44:27 -05:00
										 |  |  | Loader.notifyWorkers = function(msg, worker_pid) { | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	worker_pid = parseInt(worker_pid, 10); | 
					
						
							|  |  |  | 	workers.forEach(function(worker) { | 
					
						
							|  |  |  | 		if (parseInt(worker.pid, 10) !== worker_pid) { | 
					
						
							| 
									
										
										
										
											2014-12-10 19:44:27 -05:00
										 |  |  | 			try { | 
					
						
							|  |  |  | 				worker.send(msg); | 
					
						
							|  |  |  | 			} catch (e) { | 
					
						
							|  |  |  | 				console.log('[cluster/notifyWorkers] Failed to reach pid ' + worker_pid); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-12-02 17:40:48 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-10-28 16:33:14 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-09-16 11:17:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 22:41:44 -04:00
										 |  |  | fs.open(path.join(__dirname, 'config.json'), 'r', function(err) { | 
					
						
							|  |  |  | 	if (!err) { | 
					
						
							| 
									
										
										
										
											2015-05-20 09:57:04 -04:00
										 |  |  | 		if (nconf.get('daemon') !== 'false' && nconf.get('daemon') !== false) { | 
					
						
							| 
									
										
										
										
											2015-09-29 18:22:41 -04:00
										 |  |  | 			if (file.existsSync(pidFilePath)) { | 
					
						
							| 
									
										
										
										
											2015-04-27 22:41:44 -04:00
										 |  |  | 				try { | 
					
						
							|  |  |  | 					var	pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' }); | 
					
						
							|  |  |  | 					process.kill(pid, 0); | 
					
						
							|  |  |  | 					process.exit(); | 
					
						
							|  |  |  | 				} catch (e) { | 
					
						
							|  |  |  | 					fs.unlinkSync(pidFilePath); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-09-04 17:48:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 22:41:44 -04:00
										 |  |  | 			require('daemon')({ | 
					
						
							|  |  |  | 				stdout: process.stdout, | 
					
						
							|  |  |  | 				stderr: process.stderr | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-09-04 17:48:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 22:41:44 -04:00
										 |  |  | 			fs.writeFile(__dirname + '/pidfile', process.pid); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-09-04 17:48:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-27 22:41:44 -04:00
										 |  |  | 		async.series([ | 
					
						
							|  |  |  | 			Loader.init, | 
					
						
							|  |  |  | 			Loader.displayStartupMessages, | 
					
						
							|  |  |  | 			Loader.start | 
					
						
							|  |  |  | 		], function(err) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				console.log('[loader] Error during startup: ' + err.message); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		// No config detected, kickstart web installer
 | 
					
						
							|  |  |  | 		var child = require('child_process').fork('app'); | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-27 22:41:44 -04:00
										 |  |  | }); |