I created a job that allows me to create a pdf file with dynamic content inside. It works when I do a post request the first time and start the job, but the second time it fails with the errror above.
what I basically have in the invoiceWorker.ts file is this:
const bull = require("bull")
import { Job } from "bull"
import { Order } from "./../entities/order"
const queue = new bull("pdf-generation")
const pdfKit = require("pdfkit")
const fs = require("fs")
export async function startProcess() {
await queue.close()
queue.process(async (job: Job) => {
console.log(`Processing Job with id ${job.id}`)
await generatePdfInvoice(job.data)
console.log(job.finished())
})
}
async function generatePdfInvoice(data: Order) {
let doc = new pdfKit()
const pdfName = data.id + ".pdf"
var s = doc.pipe(fs.createWriteStream(pdfName))
doc
.fontSize(14)
.text(
`It doesnt take long... we received your order, ${data.customer.username}`,
100,
100
)
doc
.moveDown(2)
.fontSize(14)
.text(`The prize of your order is: ${data.burgers[0].totalPrize} $`)
doc
.moveDown(2)
.fontSize(14)
.text(`We are delivering it to: ${data.customer.adress} $`)
doc.end()
console.log(`Generated PDF document`)
await queue.close()
}
the start process function adds a process handler and runs the generatedPDfInvoice function which basically creats a pdf file.
In another file I got:
import { Job } from "bull"
import { Order } from "../entities/order"
const bull = require("bull")
export const queue = new bull("pdf-generation")
export async function startJob(invoiceData: Order) {
console.log("startjob")
let job: Job = await queue.add(invoiceData)
}
which adds a job to the queue. In my controller I have the following:`
order = await orderRepo.save(order)
await startJob(order)
console.log(order)
await startProcess()
``````````````
where I just run those functions.
Why I am getting this error? Am I not allowed to run queue.process() every request? If not,
how do I structure my code properly?
Thx:)