-
Notifications
You must be signed in to change notification settings - Fork 12
/
example.js
92 lines (81 loc) · 2.4 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* eslint-disable no-await-in-loop */
const { Queue: QueueMQ, Worker } = require('bullmq');
const Queue3 = require('bull');
const express = require('express');
const bullMaster = require('./server/index');
const app = express();
// eslint-disable-next-line no-promise-executor-return
const sleep = (t) => new Promise((resolve) => setTimeout(resolve, t * 1000));
const redisOptions = {
port: 6379,
host: 'localhost',
password: '',
tls: false,
};
const createQueue3 = (name) => new Queue3(name, { redis: redisOptions });
const createQueueMQ = (name) => new QueueMQ(name, { connection: redisOptions });
const run = () => {
const exampleBullName = 'ExampleBull';
const exampleBull = createQueue3(exampleBullName);
const exampleBullMqName = 'ExampleBullMQ';
const exampleBullMq = createQueueMQ(exampleBullMqName);
exampleBull.process(async (job) => {
for (let i = 0; i <= 100; i += 1) {
await sleep(Math.random());
job.progress(i);
await job.log(`logging for ${i}`);
if (Math.random() * 200 < 1) throw new Error(`Random error ${i}`);
}
});
// eslint-disable-next-line no-new
new Worker(
exampleBullMqName,
async (job) => {
for (let i = 0; i <= 100; i += 1) {
await sleep(Math.random());
await job.updateProgress(i);
await job.log(`logging for ${i}`);
if (Math.random() * 200 < 1) throw new Error(`Random error ${i}`);
}
},
{
connection: redisOptions,
},
);
app.use('/add', (req, res) => {
const opts = req.query.opts || {};
const delay = parseInt(opts.delay, 10) || 0;
exampleBull.add(
{ title: req.query.title },
{
delay: delay * 1000,
},
);
exampleBullMq.add(
'Add',
{ title: req.query.title },
{
delay,
},
);
res.json({
ok: true,
});
});
app.use(
'/ui',
bullMaster({
queues: [exampleBullMq, exampleBull],
}),
);
app.listen(4889, () => {
console.log('Running on 4889...');
console.log('For the UI, open http://localhost:4889/ui');
console.log('Make sure Redis is running on port 6379 by default');
console.log('To populate the queue, run:');
console.log(' curl http://localhost:4889/add?title=Example');
console.log('To populate the queue with custom options (opts), run:');
console.log(' curl http://localhost:4889/add?title=Test&opts[delay]=900');
});
};
run();