-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: graceful shutdown timeout (#2422)
Adds a new configuration option for the `TerminsModule.forRoot()` function that allows specifying a sleep timeout before gracefully shutting down. ```typescript Terminus.forRoot({ gracefulShutdownTimeoutMs: 1000 }) ```
- Loading branch information
1 parent
be39ada
commit cc3d402
Showing
7 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
lib/graceful-shutdown-timeout/graceful-shutdown-timeout.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { LoggerService } from '@nestjs/common'; | ||
import { | ||
GracefulShutdownService, | ||
TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT, | ||
} from './graceful-shutdown-timeout.service'; | ||
import { TERMINUS_LOGGER } from '../health-check/logger/logger.provider'; | ||
import { sleep } from '../utils'; | ||
|
||
jest.mock('../utils', () => ({ | ||
sleep: jest.fn(), | ||
})); | ||
|
||
const loggerMock: Partial<LoggerService> = { | ||
log: jest.fn(), | ||
error: jest.fn(), | ||
warn: jest.fn(), | ||
debug: jest.fn(), | ||
}; | ||
|
||
describe('GracefulShutdownService', () => { | ||
let service: GracefulShutdownService; | ||
let logger: LoggerService; | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
providers: [ | ||
GracefulShutdownService, | ||
{ provide: TERMINUS_LOGGER, useValue: loggerMock }, | ||
{ provide: TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT, useValue: 1000 }, | ||
], | ||
}).compile(); | ||
|
||
logger = module.get(TERMINUS_LOGGER); | ||
service = module.get(GracefulShutdownService); | ||
}); | ||
|
||
it('should not trigger sleep if signal is not SIGTERM', async () => { | ||
await service.beforeApplicationShutdown('SIGINT'); | ||
expect(sleep).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should trigger sleep if signal is SIGTERM', async () => { | ||
await service.beforeApplicationShutdown('SIGTERM'); | ||
expect(sleep).toHaveBeenCalledWith(1000); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
lib/graceful-shutdown-timeout/graceful-shutdown-timeout.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
type BeforeApplicationShutdown, | ||
ConsoleLogger, | ||
Inject, | ||
Injectable, | ||
LoggerService, | ||
} from '@nestjs/common'; | ||
import { TERMINUS_LOGGER } from '../health-check/logger/logger.provider'; | ||
import { sleep } from '../utils'; | ||
|
||
export const TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT = | ||
'TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT'; | ||
|
||
/** | ||
* Handles Graceful shutdown timeout useful to await | ||
* for some time before the application shuts down. | ||
*/ | ||
@Injectable() | ||
export class GracefulShutdownService implements BeforeApplicationShutdown { | ||
constructor( | ||
@Inject(TERMINUS_LOGGER) | ||
private readonly logger: LoggerService, | ||
@Inject(TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT) | ||
private readonly gracefulShutdownTimeoutMs: number, | ||
) { | ||
if (this.logger instanceof ConsoleLogger) { | ||
this.logger.setContext(GracefulShutdownService.name); | ||
} | ||
} | ||
|
||
async beforeApplicationShutdown(signal: string) { | ||
this.logger.log(`Received termination signal ${signal}`); | ||
|
||
if (signal === 'SIGTERM') { | ||
this.logger.log( | ||
`Awaiting ${this.gracefulShutdownTimeoutMs}ms before shutdown`, | ||
); | ||
await sleep(this.gracefulShutdownTimeoutMs); | ||
this.logger.log(`Timeout reached, shutting down now`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const sleep = (ms: number) => | ||
new Promise((resolve) => setTimeout(resolve, ms)); |