2022-02-27 11:07:39 +09:00
|
|
|
import cluster from 'node:cluster';
|
|
|
|
import chalk from 'chalk';
|
2022-04-17 14:42:13 +09:00
|
|
|
import Xev from 'xev';
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-02-27 11:07:39 +09:00
|
|
|
import Logger from '@/services/logger.js';
|
|
|
|
import { envOption } from '../env.js';
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
// for typeorm
|
|
|
|
import 'reflect-metadata';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { masterMain } from './master.js';
|
|
|
|
import { workerMain } from './worker.js';
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
const logger = new Logger('core', 'cyan');
|
|
|
|
const clusterLogger = logger.createSubLogger('cluster', 'orange', false);
|
2022-04-17 14:42:13 +09:00
|
|
|
const ev = new Xev();
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init process
|
|
|
|
*/
|
|
|
|
export default async function() {
|
2022-12-16 09:09:00 +09:00
|
|
|
process.title = `Calckey (${cluster.isPrimary ? 'master' : 'worker'})`;
|
2022-12-16 07:13:48 +09:00
|
|
|
|
2022-12-16 09:09:00 +09:00
|
|
|
if (cluster.isPrimary || envOption.disableClustering) {
|
|
|
|
await masterMain();
|
2022-12-16 07:13:48 +09:00
|
|
|
|
2022-12-16 09:09:00 +09:00
|
|
|
if (cluster.isPrimary) {
|
|
|
|
ev.mount();
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 07:13:48 +09:00
|
|
|
|
2022-12-16 09:09:00 +09:00
|
|
|
if (cluster.isWorker || envOption.disableClustering) {
|
|
|
|
await workerMain();
|
|
|
|
}
|
2022-12-16 07:13:48 +09:00
|
|
|
|
2022-12-16 09:09:00 +09:00
|
|
|
// For when Calckey is started in a child process during unit testing.
|
|
|
|
// Otherwise, process.send cannot be used, so start it.
|
|
|
|
if (process.send) {
|
|
|
|
process.send('ok');
|
|
|
|
}
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
//#region Events
|
|
|
|
|
|
|
|
// Listen new workers
|
|
|
|
cluster.on('fork', worker => {
|
2022-12-16 09:09:00 +09:00
|
|
|
clusterLogger.debug(`Process forked: [${worker.id}]`);
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// Listen online workers
|
|
|
|
cluster.on('online', worker => {
|
2022-12-16 09:09:00 +09:00
|
|
|
clusterLogger.debug(`Process is now online: [${worker.id}]`);
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for dying workers
|
|
|
|
cluster.on('exit', worker => {
|
2022-12-16 09:09:00 +09:00
|
|
|
// Replace the dead worker,
|
|
|
|
// we're not sentimental
|
|
|
|
clusterLogger.error(chalk.red(`[${worker.id}] died :(`));
|
|
|
|
cluster.fork();
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// Display detail of unhandled promise rejection
|
2021-10-08 21:24:05 +09:00
|
|
|
if (!envOption.quiet) {
|
2022-12-16 09:09:00 +09:00
|
|
|
process.on('unhandledRejection', console.dir);
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display detail of uncaught exception
|
|
|
|
process.on('uncaughtException', err => {
|
2022-12-16 09:09:00 +09:00
|
|
|
try {
|
|
|
|
logger.error(err);
|
|
|
|
} catch { }
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// Dying away...
|
|
|
|
process.on('exit', code => {
|
2022-12-16 09:09:00 +09:00
|
|
|
logger.info(`The process is going to exit with code ${code}`);
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
//#endregion
|