|
| 1 | +const Arena = require('../'); |
| 2 | +const fastify = require('fastify'); |
| 3 | +const {Queue, Worker, FlowProducer} = require('bullmq'); |
| 4 | +const RedisServer = require('redis-server'); |
| 5 | + |
| 6 | +// Select ports that are unlikely to be used by other services a developer might be running locally. |
| 7 | +const HTTP_SERVER_PORT = 4735; |
| 8 | +const REDIS_SERVER_PORT = 4736; |
| 9 | + |
| 10 | +// Create a Redis server. This is only for convenience |
| 11 | + |
| 12 | +async function main() { |
| 13 | + const app = fastify(); |
| 14 | + const server = new RedisServer(REDIS_SERVER_PORT); |
| 15 | + await server.open(); |
| 16 | + const queueName = 'name_of_my_queue'; |
| 17 | + const parentQueueName = 'name_of_my_parent_queue'; |
| 18 | + |
| 19 | + const queue = new Queue(queueName, { |
| 20 | + connection: {port: REDIS_SERVER_PORT}, |
| 21 | + }); |
| 22 | + new Queue(parentQueueName, { |
| 23 | + connection: {port: REDIS_SERVER_PORT}, |
| 24 | + }); |
| 25 | + |
| 26 | + const flow = new FlowProducer({ |
| 27 | + connection: {port: REDIS_SERVER_PORT}, |
| 28 | + }); |
| 29 | + |
| 30 | + new Worker( |
| 31 | + queueName, |
| 32 | + async function (job) { |
| 33 | + await job.updateProgress(20); |
| 34 | + |
| 35 | + // Wait 5sec |
| 36 | + await new Promise((res) => setTimeout(res, 5000)); |
| 37 | + |
| 38 | + // Randomly succeeds or fails the job to put some jobs in completed and some in failed. |
| 39 | + if (Math.random() > 0.5) { |
| 40 | + throw new Error('fake error'); |
| 41 | + } |
| 42 | + }, |
| 43 | + { |
| 44 | + concurrency: 3, |
| 45 | + connection: {port: REDIS_SERVER_PORT}, |
| 46 | + } |
| 47 | + ); |
| 48 | + |
| 49 | + new Worker( |
| 50 | + parentQueueName, |
| 51 | + async function () { |
| 52 | + // Wait 10sec |
| 53 | + await new Promise((res) => setTimeout(res, 10000)); |
| 54 | + |
| 55 | + // Randomly succeeds or fails the job to put some jobs in completed and some in failed. |
| 56 | + if (Math.random() > 0.5) { |
| 57 | + throw new Error('fake error'); |
| 58 | + } |
| 59 | + }, |
| 60 | + { |
| 61 | + connection: {port: REDIS_SERVER_PORT}, |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + const children = Array.from(Array(65).keys()).map((index) => ({ |
| 66 | + name: 'child', |
| 67 | + data: {idx: index, foo: 'bar'}, |
| 68 | + queueName, |
| 69 | + })); |
| 70 | + await flow.add({ |
| 71 | + name: 'parent-job', |
| 72 | + queueName: parentQueueName, |
| 73 | + data: {}, |
| 74 | + children, |
| 75 | + }); |
| 76 | + |
| 77 | + // adding delayed jobs |
| 78 | + const delayedJob = await queue.add('delayed', {}, {delay: 60 * 1000}); |
| 79 | + delayedJob.log('Log message'); |
| 80 | + |
| 81 | + const arena = Arena( |
| 82 | + { |
| 83 | + BullMQ: Queue, |
| 84 | + |
| 85 | + queues: [ |
| 86 | + { |
| 87 | + // Required for each queue definition. |
| 88 | + name: queueName, |
| 89 | + |
| 90 | + // User-readable display name for the host. Required. |
| 91 | + hostId: 'Queue Server 1', |
| 92 | + |
| 93 | + // Queue type (Bull or Bullmq or Bee - default Bull). |
| 94 | + type: 'bullmq', |
| 95 | + |
| 96 | + redis: { |
| 97 | + // host: 'localhost', |
| 98 | + port: REDIS_SERVER_PORT, |
| 99 | + }, |
| 100 | + }, |
| 101 | + { |
| 102 | + // Required for each queue definition. |
| 103 | + name: parentQueueName, |
| 104 | + |
| 105 | + // User-readable display name for the host. Required. |
| 106 | + hostId: 'Queue Server 2', |
| 107 | + |
| 108 | + // Queue type (Bull or Bullmq or Bee - default Bull). |
| 109 | + type: 'bullmq', |
| 110 | + |
| 111 | + redis: { |
| 112 | + // host: 'localhost', |
| 113 | + port: REDIS_SERVER_PORT, |
| 114 | + }, |
| 115 | + }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + { |
| 119 | + basePath: '/', |
| 120 | + disableListen: true, |
| 121 | + } |
| 122 | + ); |
| 123 | + |
| 124 | + await app.register(require('@fastify/express')); |
| 125 | + app.use('/arena', arena); |
| 126 | + |
| 127 | + app.listen({port: HTTP_SERVER_PORT}, (err, address) => { |
| 128 | + if (err) throw err; |
| 129 | + console.log(`Arena listening on port ${HTTP_SERVER_PORT}!`); |
| 130 | + }); |
| 131 | +} |
| 132 | + |
| 133 | +main().catch((err) => { |
| 134 | + console.error(err); |
| 135 | + process.exit(1); |
| 136 | +}); |
0 commit comments