misskey-awawa/src/daemons/queue-stats.ts

60 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-03-10 10:16:33 +00:00
import * as Deque from 'double-ended-queue';
import Xev from 'xev';
import { deliverQueue, inboxQueue } from '../queue';
const ev = new Xev();
const interval = 1000;
/**
* 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 01:35:17 +00:00
let activeDeliverJobs = 0;
let activeInboxJobs = 0;
deliverQueue.on('active', () => {
activeDeliverJobs++;
});
inboxQueue.on('active', () => {
activeInboxJobs++;
});
2019-03-10 10:16:33 +00:00
async function tick() {
const deliverJobCounts = await deliverQueue.getJobCounts();
const inboxJobCounts = await inboxQueue.getJobCounts();
const stats = {
2019-03-12 01:35:17 +00:00
deliver: {
active: activeDeliverJobs,
waiting: deliverJobCounts.waiting,
delyaed: deliverJobCounts.delayed
},
inbox: {
active: activeInboxJobs,
waiting: inboxJobCounts.waiting,
delyaed: inboxJobCounts.delayed
}
2019-03-10 10:16:33 +00:00
};
ev.emit('queueStats', stats);
log.unshift(stats);
if (log.length > 200) log.pop();
2019-03-12 01:35:17 +00:00
activeDeliverJobs = 0;
activeInboxJobs = 0;
2019-03-10 10:16:33 +00:00
}
tick();
setInterval(tick, interval);
}