-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: graceful shutdown timeout #2422
Merged
BrunnerLivio
merged 10 commits into
nestjs:master
from
Lp-Francois:feat-graceful-shutdown-timeout
Nov 26, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5528e7d
feat: add a sleep utility
Lp-Francois a4c826f
docs: add graceful shutdown timeout to the readme.md
Lp-Francois dd71df6
feat: add graceful-shutdown timeout service with tests
Lp-Francois 256507d
refactor: use variable for TERMINUS_GRACEFUL_SHUTDOWN_TIMEOUT
Lp-Francois a3f5d34
refactor: better documentation
Lp-Francois fc9928a
refactor: better documentation
Lp-Francois 99a42dc
refactor: better documentation
Lp-Francois 69d9205
refactor: rename GracefulShutdownTimeoutService to GracefulShutdownS…
Lp-Francois e3b281d
refactor: rename gracefulshutdownservice
Lp-Francois b6d38bb
chore: add publicapi tag to terminus module options jsdoc
Lp-Francois File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BrunnerLivio you can just:
see: https://nodejs.org/api/timers.html#timerspromisessettimeoutdelay-value-options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BrunnerLivio sorry, wrong mention... the author of the PR is @Lp-Francois
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL, didn't know this API exist :)