BullMQ Plugin: error on application close
Describe the bug When performing either of the following:
-
app.close() - manual exit of process in terminal with
ctrl-c
BullMQ plugin is throwing the following error:
[run:server] TypeError: Cannot read property 'close' of undefined
[run:server] at BullMQJobQueueStrategy.destroy (.../@vendure/job-queue-plugin/src/bullmq/bullmq-job-queue-strategy.ts:88:60)
[run:server] at ConfigModule.destroyInjectableStrategies (.../@vendure/core/src/config/config.module.ts:40:32)
[run:server] at ConfigModule.onApplicationShutdown (.../@vendure/core/src/config/config.module.ts:24:20)
[run:server] at Object.callAppShutdownHook (.../node_modules/@nestjs/core/hooks/on-app-shutdown.hook.js:51:35)
[run:server] at processTicksAndRejections (...internal/process/task_queues.js:95:5)
[run:server] at NestApplication.callShutdownHook (.../node_modules/@nestjs/core/nest-application-context.js:196:13)
[run:server] at process.cleanup (.../node_modules/@nestjs/core/nest-application-context.js:134:17)
[run:server] yarn run run:server exited with code SIGINT
[run:worker] error 6/7/22, 11:09 am - [BullMQJobQueuePlugin] BullMQ Worker error: Connection is closed.
[run:worker] Error: Connection is closed.
[run:worker] at Redis.sendCommand (.../node_modules/bullmq/node_modules/ioredis/built/redis/index.js:636:24)
[run:worker] at Script.execute (.../node_modules/bullmq/node_modules/ioredis/built/script.js:27:34)
[run:worker] at Redis.moveToActive (.../node_modules/bullmq/node_modules/ioredis/built/commander.js:158:27)
[run:worker] at Scripts.moveToActive (.../node_modules/bullmq/src/classes/scripts.ts:728:40)
[run:worker] at Worker.moveToActive (.../node_modules/bullmq/src/classes/worker.ts:434:27)
To Reproduce With a very simple configuration of the BullMQ plugin:
BullMQJobQueuePlugin.init({
connection: {
host: process.env.REDIS_HOST, //127.0.0.1
port: process.env.REDIS_PORT, //6379
},
}),
running against a docker-image of redis with configuration as per documentation within vendure: https://www.vendure.io/docs/typescript-api/job-queue-plugin/bull-mqjob-queue-plugin/
having a snippet of code that executes
app.close() is enough locally to illicit the error above.
Expected behaviour Graceful shutdown of server/worker
Environment (please complete the following information):
- "@vendure/core": "^1.6.1",
- Nodejs version: v14.18.1
- "bullmq": "^1.86.2" (have also tried with ^1.40.1)
- "@vendure/job-queue-plugin": "^1.6.2",
face this issue two, while pm2 restart
I've been digging in to this and there seem to be a couple of issues at play.
The first relates to the destroy() hook, where we are making the assumption that the this.worker object will be defined.
https://github.com/vendure-ecommerce/vendure/blob/b72ae18d61c4e4f81f0dfab8b43b83cd1bf4810d/packages/job-queue-plugin/src/bullmq/bullmq-job-queue-strategy.ts#L87-L89
However, in the Server process, this will not be the case. The solution is quite simple - this.worker?.close().
The second issue is harder to track down, but may be related to:
- https://github.com/taskforcesh/bullmq/issues/1312
- https://github.com/taskforcesh/bullmq/issues/159
- https://github.com/taskforcesh/bullmq/pull/154