| 6 | */ |
| 7 | |
| 8 | async function boot() { |
| 9 | // //////////////////////// |
| 10 | // SET UP THE CONNECTION // |
| 11 | // //////////////////////// |
| 12 | |
| 13 | const connectionDetails = { |
| 14 | pkg: "ioredis", |
| 15 | host: "127.0.0.1", |
| 16 | password: null as string, |
| 17 | port: 6379, |
| 18 | database: 0, |
| 19 | // namespace: 'resque', |
| 20 | // looping: true, |
| 21 | // options: {password: 'abc'}, |
| 22 | }; |
| 23 | |
| 24 | // /////////////////////////// |
| 25 | // DEFINE YOUR WORKER TASKS // |
| 26 | // /////////////////////////// |
| 27 | |
| 28 | let jobsToComplete = 0; |
| 29 | |
| 30 | const jobs = { |
| 31 | add: { |
| 32 | plugins: [Plugins.JobLock], |
| 33 | pluginOptions: { |
| 34 | JobLock: {}, |
| 35 | }, |
| 36 | perform: async (a: number, b: number) => { |
| 37 | await new Promise((resolve) => { |
| 38 | setTimeout(resolve, 1000); |
| 39 | }); |
| 40 | jobsToComplete--; |
| 41 | tryShutdown(); |
| 42 | |
| 43 | const answer = a + b; |
| 44 | return answer; |
| 45 | }, |
| 46 | }, |
| 47 | subtract: { |
| 48 | perform: async (a: number, b: number) => { |
| 49 | jobsToComplete--; |
| 50 | tryShutdown(); |
| 51 | |
| 52 | const answer = a - b; |
| 53 | return answer; |
| 54 | }, |
| 55 | }, |
| 56 | }; |
| 57 | |
| 58 | // just a helper for this demo |
| 59 | async function tryShutdown() { |
| 60 | if (jobsToComplete === 0) { |
| 61 | await new Promise((resolve) => { |
| 62 | setTimeout(resolve, 500); |
| 63 | }); |
| 64 | await scheduler.end(); |
| 65 | await worker.end(); |