2019-03-10 04:16:33 -06:00
|
|
|
import * as Deque from 'double-ended-queue';
|
|
|
|
import Xev from 'xev';
|
|
|
|
import { deliverQueue, inboxQueue } from '../queue';
|
|
|
|
|
|
|
|
const ev = new Xev();
|
|
|
|
|
2019-03-16 03:16:28 -06:00
|
|
|
const interval = 3000;
|
2019-03-10 04:16:33 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report queue stats regularly
|
|
|
|
*/
|
|
|
|
export default function() {
|
|
|
|
const log = new Deque<any>();
|
|
|
|
|
|
|
|
ev.on('requestQueueStatsLog', x => {
|
|
|
|
ev.emit(`queueStatsLog:${x.id}`, log.toArray().slice(0, x.length || 50));
|
|
|
|
});
|
|
|
|
|
2019-03-11 19:35:17 -06:00
|
|
|
let activeDeliverJobs = 0;
|
|
|
|
let activeInboxJobs = 0;
|
|
|
|
|
2019-03-12 01:42:56 -06:00
|
|
|
deliverQueue.on('global:active', () => {
|
2019-03-11 19:35:17 -06:00
|
|
|
activeDeliverJobs++;
|
|
|
|
});
|
|
|
|
|
2019-03-12 01:42:56 -06:00
|
|
|
inboxQueue.on('global:active', () => {
|
2019-03-11 19:35:17 -06:00
|
|
|
activeInboxJobs++;
|
|
|
|
});
|
|
|
|
|
2019-03-10 04:16:33 -06:00
|
|
|
async function tick() {
|
|
|
|
const deliverJobCounts = await deliverQueue.getJobCounts();
|
|
|
|
const inboxJobCounts = await inboxQueue.getJobCounts();
|
|
|
|
|
|
|
|
const stats = {
|
2019-03-11 19:35:17 -06:00
|
|
|
deliver: {
|
2019-03-11 21:31:01 -06:00
|
|
|
activeSincePrevTick: activeDeliverJobs,
|
|
|
|
active: deliverJobCounts.active,
|
2019-03-11 19:35:17 -06:00
|
|
|
waiting: deliverJobCounts.waiting,
|
2019-03-11 19:46:25 -06:00
|
|
|
delayed: deliverJobCounts.delayed
|
2019-03-11 19:35:17 -06:00
|
|
|
},
|
|
|
|
inbox: {
|
2019-03-11 21:31:01 -06:00
|
|
|
activeSincePrevTick: activeInboxJobs,
|
|
|
|
active: inboxJobCounts.active,
|
2019-03-11 19:35:17 -06:00
|
|
|
waiting: inboxJobCounts.waiting,
|
2019-03-11 19:46:25 -06:00
|
|
|
delayed: inboxJobCounts.delayed
|
2019-03-11 19:35:17 -06:00
|
|
|
}
|
2019-03-10 04:16:33 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
ev.emit('queueStats', stats);
|
|
|
|
|
|
|
|
log.unshift(stats);
|
|
|
|
if (log.length > 200) log.pop();
|
2019-03-11 19:35:17 -06:00
|
|
|
|
|
|
|
activeDeliverJobs = 0;
|
|
|
|
activeInboxJobs = 0;
|
2019-03-10 04:16:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
setInterval(tick, interval);
|
|
|
|
}
|