@ -1,45 +1,45 @@
'use strict' ;
var nconf = require ( 'nconf' ) ;
var fs = require ( 'fs' ) ;
var url = require ( 'url' ) ;
var path = require ( 'path' ) ;
var fork = require ( 'child_process' ) . fork ;
var async = require ( 'async' ) ;
var logrotate = require ( 'logrotate-stream' ) ;
var mkdirp = require ( 'mkdirp' ) ;
const nconf = require ( 'nconf' ) ;
const fs = require ( 'fs' ) ;
const url = require ( 'url' ) ;
const path = require ( 'path' ) ;
const fork = require ( 'child_process' ) . fork ;
const async = require ( 'async' ) ;
const logrotate = require ( 'logrotate-stream' ) ;
const mkdirp = require ( 'mkdirp' ) ;
var file = require ( './src/file' ) ;
var pkg = require ( './package.json' ) ;
const file = require ( './src/file' ) ;
const pkg = require ( './package.json' ) ;
var pathToConfig = path . resolve ( _ _dirname , process . env . CONFIG || 'config.json' ) ;
const pathToConfig = path . resolve ( _ _dirname , process . env . CONFIG || 'config.json' ) ;
nconf . argv ( ) . env ( ) . file ( {
file : pathToConfig ,
} ) ;
var pidFilePath = path . join ( _ _dirname , 'pidfile' ) ;
const pidFilePath = path . join ( _ _dirname , 'pidfile' ) ;
var outputLogFilePath = path . join ( _ _dirname , nconf . get ( 'logFile' ) || 'logs/output.log' ) ;
const outputLogFilePath = path . join ( _ _dirname , nconf . get ( 'logFile' ) || 'logs/output.log' ) ;
var logDir = path . dirname ( outputLogFilePath ) ;
const logDir = path . dirname ( outputLogFilePath ) ;
if ( ! fs . existsSync ( logDir ) ) {
mkdirp . sync ( path . dirname ( outputLogFilePath ) ) ;
}
var output = logrotate ( { file : outputLogFilePath , size : '1m' , keep : 3 , compress : true } ) ;
var silent = nconf . get ( 'silent' ) === 'false' ? false : nconf . get ( 'silent' ) !== false ;
var numProcs ;
var workers = [ ] ;
var Loader = {
const output = logrotate ( { file : outputLogFilePath , size : '1m' , keep : 3 , compress : true } ) ;
const silent = nconf . get ( 'silent' ) === 'false' ? false : nconf . get ( 'silent' ) !== false ;
let numProcs ;
const workers = [ ] ;
const Loader = {
timesStarted : 0 ,
} ;
var appPath = path . join ( _ _dirname , 'app.js' ) ;
const appPath = path . join ( _ _dirname , 'app.js' ) ;
Loader . init = function ( callback ) {
if ( silent ) {
console . log = function ( ) {
var args = Array . prototype . slice . call ( arguments ) ;
const args = Array . prototype . slice . call ( arguments ) ;
output . write ( ` ${ args . join ( ' ' ) } \n ` ) ;
} ;
}
@ -112,7 +112,7 @@ Loader.start = function (callback) {
numProcs = getPorts ( ) . length ;
console . log ( ` Clustering enabled: Spinning up ${ numProcs } process(es). \n ` ) ;
for ( var x = 0 ; x < numProcs ; x += 1 ) {
for ( let x = 0 ; x < numProcs ; x += 1 ) {
forkWorker ( x , x === 0 ) ;
}
@ -122,8 +122,8 @@ Loader.start = function (callback) {
} ;
function forkWorker ( index , isPrimary ) {
var ports = getPorts ( ) ;
var args = [ ] ;
const ports = getPorts ( ) ;
const args = [ ] ;
if ( ! ports [ index ] ) {
return console . log ( ` [cluster] invalid port for worker : ${ index } ports: ${ ports . length } ` ) ;
@ -133,7 +133,7 @@ function forkWorker(index, isPrimary) {
process . env . isCluster = nconf . get ( 'isCluster' ) || ports . length > 1 ;
process . env . port = ports [ index ] ;
var worker = fork ( appPath , args , {
const worker = fork ( appPath , args , {
silent : silent ,
env : process . env ,
} ) ;
@ -146,20 +146,20 @@ function forkWorker(index, isPrimary) {
Loader . addWorkerEvents ( worker ) ;
if ( silent ) {
var output = logrotate ( { file : outputLogFilePath , size : '1m' , keep : 3 , compress : true } ) ;
const output = logrotate ( { file : outputLogFilePath , size : '1m' , keep : 3 , compress : true } ) ;
worker . stdout . pipe ( output ) ;
worker . stderr . pipe ( output ) ;
}
}
function getPorts ( ) {
var _url = nconf . get ( 'url' ) ;
const _url = nconf . get ( 'url' ) ;
if ( ! _url ) {
console . log ( '[cluster] url is undefined, please check your config.json' ) ;
process . exit ( ) ;
}
var urlObject = url . parse ( _url ) ;
var port = nconf . get ( 'PORT' ) || nconf . get ( 'port' ) || urlObject . port || 4567 ;
const urlObject = url . parse ( _url ) ;
let port = nconf . get ( 'PORT' ) || nconf . get ( 'port' ) || urlObject . port || 4567 ;
if ( ! Array . isArray ( port ) ) {
port = [ port ] ;
}
@ -178,7 +178,7 @@ Loader.restart = function () {
throw err ;
}
var conf = JSON . parse ( configFile ) ;
const conf = JSON . parse ( configFile ) ;
nconf . stores . env . readOnly = false ;
nconf . set ( 'url' , conf . url ) ;
@ -217,7 +217,7 @@ fs.open(pathToConfig, 'r', (err) => {
if ( nconf . get ( 'daemon' ) !== 'false' && nconf . get ( 'daemon' ) !== false ) {
if ( file . existsSync ( pidFilePath ) ) {
try {
var pid = fs . readFileSync ( pidFilePath , { encoding : 'utf-8' } ) ;
const pid = fs . readFileSync ( pidFilePath , { encoding : 'utf-8' } ) ;
process . kill ( pid , 0 ) ;
process . exit ( ) ;
} catch ( e ) {