FoundKey/packages/backend/src/boot/index.ts
Johann150 095802d059
Some checks failed
ci/woodpecker/push/lint-client Pipeline failed
ci/woodpecker/push/lint-backend Pipeline failed
ci/woodpecker/push/lint-sw Pipeline failed
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/test Pipeline failed
server: remove unused logger.store attribute
2024-04-01 21:25:14 +02:00

79 lines
1.8 KiB
TypeScript

import cluster from 'node:cluster';
import Xev from 'xev';
import Logger from '@/services/logger.js';
import { envOption, LOG_LEVELS } from '@/env.js';
// for typeorm
import 'reflect-metadata';
import { masterMain } from './master.js';
import { workerMain } from './worker.js';
const logger = new Logger('core');
const clusterLogger = logger.createSubLogger('cluster');
const ev = new Xev();
/**
* Init process
*/
export async function boot(): Promise<void> {
if (envOption.disableClustering) {
process.title = "Foundkey";
} else if (cluster.isPrimary) {
process.title = "Foundkey (master)";
} else if (cluster.isWorker) {
process.title = `Foundkey (${process.env.mode})`;
}
if (cluster.isPrimary || envOption.disableClustering) {
await masterMain();
if (cluster.isPrimary) {
ev.mount();
}
}
if (cluster.isWorker || envOption.disableClustering) {
await workerMain();
}
// for when FoundKey is launched as a child process during unit testing
// otherwise, process.send cannot be used, so it is suppressed
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}]`);
});
// Display detail of unhandled promise rejection
if (envOption.logLevel !== LOG_LEVELS.quiet) {
process.on('unhandledRejection', console.dir);
}
// Display detail of uncaught exception
process.on('uncaughtException', err => {
try {
logger.error(err);
} catch {
// if that fails too there is nothing we can do
}
});
// Dying away...
process.on('exit', code => {
logger.info(`The process is going to exit with code ${code}`);
});
//#endregion