2019-03-10 19:16:33 +09:00
|
|
|
import Xev from 'xev';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { deliverQueue, inboxQueue } from '../queue/queues';
|
2019-03-10 19:16:33 +09:00
|
|
|
|
|
|
|
const ev = new Xev();
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
const interval = 10000;
|
2019-03-10 19:16:33 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report queue stats regularly
|
|
|
|
*/
|
|
|
|
export default function() {
|
2020-01-30 04:37:25 +09:00
|
|
|
const log = [] as any[];
|
2019-03-10 19:16:33 +09:00
|
|
|
|
|
|
|
ev.on('requestQueueStatsLog', x => {
|
2020-01-30 04:37:25 +09:00
|
|
|
ev.emit(`queueStatsLog:${x.id}`, log.slice(0, x.length || 50));
|
2019-03-10 19:16:33 +09:00
|
|
|
});
|
|
|
|
|
2019-03-12 10:35:17 +09:00
|
|
|
let activeDeliverJobs = 0;
|
|
|
|
let activeInboxJobs = 0;
|
|
|
|
|
2019-03-12 16:42:56 +09:00
|
|
|
deliverQueue.on('global:active', () => {
|
2019-03-12 10:35:17 +09:00
|
|
|
activeDeliverJobs++;
|
|
|
|
});
|
|
|
|
|
2019-03-12 16:42:56 +09:00
|
|
|
inboxQueue.on('global:active', () => {
|
2019-03-12 10:35:17 +09:00
|
|
|
activeInboxJobs++;
|
|
|
|
});
|
|
|
|
|
2019-03-10 19:16:33 +09:00
|
|
|
async function tick() {
|
|
|
|
const deliverJobCounts = await deliverQueue.getJobCounts();
|
|
|
|
const inboxJobCounts = await inboxQueue.getJobCounts();
|
|
|
|
|
|
|
|
const stats = {
|
2019-03-12 10:35:17 +09:00
|
|
|
deliver: {
|
2019-03-12 12:31:01 +09:00
|
|
|
activeSincePrevTick: activeDeliverJobs,
|
|
|
|
active: deliverJobCounts.active,
|
2019-03-12 10:35:17 +09:00
|
|
|
waiting: deliverJobCounts.waiting,
|
2021-12-09 23:58:30 +09:00
|
|
|
delayed: deliverJobCounts.delayed,
|
2019-03-12 10:35:17 +09:00
|
|
|
},
|
|
|
|
inbox: {
|
2019-03-12 12:31:01 +09:00
|
|
|
activeSincePrevTick: activeInboxJobs,
|
|
|
|
active: inboxJobCounts.active,
|
2019-03-12 10:35:17 +09:00
|
|
|
waiting: inboxJobCounts.waiting,
|
2021-12-09 23:58:30 +09:00
|
|
|
delayed: inboxJobCounts.delayed,
|
2019-05-27 17:23:05 +09:00
|
|
|
},
|
2019-03-10 19:16:33 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
ev.emit('queueStats', stats);
|
|
|
|
|
|
|
|
log.unshift(stats);
|
|
|
|
if (log.length > 200) log.pop();
|
2019-03-12 10:35:17 +09:00
|
|
|
|
|
|
|
activeDeliverJobs = 0;
|
|
|
|
activeInboxJobs = 0;
|
2019-03-10 19:16:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
tick();
|
|
|
|
|
|
|
|
setInterval(tick, interval);
|
|
|
|
}
|