2023-08-15 02:52:38 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2022-09-18 23:17:32 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Misskey Entry Point!
|
|
|
|
*/
|
|
|
|
|
2022-02-27 11:07:39 +09:00
|
|
|
import cluster from 'node:cluster';
|
2022-09-18 23:17:32 +09:00
|
|
|
import { EventEmitter } from 'node:events';
|
2023-08-25 15:55:25 +09:00
|
|
|
import process from 'node:process';
|
2022-02-27 11:07:39 +09:00
|
|
|
import chalk from 'chalk';
|
2022-04-17 14:42:13 +09:00
|
|
|
import Xev from 'xev';
|
2022-09-18 03:27:08 +09:00
|
|
|
import Logger from '@/logger.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { envOption } from '../env.js';
|
|
|
|
import { masterMain } from './master.js';
|
|
|
|
import { workerMain } from './worker.js';
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-09-18 23:17:32 +09:00
|
|
|
import 'reflect-metadata';
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-09-18 23:17:32 +09:00
|
|
|
process.title = `Misskey (${cluster.isPrimary ? 'master' : 'worker'})`;
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-09-18 23:17:32 +09:00
|
|
|
Error.stackTraceLimit = Infinity;
|
|
|
|
EventEmitter.defaultMaxListeners = 128;
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-09-18 23:17:32 +09:00
|
|
|
const logger = new Logger('core', 'cyan');
|
|
|
|
const clusterLogger = logger.createSubLogger('cluster', 'orange', false);
|
|
|
|
const ev = new Xev();
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
//#region Events
|
|
|
|
|
2023-08-25 15:55:25 +09:00
|
|
|
let isShuttingDown = false;
|
|
|
|
|
2023-08-22 21:42:21 +09:00
|
|
|
if (cluster.isPrimary && !envOption.disableClustering) {
|
|
|
|
// 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
|
2023-08-25 15:55:25 +09:00
|
|
|
cluster.on('exit', (worker, code, signal) => {
|
2023-08-22 21:42:21 +09:00
|
|
|
// Replace the dead worker,
|
|
|
|
// we're not sentimental
|
2023-08-25 15:55:25 +09:00
|
|
|
clusterLogger.error(chalk.red(`[${worker.id}] died (${signal || code})`));
|
|
|
|
if (!isShuttingDown) cluster.fork();
|
|
|
|
else clusterLogger.info(chalk.yellow('Worker respawn disabled because of shutdown'));
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
logger.warn(chalk.yellow('Process received SIGINT'));
|
|
|
|
isShuttingDown = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
logger.warn(chalk.yellow('Process received SIGTERM'));
|
|
|
|
isShuttingDown = true;
|
2023-08-22 21:42:21 +09:00
|
|
|
});
|
|
|
|
}
|
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) {
|
2019-04-07 21:50:36 +09:00
|
|
|
process.on('unhandledRejection', console.dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display detail of uncaught exception
|
|
|
|
process.on('uncaughtException', err => {
|
2021-02-27 17:39:55 +09:00
|
|
|
try {
|
2024-01-16 02:29:53 +09:00
|
|
|
logger.error(`Uncaught exception: ${err.message}`, { error: err });
|
2021-02-27 17:39:55 +09:00
|
|
|
} catch { }
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// Dying away...
|
|
|
|
process.on('exit', code => {
|
2023-08-25 15:55:25 +09:00
|
|
|
logger.warn(chalk.yellow(`The process is going to exit with code ${code}`));
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('warning', warning => {
|
|
|
|
if ((warning as never)['code'] !== 'MISSKEY_SHUTDOWN') return;
|
|
|
|
logger.warn(chalk.yellow(`${warning.message}: ${(warning as never)['detail']}`));
|
|
|
|
for (const id in cluster.workers) cluster.workers[id]?.process.kill('SIGTERM');
|
|
|
|
process.exit();
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
//#endregion
|
2022-09-18 23:17:32 +09:00
|
|
|
|
|
|
|
if (cluster.isPrimary || envOption.disableClustering) {
|
|
|
|
await masterMain();
|
|
|
|
|
|
|
|
if (cluster.isPrimary) {
|
|
|
|
ev.mount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 08:13:22 +09:00
|
|
|
if (cluster.isWorker) {
|
2022-09-18 23:17:32 +09:00
|
|
|
await workerMain();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ユニットテスト時にMisskeyが子プロセスで起動された時のため
|
|
|
|
// それ以外のときは process.send は使えないので弾く
|
|
|
|
if (process.send) {
|
|
|
|
process.send('ok');
|
|
|
|
}
|