Skip to content

Commit

Permalink
use correct exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus-Rost committed Jul 20, 2024
1 parent d78ff8d commit 72c51b4
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ manager.on( 'shardCreate', shard => {
}
if ( message === 'SIGKILL' ) {
console.log( '\n- Killing all shards!\n\n' );
graceful('SIGKILL');
manager.shards.filter( shard => shard.process && !shard.process.killed ).forEach( shard => shard.kill() );
if ( typeof server !== 'undefined' && !server.killed ) server.kill();
}
Expand All @@ -75,6 +76,12 @@ manager.on( 'shardCreate', shard => {
console.log( `\n\n- Shard[${shard.id}]: Died due to fatal error, disable respawn!\n\n` );
}
else console.log( `\n\n- Shard[${shard.id}]: Died due to fatal error!\n\n` );
if ( isDebug || diedShards >= manager.totalShards ) {
graceful('SIGKILL');
manager.shards.filter( leftShard => leftShard !== shard && leftShard.process && !leftShard.process.killed ).forEach( leftShard => leftShard.kill() );
if ( typeof server !== 'undefined' && !server.killed ) server.kill();
process.exit(1);
};
}
} );

Expand All @@ -100,7 +107,7 @@ manager.spawn( {
console.error( '- Error while spawning the shards: ' + error );
manager.shards.filter( shard => shard.process && !shard.process.killed ).forEach( shard => shard.kill() );
if ( isDebug ) {
manager.respawn = false;
graceful('SIGKILL');
if ( typeof server !== 'undefined' && !server.killed ) server.kill();
process.exit(1);
}
Expand All @@ -109,7 +116,7 @@ manager.spawn( {
timeout: 90_000
} ).catch( error2 => {
console.error( '- Error while spawning the shards: ' + error2 );
manager.respawn = false;
graceful('SIGKILL');
manager.shards.filter( shard => shard.process && !shard.process.killed ).forEach( shard => shard.kill() );
if ( typeof server !== 'undefined' && !server.killed ) server.kill();
process.exit(1);
Expand Down Expand Up @@ -412,6 +419,7 @@ if ( process.env.dashboard ) {
dashboard.on( 'exit', (code) => {
if ( code ) console.log( '- [Dashboard]: Process exited!', code );
if ( isDebug ) {
graceful('SIGKILL');
manager.shards.filter( shard => shard.process && !shard.process.killed ).forEach( shard => shard.kill() );
process.exit(1);
}
Expand Down Expand Up @@ -496,17 +504,13 @@ function graceful(signal) {
process.once( 'SIGINT', graceful );
process.once( 'SIGTERM', graceful );

process.on( 'exit', code => {
if ( diedShards >= manager.totalShards ) process.exit(1);
} );

if ( isDebug && process.argv[3]?.startsWith( '--timeout:' ) ) {
let timeout = process.argv[3].split(':')[1];
console.log( `\n- Close process in ${timeout} seconds!\n` );
setTimeout( () => {
console.log( `\n- Running for ${timeout} seconds, closing process!\n` );
isDebug = false;
graceful('SIGTERM');
graceful('SIGKILL');
manager.shards.filter( shard => shard.process && !shard.process.killed ).forEach( shard => shard.kill() );
if ( typeof server !== 'undefined' && !server.killed ) server.kill();
}, timeout * 1_000 ).unref();
Expand Down

0 comments on commit 72c51b4

Please sign in to comment.