refactor(backend): simplify boot

This commit is contained in:
syuilo 2022-09-18 23:17:32 +09:00
parent 0abecffa8b
commit a66111ac1f
4 changed files with 34 additions and 45 deletions

View File

@ -10,8 +10,8 @@
"scripts": { "scripts": {
"postinstall": "node ./scripts/install-packages.js", "postinstall": "node ./scripts/install-packages.js",
"build": "node ./scripts/build.js", "build": "node ./scripts/build.js",
"start": "cd packages/backend && node --experimental-json-modules ./built/index.js", "start": "cd packages/backend && node --experimental-json-modules ./built/boot/index.js",
"start:test": "cd packages/backend && cross-env NODE_ENV=test node --experimental-json-modules ./built/index.js", "start:test": "cd packages/backend && cross-env NODE_ENV=test node --experimental-json-modules ./built/boot/index.js",
"init": "npm run migrate", "init": "npm run migrate",
"migrate": "cd packages/backend && npx typeorm migration:run -d ormconfig.js", "migrate": "cd packages/backend && npx typeorm migration:run -d ormconfig.js",
"migrateandstart": "npm run migrate && npm run start", "migrateandstart": "npm run migrate && npm run start",

View File

@ -1,44 +1,28 @@
/**
* Misskey Entry Point!
*/
import cluster from 'node:cluster'; import cluster from 'node:cluster';
import { EventEmitter } from 'node:events';
import chalk from 'chalk'; import chalk from 'chalk';
import Xev from 'xev'; import Xev from 'xev';
import Logger from '@/logger.js'; import Logger from '@/logger.js';
import { envOption } from '../env.js'; import { envOption } from '../env.js';
// for typeorm
import 'reflect-metadata';
import { masterMain } from './master.js'; import { masterMain } from './master.js';
import { workerMain } from './worker.js'; import { workerMain } from './worker.js';
import 'reflect-metadata';
process.title = `Misskey (${cluster.isPrimary ? 'master' : 'worker'})`;
Error.stackTraceLimit = Infinity;
EventEmitter.defaultMaxListeners = 128;
const logger = new Logger('core', 'cyan'); const logger = new Logger('core', 'cyan');
const clusterLogger = logger.createSubLogger('cluster', 'orange', false); const clusterLogger = logger.createSubLogger('cluster', 'orange', false);
const ev = new Xev(); const ev = new Xev();
/**
* Init process
*/
export default async function() {
process.title = `Misskey (${cluster.isPrimary ? 'master' : 'worker'})`;
if (cluster.isPrimary || envOption.disableClustering) {
await masterMain();
if (cluster.isPrimary) {
ev.mount();
}
}
if (cluster.isWorker || envOption.disableClustering) {
await workerMain();
}
// ユニットテスト時にMisskeyが子プロセスで起動された時のため
// それ以外のときは process.send は使えないので弾く
if (process.send) {
process.send('ok');
}
}
//#region Events //#region Events
// Listen new workers // Listen new workers
@ -77,3 +61,21 @@ process.on('exit', code => {
}); });
//#endregion //#endregion
if (cluster.isPrimary || envOption.disableClustering) {
await masterMain();
if (cluster.isPrimary) {
ev.mount();
}
}
if (cluster.isWorker || envOption.disableClustering) {
await workerMain();
}
// ユニットテスト時にMisskeyが子プロセスで起動された時のため
// それ以外のときは process.send は使えないので弾く
if (process.send) {
process.send('ok');
}

View File

@ -1,13 +0,0 @@
/**
* Misskey Entry Point!
*/
import { EventEmitter } from 'node:events';
import boot from './boot/index.js';
Error.stackTraceLimit = Infinity;
EventEmitter.defaultMaxListeners = 128;
boot().catch(err => {
console.error(err);
});

View File

@ -10,7 +10,7 @@
"declaration": false, "declaration": false,
"sourceMap": false, "sourceMap": false,
"target": "es2021", "target": "es2021",
"module": "es2020", "module": "es2022",
"moduleResolution": "node", "moduleResolution": "node",
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"removeComments": false, "removeComments": false,