jormungandr-bite/packages/backend/src/daemons/queue-stats.ts

61 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-01-12 21:40:33 -07:00
import Xev from "xev";
import { deliverQueue, inboxQueue } from "../queue/queues.js";
2019-03-10 04:16:33 -06:00
2022-04-16 23:42:13 -06:00
const ev = new Xev();
2019-03-10 04:16:33 -06:00
const interval = 10000;
2019-03-10 04:16:33 -06:00
/**
* Report queue stats regularly
*/
2023-01-12 21:40:33 -07:00
export default function () {
const log = [] as any[];
2019-03-10 04:16:33 -06:00
2023-01-12 21:40:33 -07:00
ev.on("requestQueueStatsLog", (x) => {
ev.emit(`queueStatsLog:${x.id}`, log.slice(0, x.length || 50));
2019-03-10 04:16:33 -06:00
});
2019-03-11 19:35:17 -06:00
let activeDeliverJobs = 0;
let activeInboxJobs = 0;
2023-01-12 21:40:33 -07:00
deliverQueue.on("global:active", () => {
2019-03-11 19:35:17 -06:00
activeDeliverJobs++;
});
2023-01-12 21:40:33 -07: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: {
activeSincePrevTick: activeDeliverJobs,
active: deliverJobCounts.active,
2019-03-11 19:35:17 -06:00
waiting: deliverJobCounts.waiting,
2021-12-09 07:58:30 -07:00
delayed: deliverJobCounts.delayed,
2019-03-11 19:35:17 -06:00
},
inbox: {
activeSincePrevTick: activeInboxJobs,
active: inboxJobCounts.active,
2019-03-11 19:35:17 -06:00
waiting: inboxJobCounts.waiting,
2021-12-09 07:58:30 -07:00
delayed: inboxJobCounts.delayed,
2019-05-27 02:23:05 -06:00
},
2019-03-10 04:16:33 -06:00
};
2023-01-12 21:40:33 -07:00
ev.emit("queueStats", stats);
2019-03-10 04:16:33 -06:00
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);
}