first commit
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Niolla
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is proprietary and confidential.
|
||||
* Unauthorized copying, modification, distribution, or use
|
||||
* of this file, via any medium, is strictly prohibited.
|
||||
*/
|
||||
|
||||
// app/workers/pdf.worker.js
|
||||
|
||||
const { Worker } = require("bullmq");
|
||||
|
||||
const redis = require("../config/redisClient");
|
||||
|
||||
const { generateDocument } = require("../logic/documents");
|
||||
|
||||
const { uploadToS3 } = require("../utils/s3Upload.utill");
|
||||
|
||||
module.exports = async function createDocumentWorker() {
|
||||
const documentWorker = new Worker(
|
||||
"document-generation",
|
||||
|
||||
async (job) => {
|
||||
try {
|
||||
const { document, documentType, data } = job.data;
|
||||
|
||||
console.log(
|
||||
`📄 Processing Job ${job.id}: document="${document}", documentType="${documentType}"`,
|
||||
);
|
||||
|
||||
const { fileBuffer, fileName, mimeType } = await generateDocument(
|
||||
document,
|
||||
documentType,
|
||||
data,
|
||||
);
|
||||
|
||||
/**
|
||||
* Store in S3 instead of local filesystem
|
||||
*/
|
||||
const s3Key = await uploadToS3(
|
||||
{
|
||||
originalname: fileName,
|
||||
buffer: fileBuffer,
|
||||
mimetype: mimeType,
|
||||
},
|
||||
"document",
|
||||
);
|
||||
|
||||
// Extract UUID from S3 key
|
||||
const documentId = s3Key
|
||||
.split("/")
|
||||
.pop()
|
||||
.replace(/\.[^/.]+$/, "");
|
||||
|
||||
console.log(`✅ Job ${job.id} completed: ${fileName} (${s3Key})`);
|
||||
|
||||
/**
|
||||
* Keep response compatible with existing flow
|
||||
*/
|
||||
return {
|
||||
fileName,
|
||||
mimeType,
|
||||
size: fileBuffer.length,
|
||||
s3Key,
|
||||
documentId,
|
||||
};
|
||||
} catch (err) {
|
||||
console.error(`❌ Job ${job.id} failed:`, err.message);
|
||||
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
connection: redis,
|
||||
concurrency: 2,
|
||||
},
|
||||
);
|
||||
|
||||
documentWorker.on("error", (err) => {
|
||||
console.error("❌ Document Worker Error:", err);
|
||||
});
|
||||
|
||||
documentWorker.on("failed", (job, err) => {
|
||||
console.error(`❌ Job ${job?.id} failed after retries:`, err.message);
|
||||
});
|
||||
|
||||
documentWorker.on("completed", (job) => {
|
||||
console.log(`✅ Worker completed Job ${job.id}`);
|
||||
});
|
||||
|
||||
console.log("📄 Document Worker initialized");
|
||||
|
||||
return documentWorker;
|
||||
};
|
||||
Reference in New Issue
Block a user