-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
48 lines (38 loc) · 1.36 KB
/
test.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
// set up logging
const LOG_LEVEL = 'trace';
const log = require('simple-node-logger').createSimpleLogger({
timestampFormat: '[[QYU-LOG]]'
});
log.setLevel(LOG_LEVEL);
log.trace(`Qyu ${LOG_LEVEL} logging to standard output`);
const qyu = require('./qyu');
const q = qyu({
log,
rateLimit: 50, // maximum number of jobs being processed by second
statsInterval: 300 // When stat event is sent, in ms
});
q.on('done', ({jobId, jobResult, res}) => {
console.log(`Job done ${jobId}`); // `jobId` is generated by `qyu`
});
q.on('error', ({jobId, error}) => {
console.log(`Job ${jobId} threw an error: ${error.message}`);
});
q.on('drain', () => {
console.log('No more jobs to do');
});
q.on('stats', ({nbJobsPerSecond}) => {
console.log(`${nbJobsPerSecond} jobs/s processed`)
});
q.push(job, { // job is a function returning a promise to indicate when the job is done
priority: 1, // from 1 to 10, 1 being the highest priority
}); // returns a promise (which resolves with {jobId, jobResult})
q.pause(); // returns a promise resolved when `q` has paused (no jobs being processed)
q.start(); // returns a promise resolved when `q` has started (first time) or unpaused
// example job:
async function job() {
await wait(30);
return {Hello: 'world!'} // That's the `jobResult`
}
function wait(ms) {
return new Promise((resolve, reject) => setTimeout(resolve, ms));
}