felanios / murlock

MurLock: A distributed locking solution for NestJS, providing a decorator for critical sections with Redis-based synchronization. Ideal for microservices and scalable applications.
https://www.npmjs.com/package/murlock
MIT License
62 stars 1 forks source link

[BUG] onModuleInit and onApplicationShutdown called twice #29

Closed Scoup closed 6 months ago

Scoup commented 7 months ago

The MurlockService is registred twice (https://github.com/felanios/murlock/blob/master/lib/murlock.module.ts#L29-L36) and this makes the onModuleInit and onApplicationShutdown be triggered twice. Since the onApplicationShutdown tries to close Redis twice (with the same instance) the last one creates an error that breaks the tests.

⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯
Error: The client is closed
 ❯ RedisSocket.quit node_modules/murlock/node_modules/@redis/client/dist/lib/client/socket.js:70:19
 ❯ Commander.QUIT node_modules/murlock/node_modules/@redis/client/dist/lib/client/index.js:260:71
 ❯ MurLockService.onApplicationShutdown node_modules/murlock/lib/murlock.service.ts:40:23
 ❯ MapIterator.iteratee node_modules/@nestjs/core/hooks/on-app-shutdown.hook.js:22:43
 ❯ MapIterator.next node_modules/iterare/src/map.ts:9:39
 ❯ IteratorWithOperators.next node_modules/iterare/src/iterate.ts:19:28
 ❯ IteratorWithOperators.toArray node_modules/iterare/src/iterate.ts:227:22
 ❯ callOperator node_modules/@nestjs/core/hooks/on-app-shutdown.hook.js:23:10
 ❯ callAppShutdownHook node_modules/@nestjs/core/hooks/on-app-shutdown.hook.js:43:23
felanios commented 6 months ago

Hi @Scoup, thank you four your contribution. I merged the pr, you can check the latest version https://www.npmjs.com/package/murlock/v/3.0.2