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>
This commit is contained in:
@@ -94,4 +94,17 @@ export abstract class AbstractPush<T> extends EventEmitter {
|
||||
|
||||
this.sendToSessions(type, data, userSessionIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes all push existing connections
|
||||
*/
|
||||
closeAllConnections() {
|
||||
for (const sessionId in this.connections) {
|
||||
// Signal the connection that we want to close it.
|
||||
// We are not removing the sessions here because it should be
|
||||
// the implementation's responsibility to do so once the connection
|
||||
// has actually closed.
|
||||
this.close(this.connections[sessionId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import { WebSocketPush } from './websocket.push';
|
||||
import type { PushResponse, SSEPushRequest, WebSocketPushRequest } from './types';
|
||||
import type { IPushDataType } from '@/Interfaces';
|
||||
import type { User } from '@db/entities/User';
|
||||
import { OnShutdown } from '@/decorators/OnShutdown';
|
||||
|
||||
const useWebSockets = config.getEnv('push.backend') === 'websocket';
|
||||
|
||||
@@ -70,6 +71,11 @@ export class Push extends EventEmitter {
|
||||
sendToUsers<D>(type: IPushDataType, data: D, userIds: Array<User['id']>) {
|
||||
this.backend.sendToUsers(type, data, userIds);
|
||||
}
|
||||
|
||||
@OnShutdown()
|
||||
onShutdown(): void {
|
||||
this.backend.closeAllConnections();
|
||||
}
|
||||
}
|
||||
|
||||
export const setupPushServer = (restEndpoint: string, server: Server, app: Application) => {
|
||||
|
||||
Reference in New Issue
Block a user