我创建了一个工作,允许我创建一个包含动态内容的 pdf 文件。当我第一次发出帖子请求并开始工作时它可以工作,但第二次它失败并出现上述错误。
我在 invoiceWorker.ts 文件中的基本内容是:
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()
}
启动进程函数添加了一个进程处理程序并运行生成的PDfInvoice 函数,该函数基本上创建一个pdf文件。
在另一个文件中,我得到:
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)
}
它将作业添加到队列中。在我的控制器中,我有以下内容:`
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:)