-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Unify application components shutdown (#8097)
## Summary Add `ShutdownService` and `OnShutdown` decorator for more unified way to shutdown different components. Use this new way in the following components: - HTTP(S) server - Pruning service - Push connection - License --------- Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
- Loading branch information
Showing
15 changed files
with
412 additions
and
17 deletions.
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
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
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,38 @@ | ||
import { Container } from 'typedi'; | ||
import { ApplicationError } from 'n8n-workflow'; | ||
import { type ServiceClass, ShutdownService } from '@/shutdown/Shutdown.service'; | ||
|
||
/** | ||
* Decorator that registers a method as a shutdown hook. The method will | ||
* be called when the application is shutting down. | ||
* | ||
* Priority is used to determine the order in which the hooks are called. | ||
* | ||
* NOTE: Requires also @Service() decorator to be used on the class. | ||
* | ||
* @example | ||
* ```ts | ||
* @Service() | ||
* class MyClass { | ||
* @OnShutdown() | ||
* async shutdown() { | ||
* // Will be called when the app is shutting down | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
export const OnShutdown = | ||
(priority = 100): MethodDecorator => | ||
(prototype, propertyKey, descriptor) => { | ||
const serviceClass = prototype.constructor as ServiceClass; | ||
const methodName = String(propertyKey); | ||
// TODO: assert that serviceClass is decorated with @Service | ||
if (typeof descriptor?.value === 'function') { | ||
Container.get(ShutdownService).register(priority, { serviceClass, methodName }); | ||
} else { | ||
const name = `${serviceClass.name}.${methodName}()`; | ||
throw new ApplicationError( | ||
`${name} must be a method on ${serviceClass.name} to use "OnShutdown"`, | ||
); | ||
} | ||
}; |
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
Oops, something went wrong.