2022-02-26 18:07:39 -08:00
|
|
|
import cluster from 'node:cluster';
|
|
|
|
import chalk from 'chalk';
|
2022-04-16 22:42:13 -07:00
|
|
|
import Xev from 'xev';
|
2019-04-07 05:50:36 -07:00
|
|
|
|
2022-02-26 18:07:39 -08:00
|
|
|
import Logger from '@/services/logger.js';
|
|
|
|
import { envOption } from '../env.js';
|
2019-04-07 05:50:36 -07:00
|
|
|
|
|
|
|
// for typeorm
|
|
|
|
import 'reflect-metadata';
|
2022-02-26 18:07:39 -08:00
|
|
|
import { masterMain } from './master.js';
|
|
|
|
import { workerMain } from './worker.js';
|
2019-04-07 05:50:36 -07:00
|
|
|
|
|
|
|
const logger = new Logger('core', 'cyan');
|
|
|
|
const clusterLogger = logger.createSubLogger('cluster', 'orange', false);
|
2022-04-16 22:42:13 -07:00
|
|
|
const ev = new Xev();
|
2019-04-07 05:50:36 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init process
|
|
|
|
*/
|
|
|
|
export default async function() {
|
2021-11-11 09:02:25 -08:00
|
|
|
process.title = `Misskey (${cluster.isPrimary ? 'master' : 'worker'})`;
|
2019-04-07 05:50:36 -07:00
|
|
|
|
2021-11-11 09:02:25 -08:00
|
|
|
if (cluster.isPrimary || envOption.disableClustering) {
|
2019-04-07 05:50:36 -07:00
|
|
|
await masterMain();
|
|
|
|
|
2021-11-11 09:02:25 -08:00
|
|
|
if (cluster.isPrimary) {
|
2019-04-07 05:50:36 -07:00
|
|
|
ev.mount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 05:24:05 -07:00
|
|
|
if (cluster.isWorker || envOption.disableClustering) {
|
2019-04-07 05:50:36 -07:00
|
|
|
await workerMain();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ユニットテスト時にMisskeyが子プロセスで起動された時のため
|
|
|
|
// それ以外のときは process.send は使えないので弾く
|
|
|
|
if (process.send) {
|
|
|
|
process.send('ok');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//#region Events
|
|
|
|
|
|
|
|
// Listen new workers
|
|
|
|
cluster.on('fork', worker => {
|
|
|
|
clusterLogger.debug(`Process forked: [${worker.id}]`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Listen online workers
|
|
|
|
cluster.on('online', worker => {
|
|
|
|
clusterLogger.debug(`Process is now online: [${worker.id}]`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for dying workers
|
|
|
|
cluster.on('exit', worker => {
|
|
|
|
// Replace the dead worker,
|
|
|
|
// we're not sentimental
|
|
|
|
clusterLogger.error(chalk.red(`[${worker.id}] died :(`));
|
|
|
|
cluster.fork();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Display detail of unhandled promise rejection
|
2021-10-08 05:24:05 -07:00
|
|
|
if (!envOption.quiet) {
|
2019-04-07 05:50:36 -07:00
|
|
|
process.on('unhandledRejection', console.dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display detail of uncaught exception
|
|
|
|
process.on('uncaughtException', err => {
|
2021-02-27 00:39:55 -08:00
|
|
|
try {
|
|
|
|
logger.error(err);
|
|
|
|
} catch { }
|
2019-04-07 05:50:36 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// Dying away...
|
|
|
|
process.on('exit', code => {
|
|
|
|
logger.info(`The process is going to exit with code ${code}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
//#endregion
|