From 049095348ef5310d613779c150613a4304b14293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Moreno?= Date: Sat, 16 Nov 2024 15:43:53 -0400 Subject: [PATCH] feat(ServiceBase): add stopAPI and stopRPCs methods --- src/ServiceBase.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/ServiceBase.ts b/src/ServiceBase.ts index b49deb7..2cf5aa1 100644 --- a/src/ServiceBase.ts +++ b/src/ServiceBase.ts @@ -50,6 +50,8 @@ export default class ServiceBase { protected tasks: any; protected routes: any; protected rpcs: any; + private apiServer: any; + private rpcServer: any; constructor(opts?: ServiceOptions) { const conf = _.get(opts, 'configuration', null); @@ -132,15 +134,33 @@ export default class ServiceBase { } async startAPI() { - await this.apiApp.listen(this.resources.configuration.api.port); + this.apiServer = await this.apiApp.listen(this.resources.configuration.api.port); this.resources.logger.info('API started on port', this.resources.configuration.api.port); } + async stopAPI() { + if (this.apiServer) { + await this.apiServer.close(); + this.resources.logger.info('API stopped on port', this.resources.configuration.api.port); + } else { + this.resources.logger.warn('API server is not running'); + } + } + async startRPCs() { - await this.rpcApp.listen(this.resources.configuration.rpc.port); + this.rpcServer = await this.rpcApp.listen(this.resources.configuration.rpc.port); this.resources.logger.info('RPCs started on port', this.resources.configuration.rpc.port); } + async stopRPCs() { + if (this.rpcServer) { + await this.rpcServer.close(); + this.resources.logger.info('RPCs stopped on port', this.resources.configuration.rpc.port); + } else { + this.resources.logger.warn('RPC server is not running'); + } + } + async initAPI() { await this.initAPIRoutes(); await this.startAPI();