| 8 | let scheduler; |
| 9 | |
| 10 | async function boot() { |
| 11 | const connectionDetails = { |
| 12 | pkg: "ioredis", |
| 13 | host: process.env.REDIS_HOST, |
| 14 | }; |
| 15 | |
| 16 | const jobs = { |
| 17 | add: { |
| 18 | perform: async (a, b) => { |
| 19 | const answer = a + b; |
| 20 | return answer; |
| 21 | }, |
| 22 | }, |
| 23 | subtract: { |
| 24 | perform: (a, b) => { |
| 25 | const answer = a - b; |
| 26 | return answer; |
| 27 | }, |
| 28 | }, |
| 29 | }; |
| 30 | |
| 31 | worker = new Worker( |
| 32 | { connection: connectionDetails, queues: ["math", "otherQueue"] }, |
| 33 | jobs, |
| 34 | ); |
| 35 | await worker.connect(); |
| 36 | worker.start(); |
| 37 | |
| 38 | worker.on("start", () => { |
| 39 | console.log("worker started"); |
| 40 | }); |
| 41 | worker.on("end", () => { |
| 42 | console.log("worker ended"); |
| 43 | }); |
| 44 | worker.on("cleaning_worker", (worker, pid) => { |
| 45 | console.log(`cleaning old worker ${worker}`); |
| 46 | }); |
| 47 | worker.on("poll", (queue) => { |
| 48 | console.log(`worker polling ${queue}`); |
| 49 | }); |
| 50 | worker.on("ping", (time) => { |
| 51 | console.log(`worker check in @ ${time}`); |
| 52 | }); |
| 53 | worker.on("job", (queue, job) => { |
| 54 | console.log(`working job ${queue} ${JSON.stringify(job)}`); |
| 55 | }); |
| 56 | worker.on("reEnqueue", (queue, job, plugin) => { |
| 57 | console.log(`reEnqueue job (${plugin}) ${queue} ${JSON.stringify(job)}`); |
| 58 | }); |
| 59 | worker.on("success", (queue, job, result, duration) => { |
| 60 | console.log( |
| 61 | `job success ${queue} ${JSON.stringify( |
| 62 | job, |
| 63 | )} >> ${result} (${duration}ms)`, |
| 64 | ); |
| 65 | }); |
| 66 | worker.on("failure", (queue, job, failure, duration) => { |
| 67 | console.log( |