server: fix restarted job retaining the right mode

When a web or queue worker exited unexpectedly, the new restarted worker would
not have any mode set and so would try to do web and queue worker stuff at the
same time, which was not the intended behaviour.

Changelog: Fixed
This commit is contained in:
Johann150 2024-04-01 17:55:13 +02:00
parent d293fc1dc7
commit 5444ca9aca
Signed by: Johann150
GPG key ID: 9EE6577A2A06F8F1
2 changed files with 4 additions and 9 deletions

View file

@ -1,5 +1,4 @@
import cluster from 'node:cluster';
import chalk from 'chalk';
import Xev from 'xev';
import Logger from '@/services/logger.js';
@ -57,14 +56,6 @@ 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.logLevel !== LOG_LEVELS.quiet) {
process.on('unhandledRejection', console.dir);

View file

@ -168,6 +168,10 @@ async function spawnWorkers(clusterLimits: Required<Config['clusterLimits']>): P
function spawnWorker(mode: 'web' | 'queue'): Promise<void> {
return new Promise(res => {
const worker = cluster.fork({ mode });
worker.on('exit', async (code, signal) => {
logger.error(mode + ' worker died, restarting...');
await spawnWorker(mode);
});
worker.on('message', message => {
switch (message) {
case 'listenFailed':