2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2022-09-18 16:17:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Misskey Entry Point!
|
|
|
|
*/
|
|
|
|
|
2022-02-27 03:07:39 +01:00
|
|
|
import cluster from 'node:cluster';
|
2022-09-18 16:17:32 +02:00
|
|
|
import { EventEmitter } from 'node:events';
|
2022-02-27 03:07:39 +01:00
|
|
|
import chalk from 'chalk';
|
2022-04-17 07:42:13 +02:00
|
|
|
import Xev from 'xev';
|
2022-09-17 20:27:08 +02:00
|
|
|
import Logger from '@/logger.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { envOption } from '../env.js';
|
|
|
|
import { masterMain } from './master.js';
|
|
|
|
import { workerMain } from './worker.js';
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-09-18 16:17:32 +02:00
|
|
|
import 'reflect-metadata';
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-09-18 16:17:32 +02:00
|
|
|
process.title = `Misskey (${cluster.isPrimary ? 'master' : 'worker'})`;
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-09-18 16:17:32 +02:00
|
|
|
Error.stackTraceLimit = Infinity;
|
|
|
|
EventEmitter.defaultMaxListeners = 128;
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-09-18 16:17:32 +02:00
|
|
|
const logger = new Logger('core', 'cyan');
|
|
|
|
const clusterLogger = logger.createSubLogger('cluster', 'orange', false);
|
|
|
|
const ev = new Xev();
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2023-10-07 16:21:09 +02:00
|
|
|
// We wrap this in a main function, that gets called,
|
|
|
|
// because not all platforms support top level await :/
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
//#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
|
|
|
|
if (!envOption.quiet) {
|
|
|
|
process.on('unhandledRejection', console.dir);
|
|
|
|
}
|
2022-09-18 16:17:32 +02:00
|
|
|
|
2023-10-07 16:21:09 +02:00
|
|
|
// Display detail of uncaught exception
|
|
|
|
process.on('uncaughtException', err => {
|
|
|
|
try {
|
|
|
|
logger.error(err);
|
|
|
|
console.trace(err);
|
|
|
|
} catch { }
|
|
|
|
});
|
|
|
|
|
|
|
|
// Dying away...
|
|
|
|
process.on('exit', code => {
|
|
|
|
logger.info(`The process is going to exit with code ${code}`);
|
|
|
|
});
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
if (cluster.isPrimary || envOption.disableClustering) {
|
|
|
|
await masterMain();
|
|
|
|
if (cluster.isPrimary) {
|
|
|
|
ev.mount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cluster.isWorker || envOption.disableClustering) {
|
|
|
|
await workerMain();
|
2022-09-18 16:17:32 +02:00
|
|
|
}
|
|
|
|
|
2023-10-07 16:21:09 +02:00
|
|
|
// ユニットテスト時にMisskeyが子プロセスで起動された時のため
|
|
|
|
// それ以外のときは process.send は使えないので弾く
|
|
|
|
if (process.send) {
|
|
|
|
process.send('ok');
|
|
|
|
}
|
2022-09-18 16:17:32 +02:00
|
|
|
}
|
|
|
|
|
2023-10-07 16:21:09 +02:00
|
|
|
main();
|