| 
									
										
										
										
											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'), | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pkg = require('./package.json'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 17:48:22 -04:00
										 |  |  | 	pidFilePath = __dirname + '/pidfile', | 
					
						
							| 
									
										
										
										
											2014-09-30 21:48:39 -04:00
										 |  |  | 	output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }), | 
					
						
							| 
									
										
										
										
											2014-12-02 17:40:48 -05:00
										 |  |  | 	silent = process.env.NODE_ENV !== 'development', | 
					
						
							| 
									
										
										
										
											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: { | 
					
						
							|  |  |  | 			cache: undefined, | 
					
						
							|  |  |  | 			map: undefined | 
					
						
							| 
									
										
										
										
											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-03 15:23:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-30 21:48:39 -04:00
										 |  |  | 	if (silent) { | 
					
						
							|  |  |  | 		console.log = function(value) { | 
					
						
							|  |  |  | 			output.write(value + '\n'); | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-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-09-04 17:09:57 -04:00
										 |  |  | 		if (!worker.suicide) { | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 			var otherWorkers; | 
					
						
							| 
									
										
										
										
											2014-10-26 14:30:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 			switch (message.action) { | 
					
						
							|  |  |  | 				case 'ready': | 
					
						
							|  |  |  | 					if (Loader.js.cache) { | 
					
						
							|  |  |  | 						worker.send({ | 
					
						
							|  |  |  | 							action: 'js-propagate', | 
					
						
							|  |  |  | 							cache: Loader.js.cache, | 
					
						
							|  |  |  | 							map: Loader.js.map, | 
					
						
							|  |  |  | 							hash: Loader.js.hash | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (Loader.css.cache) { | 
					
						
							|  |  |  | 						worker.send({ | 
					
						
							|  |  |  | 							action: 'css-propagate', | 
					
						
							|  |  |  | 							cache: Loader.css.cache, | 
					
						
							|  |  |  | 							acpCache: Loader.css.acpCache, | 
					
						
							|  |  |  | 							hash: Loader.css.hash | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 				case 'restart': | 
					
						
							|  |  |  | 					console.log('[cluster] Restarting...'); | 
					
						
							|  |  |  | 					Loader.restart(function(err) { | 
					
						
							|  |  |  | 						console.log('[cluster] Restarting...'); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 				case 'reload': | 
					
						
							|  |  |  | 					console.log('[cluster] Reloading...'); | 
					
						
							|  |  |  | 					Loader.reload(); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 				case 'js-propagate': | 
					
						
							|  |  |  | 					Loader.js.cache = message.cache; | 
					
						
							|  |  |  | 					Loader.js.map = message.map; | 
					
						
							|  |  |  | 					Loader.js.hash = message.hash; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					Loader.notifyWorkers({ | 
					
						
							|  |  |  | 						action: 'js-propagate', | 
					
						
							|  |  |  | 						cache: message.cache, | 
					
						
							|  |  |  | 						map: message.map, | 
					
						
							|  |  |  | 						hash: message.hash | 
					
						
							|  |  |  | 					}, worker.pid); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 				case 'css-propagate': | 
					
						
							|  |  |  | 					Loader.css.cache = message.cache; | 
					
						
							|  |  |  | 					Loader.css.acpCache = message.acpCache; | 
					
						
							|  |  |  | 					Loader.css.hash = message.hash; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					Loader.notifyWorkers({ | 
					
						
							|  |  |  | 						action: 'css-propagate', | 
					
						
							|  |  |  | 						cache: message.cache, | 
					
						
							|  |  |  | 						acpCache: message.acpCache, | 
					
						
							|  |  |  | 						hash: message.hash | 
					
						
							|  |  |  | 					}, worker.pid); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 				case 'listening': | 
					
						
							|  |  |  | 					if (message.primary) { | 
					
						
							|  |  |  | 						Loader.primaryWorker = parseInt(worker.pid, 10); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 				case 'config:update': | 
					
						
							|  |  |  | 					Loader.notifyWorkers(message); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											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 14:03:41 -05:00
										 |  |  | 	var worker = fork('app.js', [], { | 
					
						
							|  |  |  | 		silent: silent, | 
					
						
							|  |  |  | 		env: { | 
					
						
							|  |  |  | 			isPrimary: isPrimary, | 
					
						
							|  |  |  | 			isCluster: true, | 
					
						
							| 
									
										
										
										
											2014-12-03 15:46:52 -05:00
										 |  |  | 			port: ports[index] | 
					
						
							| 
									
										
										
										
											2014-11-27 14:09:01 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	var output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }); | 
					
						
							| 
									
										
										
										
											2014-11-26 17:24:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-03 14:03:41 -05:00
										 |  |  | 	if (silent) { | 
					
						
							|  |  |  | 		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() { | 
					
						
							|  |  |  | 	var urlObject = url.parse(nconf.get('url')); | 
					
						
							|  |  |  | 	var port = nconf.get('port') || nconf.get('PORT') || urlObject.port || 4567; | 
					
						
							|  |  |  | 	if (!Array.isArray(port)) { | 
					
						
							|  |  |  | 		port = [port]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return port; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 15:23:40 -04:00
										 |  |  | Loader.restart = function(callback) { | 
					
						
							| 
									
										
										
										
											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-03 14:03:41 -05:00
										 |  |  | Loader.notifyWorkers = function (msg, worker_pid) { | 
					
						
							|  |  |  | 	worker_pid = parseInt(worker_pid, 10); | 
					
						
							|  |  |  | 	workers.forEach(function(worker) { | 
					
						
							|  |  |  | 		if (parseInt(worker.pid, 10) !== worker_pid) { | 
					
						
							|  |  |  | 			worker.send(msg); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | nconf.argv().file({ | 
					
						
							|  |  |  | 	file: path.join(__dirname, '/config.json') | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 17:48:22 -04:00
										 |  |  | if (nconf.get('daemon') !== false) { | 
					
						
							|  |  |  | 	if (fs.existsSync(pidFilePath)) { | 
					
						
							|  |  |  | 		try { | 
					
						
							|  |  |  | 			var	pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' }); | 
					
						
							|  |  |  | 			process.kill(pid, 0); | 
					
						
							|  |  |  | 			process.exit(); | 
					
						
							|  |  |  | 		} catch (e) { | 
					
						
							|  |  |  | 			fs.unlinkSync(pidFilePath); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-30 21:48:39 -04:00
										 |  |  | 	require('daemon')(); | 
					
						
							| 
									
										
										
										
											2014-09-04 17:48:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	fs.writeFile(__dirname + '/pidfile', process.pid); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 01:47:13 -04:00
										 |  |  | async.series([ | 
					
						
							|  |  |  | 	Loader.init, | 
					
						
							|  |  |  | 	Loader.displayStartupMessages, | 
					
						
							|  |  |  | 	Loader.start | 
					
						
							|  |  |  | ], function(err) { | 
					
						
							|  |  |  | 	if (err) { | 
					
						
							|  |  |  | 		console.log('[loader] Error during startup: ' + err.message); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }); |