@piggly/fastify-chassis
Version:
An ESM/CommonJS toolkit to help you to do common operations in your back-end applications with Fastify and NodeJS.
42 lines • 1.32 kB
JavaScript
import { ServiceProvider, LoggerService } from '@piggly/ddd-toolkit';
import EventBus from '@piggly/event-bus';
import debug from 'debug';
/**
* Cleanup dependencies.
* It will run the CleanUpService to clean all registered dependencies.
*
* It will wait for:
* - EventBus cleanup;
* - LoggerService flush and cleanup.
*
* You may not need to cleanup classes above when using this.
*
* @param {HttpServerInterface<Server, AppEnvironment> | undefined} server
* @returns {Promise<number>}
* @since 5.5.0
* @author Caique Araujo <caique@piggly.com.br>
*/
export const cleanupDependencies = (server) => async () => {
try {
if (server) {
await server.stop();
}
const service = ServiceProvider.get('CleanUpService');
if (service) {
const response = await service.softClean();
return response.success ? 0 : 1;
}
// Register the EventBus cleanup.
await EventBus.instance.cleanup();
LoggerService.softResolve().flush();
await LoggerService.softResolve().cleanup();
return 0;
}
catch (error) {
/* eslint-disable-next-line no-console */
console.error(error);
debug('app:cleanup:error')(error);
return 1;
}
};
//# sourceMappingURL=cleanupDependencies.js.map