2017-03-11 23:21:08 +08:00
|
|
|
/* eslint global-require:0 */
|
|
|
|
|
2017-03-06 05:45:50 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-24 01:05:16 +08:00
|
|
|
process.env.UV_THREADPOOL_SIZE = 16;
|
2017-05-24 00:11:42 +08:00
|
|
|
|
2023-09-28 15:44:23 +08:00
|
|
|
const v8 = require('node:v8');
|
|
|
|
const Path = require('path');
|
|
|
|
const os = require('os');
|
2017-07-16 19:37:33 +08:00
|
|
|
const config = require('wild-config');
|
2017-09-04 18:19:52 +08:00
|
|
|
|
|
|
|
if (process.env.NODE_CONFIG_ONLY === 'true') {
|
|
|
|
console.log(require('util').inspect(config, false, 22)); // eslint-disable-line
|
|
|
|
return process.exit();
|
|
|
|
}
|
|
|
|
|
2017-08-29 15:36:45 +08:00
|
|
|
const errors = require('./lib/errors');
|
2017-07-12 02:38:23 +08:00
|
|
|
const fs = require('fs');
|
2017-06-03 14:51:58 +08:00
|
|
|
const log = require('npmlog');
|
|
|
|
const packageData = require('./package.json');
|
2023-01-09 17:57:15 +08:00
|
|
|
const { init: initElasticSearch } = require('./lib/elasticsearch');
|
2017-03-06 05:45:50 +08:00
|
|
|
|
|
|
|
log.level = config.log.level;
|
2017-03-11 23:21:08 +08:00
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
const printLogo = () => {
|
2017-08-31 19:44:05 +08:00
|
|
|
let logo = fs
|
|
|
|
.readFileSync(__dirname + '/logo.txt', 'utf-8')
|
|
|
|
.replace(/^\n+|\n+$/g, '')
|
|
|
|
.split('\n');
|
2017-07-12 02:38:23 +08:00
|
|
|
|
|
|
|
let columnLength = logo.map(l => l.length).reduce((max, val) => (val > max ? val : max), 0);
|
|
|
|
let versionString = ' ' + packageData.name + '@' + packageData.version + ' ';
|
|
|
|
let versionPrefix = '-'.repeat(Math.round(columnLength / 2 - versionString.length / 2));
|
|
|
|
let versionSuffix = '-'.repeat(columnLength - versionPrefix.length - versionString.length);
|
|
|
|
|
|
|
|
log.info('App', ' ' + '-'.repeat(columnLength));
|
2017-04-02 00:22:47 +08:00
|
|
|
log.info('App', '');
|
2017-07-12 02:38:23 +08:00
|
|
|
|
|
|
|
logo.forEach(line => {
|
|
|
|
log.info('App', ' ' + line);
|
|
|
|
});
|
|
|
|
|
2017-04-02 00:22:47 +08:00
|
|
|
log.info('App', '');
|
2017-07-12 02:38:23 +08:00
|
|
|
|
|
|
|
log.info('App', ' ' + versionPrefix + versionString + versionSuffix);
|
2017-04-02 00:22:47 +08:00
|
|
|
log.info('App', '');
|
2017-03-11 23:21:08 +08:00
|
|
|
};
|
2017-03-06 05:45:50 +08:00
|
|
|
|
2021-11-12 16:29:17 +08:00
|
|
|
let processCount = config.processes;
|
|
|
|
if (processCount) {
|
|
|
|
if (/^\s*cpus\s*$/i.test(processCount)) {
|
|
|
|
processCount = os.cpus().length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof processCount !== 'number' && !isNaN(processCount)) {
|
|
|
|
processCount = Number(processCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNaN(processCount)) {
|
|
|
|
processCount = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!processCount || processCount <= 1) {
|
2017-03-11 23:21:08 +08:00
|
|
|
printLogo();
|
|
|
|
if (config.ident) {
|
|
|
|
process.title = config.ident;
|
2017-03-06 05:45:50 +08:00
|
|
|
}
|
2017-03-11 23:21:08 +08:00
|
|
|
// single process mode, do not fork anything
|
2023-01-09 17:57:15 +08:00
|
|
|
|
|
|
|
initElasticSearch()
|
2023-01-19 16:46:12 +08:00
|
|
|
.then(started => {
|
|
|
|
if (started) {
|
|
|
|
log.verbose('App', `ElasticSearch setup checked`);
|
|
|
|
}
|
2023-01-12 17:43:01 +08:00
|
|
|
})
|
2023-01-09 17:57:15 +08:00
|
|
|
.catch(err => {
|
|
|
|
log.error('App', `ElasticSearch setup failed: ${err.message}${err.meta?.statusCode ? ` (${err.meta?.statusCode})` : ''}`);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
require('./worker.js');
|
|
|
|
});
|
2017-03-11 23:21:08 +08:00
|
|
|
} else {
|
|
|
|
let cluster = require('cluster');
|
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
printLogo();
|
|
|
|
|
|
|
|
if (config.ident) {
|
|
|
|
process.title = config.ident + ' master';
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info('App', `Master [${process.pid}] is running`);
|
|
|
|
|
2017-09-04 18:19:52 +08:00
|
|
|
let workers = new Set();
|
|
|
|
|
2017-03-11 23:21:08 +08:00
|
|
|
let forkWorker = () => {
|
|
|
|
let worker = cluster.fork();
|
2017-09-04 18:19:52 +08:00
|
|
|
workers.add(worker);
|
2017-03-11 23:21:08 +08:00
|
|
|
log.info('App', `Forked worker ${worker.process.pid}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fork workers.
|
2023-01-09 17:57:15 +08:00
|
|
|
initElasticSearch()
|
2023-01-19 16:46:12 +08:00
|
|
|
.then(started => {
|
|
|
|
if (started) {
|
|
|
|
log.verbose('App', `ElasticSearch setup checked`);
|
|
|
|
}
|
2023-01-12 17:43:01 +08:00
|
|
|
})
|
2023-01-09 17:57:15 +08:00
|
|
|
.catch(err => {
|
|
|
|
log.error('App', `ElasticSearch setup failed: ${err.message}${err.meta?.statusCode ? ` (${err.meta?.statusCode})` : ''}`);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
for (let i = 0; i < processCount; i++) {
|
|
|
|
forkWorker();
|
|
|
|
}
|
|
|
|
});
|
2017-03-11 23:21:08 +08:00
|
|
|
|
|
|
|
cluster.on('exit', worker => {
|
|
|
|
log.info('App', `Worker ${worker.process.pid} died`);
|
2017-09-04 18:19:52 +08:00
|
|
|
workers.delete(worker);
|
2017-03-11 23:21:08 +08:00
|
|
|
setTimeout(forkWorker, 1000);
|
2017-03-06 05:45:50 +08:00
|
|
|
});
|
2017-09-04 18:19:52 +08:00
|
|
|
|
|
|
|
config.on('reload', () => {
|
|
|
|
workers.forEach(child => {
|
|
|
|
try {
|
|
|
|
child.kill('SIGHUP');
|
|
|
|
} catch (E) {
|
|
|
|
//ignore
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-03-11 23:21:08 +08:00
|
|
|
} else {
|
|
|
|
if (config.ident) {
|
|
|
|
process.title = config.ident + ' worker';
|
|
|
|
}
|
|
|
|
|
|
|
|
require('./worker.js');
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 15:36:45 +08:00
|
|
|
|
|
|
|
process.on('unhandledRejection', err => {
|
2023-09-28 15:44:23 +08:00
|
|
|
log.error('App', 'Unhandled rejection: %s', (err && err.stack) || err);
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
});
|
2023-09-28 15:44:23 +08:00
|
|
|
|
2023-10-04 15:18:13 +08:00
|
|
|
process.on('SIGHUP', () => {
|
2023-09-28 15:44:23 +08:00
|
|
|
// generate memory dump
|
|
|
|
log.info('Process', 'PID=%s Generating heap snapshot...', process.pid);
|
|
|
|
let stream;
|
|
|
|
|
|
|
|
try {
|
|
|
|
stream = v8.getHeapSnapshot();
|
|
|
|
} catch (err) {
|
|
|
|
log.error('Process', 'PID=%s Failed to generate heap snapshot: %s', process.pid, err.stack || err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream) {
|
|
|
|
const path = Path.join(
|
|
|
|
os.tmpdir(),
|
|
|
|
`Heap-${process.pid}-${new Date()
|
|
|
|
.toISOString()
|
|
|
|
.substring(0, 19)
|
|
|
|
.replace(/[^0-9T]+/g, '')}.heapsnapshot`
|
|
|
|
);
|
|
|
|
|
|
|
|
let f;
|
|
|
|
try {
|
|
|
|
f = fs.createWriteStream(path);
|
|
|
|
} catch (err) {
|
|
|
|
log.error('Process', 'PID=%s Failed to generate heap snapshot: %s', process.pid, err.stack || err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
f.once('error', err => {
|
|
|
|
log.error('Process', 'PID=%s Failed to generate heap snapshot: %s', process.pid, err.stack || err);
|
|
|
|
});
|
|
|
|
stream.once('error', err => {
|
|
|
|
log.error('Process', 'PID=%s Failed to generate heap snapshot: %s', process.pid, err.stack || err);
|
|
|
|
});
|
|
|
|
stream.pipe(f);
|
|
|
|
f.once('finish', () => {
|
|
|
|
log.info('Process', 'PID=%s Generated heap snapshot: %s', process.pid, path);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|