hippofish/src/daemons/queue-stats.ts

88 lines
2 KiB
TypeScript
Raw Normal View History

2019-03-10 11:16:33 +01:00
import * as Deque from 'double-ended-queue';
import Xev from 'xev';
2019-05-27 10:23:05 +02:00
import { deliverQueue, inboxQueue, dbQueue, objectStorageQueue } from '../queue';
2019-03-10 11:16:33 +01:00
const ev = new Xev();
2019-03-16 10:16:28 +01:00
const interval = 3000;
2019-03-10 11:16:33 +01: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-12 02:35:17 +01:00
let activeDeliverJobs = 0;
let activeInboxJobs = 0;
2019-05-27 10:23:05 +02:00
let activeDbJobs = 0;
let activeObjectStorageJobs = 0;
2019-03-12 02:35:17 +01:00
2019-03-12 08:42:56 +01:00
deliverQueue.on('global:active', () => {
2019-03-12 02:35:17 +01:00
activeDeliverJobs++;
});
2019-03-12 08:42:56 +01:00
inboxQueue.on('global:active', () => {
2019-03-12 02:35:17 +01:00
activeInboxJobs++;
});
2019-05-27 10:23:05 +02:00
dbQueue.on('global:active', () => {
activeDbJobs++;
});
objectStorageQueue.on('global:active', () => {
activeObjectStorageJobs++;
});
2019-03-10 11:16:33 +01:00
async function tick() {
const deliverJobCounts = await deliverQueue.getJobCounts();
const inboxJobCounts = await inboxQueue.getJobCounts();
2019-05-27 10:23:05 +02:00
const dbJobCounts = await dbQueue.getJobCounts();
const objectStorageJobCounts = await objectStorageQueue.getJobCounts();
2019-03-10 11:16:33 +01:00
const stats = {
2019-03-12 02:35:17 +01:00
deliver: {
activeSincePrevTick: activeDeliverJobs,
active: deliverJobCounts.active,
2019-03-12 02:35:17 +01:00
waiting: deliverJobCounts.waiting,
2019-03-12 02:46:25 +01:00
delayed: deliverJobCounts.delayed
2019-03-12 02:35:17 +01:00
},
inbox: {
activeSincePrevTick: activeInboxJobs,
active: inboxJobCounts.active,
2019-03-12 02:35:17 +01:00
waiting: inboxJobCounts.waiting,
2019-03-12 02:46:25 +01:00
delayed: inboxJobCounts.delayed
2019-05-27 10:23:05 +02:00
},
db: {
activeSincePrevTick: activeDbJobs,
active: dbJobCounts.active,
waiting: dbJobCounts.waiting,
delayed: dbJobCounts.delayed
},
objectStorage: {
activeSincePrevTick: activeObjectStorageJobs,
active: objectStorageJobCounts.active,
waiting: objectStorageJobCounts.waiting,
delayed: objectStorageJobCounts.delayed
},
2019-03-10 11:16:33 +01:00
};
ev.emit('queueStats', stats);
log.unshift(stats);
if (log.length > 200) log.pop();
2019-03-12 02:35:17 +01:00
activeDeliverJobs = 0;
activeInboxJobs = 0;
2019-05-27 10:23:05 +02:00
activeDbJobs = 0;
activeObjectStorageJobs = 0;
2019-03-10 11:16:33 +01:00
}
tick();
setInterval(tick, interval);
}