Merge pull request #15986 from mag123c/fix/shutdown-hooks-process-exit

feat(core): add option for async logger compatibility
This commit is contained in:
Kamil Mysliwiec
2025-12-05 13:00:52 +01:00
committed by GitHub
7 changed files with 124 additions and 8 deletions

View File

@@ -51,4 +51,21 @@ describe('enableShutdownHooks', () => {
expect(result.stdout.toString().trim()).to.be.eq('');
done();
}).timeout(10000);
it('should call the correct hooks with useProcessExit option', done => {
const result = spawnSync('ts-node', [
join(__dirname, '../src/enable-shutdown-hooks-main.ts'),
'SIGHUP',
'SIGHUP',
'graceful',
]);
const calls = result.stdout
.toString()
.split('\n')
.map((call: string) => call.trim());
expect(calls[0]).to.equal('beforeApplicationShutdown SIGHUP');
expect(calls[1]).to.equal('onApplicationShutdown SIGHUP');
expect(result.status).to.equal(0);
done();
}).timeout(10000);
});

View File

@@ -7,6 +7,7 @@ import {
import { NestFactory } from '@nestjs/core';
const SIGNAL = process.argv[2];
const SIGNAL_TO_LISTEN = process.argv[3];
const USE_GRACEFUL_EXIT = process.argv[4] === 'graceful';
@Injectable()
class TestInjectable
@@ -29,10 +30,12 @@ class AppModule {}
async function bootstrap() {
const app = await NestFactory.create(AppModule, { logger: false });
const shutdownOptions = USE_GRACEFUL_EXIT ? { useProcessExit: true } : {};
if (SIGNAL_TO_LISTEN && SIGNAL_TO_LISTEN !== 'NONE') {
app.enableShutdownHooks([SIGNAL_TO_LISTEN]);
app.enableShutdownHooks([SIGNAL_TO_LISTEN], shutdownOptions);
} else if (SIGNAL_TO_LISTEN !== 'NONE') {
app.enableShutdownHooks();
app.enableShutdownHooks([], shutdownOptions);
}
await app.listen(1800);