Skip to content

Instantly share code, notes, and snippets.

@jamesholcomb
Last active July 9, 2021 19:41
Show Gist options
  • Save jamesholcomb/696b1750fb9fd67156445969b7e2138f to your computer and use it in GitHub Desktop.
Save jamesholcomb/696b1750fb9fd67156445969b7e2138f to your computer and use it in GitHub Desktop.
bullmq example showing add job to queue with same jobId
// only one job is added despite removeOnComplete: true
import { Queue, QueueScheduler, Job, Worker } from "bullmq"
const now = () => `[${new Date().toISOString()}]`
const queue = new Queue("Paint", {
defaultJobOptions: {
removeOnComplete: true,
},
})
const worker = new Worker("Paint", async (job: Job) => {
console.log(`${now()} worker started ${job.name}`)
return "done"
})
worker
.on("completed", (job: Job) => {
console.log(`${now()} completed ${job.name}`)
})
.on("failed", (job: Job, failedReason: string) => {
console.error(`${now()} worker failed`, job, failedReason)
})
.on("active", (job) => {
console.log(`${now()} job active ${job.name}`, job.data)
})
.on("error", (e) => {
console.error(`${now()} job error`, e)
})
.on("closing", () => console.log(`${now()} closing`))
const main = async (): Promise<void> => {
console.log(`${now()} started`)
const qs = new QueueScheduler("Paint")
await queue.waitUntilReady()
await queue.add("house1", { color: "red" }, { delay: 1000, jobId: "house1" })
await queue.add("house1", { color: "blue" }, { delay: 1000, jobId: "house1" })
}
process.on("SIGINT", async () => {
await worker.close()
setTimeout(() => process.exit(0), 100)
})
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment