Problem
Even after calling await api.fail("Error: Some Error which needs to be stored in the error field of the doc")
, it's failing the job but not logging the error in the DB.
However, when the max retries count reached the limit, it call the
api.fail()
and the job is marked as dead with error({"type":"MaxAttemptsExceededError","name":"Error","message":"Exceeded the maximum number of retries (0) for this job","stack":"Error: Exceeded the maximum number of retries (0) for this job\n at MongoDriver.dead (D:\\Abhishek Jadhav\\Codes\\Blockchain\\gate\\node_modules\\docmq\\src\\driver\\mongo.ts:487:17)\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async D:\\Abhishek Jadhav\\Codes\\Blockchain\\gate\\node_modules\\docmq\\src\\worker.ts:251:11\n at async D:\\Abhishek Jadhav\\Codes\\Blockchain\\gate\\node_modules\\docmq\\src\\driver\\mongo.ts:307:7\n at async MongoDriver.transaction (D:\\Abhishek Jadhav\\Codes\\Blockchain\\gate\\node_modules\\docmq\\src\\driver\\mongo.ts:306:5)\n at async Worker.processOne (D:\\Abhishek Jadhav\\Codes\\Blockchain\\gate\\node_modules\\docmq\\src\\worker.ts:249:9)\n at async D:\\Abhishek Jadhav\\Codes\\Blockchain\\gate\\node_modules\\docmq\\src\\queue.ts:440:13"})
stored in error field in the DB.
I'm working with MongoDriver option for storing the jobs data.
Code
async function paymentJobHandler(job: PaymentRequestJob | PaymentLogJob, api: HandlerApi) {
try {
....
if (isExist) {
await api.fail(type === "Log" ? "Duplicate: Payment already exists" : "Error: Payment log already exists", { attempt: 1 }); //! This is causing the job to fail, but the error is not getting logged in the database.
return;
}
.....
}
async function queuePayment(data: PaymentRequestJob) {
await paymentQueue.enqueue({
ref: data.hash,
payload: data,
retries: 0,
});
}
async function initPaymentJob(driver: MongoDriver) {
connection = mongoose.connection.db.collection("payment_jobs");
paymentQueue = new Queue<PaymentRequestJob | PaymentLogJob>(driver, QUEUES.PAYMENTS, { retention: { jobs: 60 * 60 * 24 * 30 }, });
await paymentQueue.ready().catch((err) => {
console.error("❗ Failed to initialize payment queue", err);
process.exit(1);
});
paymentQueue.process(paymentJobHandler);
paymentQueue.events.on("ack", (context) => {
console.log(` Payment job ${context.ref} acknowledged with payload ${context.payload.hash}`);
});
paymentQueue.events.on("fail", (info) => {
console.error(
`❗ Payment job ${info.ref} failed with error: ${info.error?.message}`,
);
});
paymentQueue.events.on("dead", (info) => {
console.error(
`❗ Payment job ${info.ref} is dead with error: ${info.error?.message}`,
);
});
paymentQueue.events.on("halt", () => {
console.error("❌ Received HALT from payment queue");
process.exit(1);
});
return paymentQueue;
}
Console
❗ Payment job 0x7a1357d86cc6b3999e failed with error: Error: Payment log already exists
❗ Payment job 0x7a1357d86cc6b3999e is dead with error: Exceeded the maximum number of retries (0) for this job
Environment
- OS - Windows 11 Home 22H2 AMD Ryzen 5 4600H
- Node - v18.17.0
package.json
{
"dependencies": {
"@aws-sdk/client-ses": "^3.387.0",
"cors": "^2.8.5",
"docmq": "^0.5.6",
"ethers": "^5.7.2",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.1",
"mongodb": "^4.7.0",
"mongoose": "^7.4.1",
"redis": "^4.6.7",
"uuid": "^9.0.0"
},
"devDependencies": {
"@typechain/ethers-v5": "^11.1.1",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^20.4.6",
"@types/uuid": "^9.0.2",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"env-cmd": "^10.1.0",
"eslint": "^8.46.0",
"eslint-config-prettier": "^9.0.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-promise": "^6.1.1",
"husky": "^8.0.0",
"lint-staged": "^13.2.3",
"nodemon": "^3.0.1",
"prettier": "^3.0.1",
"ts-node": "^10.9.1",
"typechain": "^8.3.1",
"typescript": "^5.1.6"
},
"engines": {
"node": ">=18.16.0 <=18.17.0"
}
}