2019-04-07 12:50:36 +00:00
|
|
|
import * as cluster from 'cluster';
|
2019-11-24 08:09:32 +00:00
|
|
|
import * as chalk from 'chalk';
|
2019-04-07 12:50:36 +00:00
|
|
|
import Xev from 'xev';
|
|
|
|
|
2021-08-19 12:55:45 +00:00
|
|
|
import Logger from '@/services/logger';
|
2021-10-08 12:24:05 +00:00
|
|
|
import { envOption } from '../env';
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
// for typeorm
|
|
|
|
import 'reflect-metadata';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { masterMain } from './master';
|
|
|
|
import { workerMain } from './worker';
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
const logger = new Logger('core', 'cyan');
|
|
|
|
const clusterLogger = logger.createSubLogger('cluster', 'orange', false);
|
|
|
|
const ev = new Xev();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init process
|
|
|
|
*/
|
|
|
|
export default async function() {
|
|
|
|
process.title = `Misskey (${cluster.isMaster ? 'master' : 'worker'})`;
|
|
|
|
|
2021-10-08 12:24:05 +00:00
|
|
|
if (cluster.isMaster || envOption.disableClustering) {
|
2019-04-07 12:50:36 +00:00
|
|
|
await masterMain();
|
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
ev.mount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 12:24:05 +00:00
|
|
|
if (cluster.isWorker || envOption.disableClustering) {
|
2019-04-07 12:50:36 +00: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 12:24:05 +00:00
|
|
|
if (!envOption.quiet) {
|
2019-04-07 12:50:36 +00:00
|
|
|
process.on('unhandledRejection', console.dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display detail of uncaught exception
|
|
|
|
process.on('uncaughtException', err => {
|
2021-02-27 08:39:55 +00:00
|
|
|
try {
|
|
|
|
logger.error(err);
|
|
|
|
} catch { }
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Dying away...
|
|
|
|
process.on('exit', code => {
|
|
|
|
logger.info(`The process is going to exit with code ${code}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
//#endregion
|