From 0e4a111f81cceed275d9bec2695f6e401fb654d8 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 12 Nov 2021 02:02:25 +0900 Subject: refactoring Resolve #7779 --- packages/backend/src/daemons/queue-stats.ts | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 packages/backend/src/daemons/queue-stats.ts (limited to 'packages/backend/src/daemons/queue-stats.ts') diff --git a/packages/backend/src/daemons/queue-stats.ts b/packages/backend/src/daemons/queue-stats.ts new file mode 100644 index 0000000000..77f09b18d6 --- /dev/null +++ b/packages/backend/src/daemons/queue-stats.ts @@ -0,0 +1,60 @@ +import Xev from 'xev'; +import { deliverQueue, inboxQueue } from '../queue/queues'; + +const ev = new Xev(); + +const interval = 10000; + +/** + * Report queue stats regularly + */ +export default function() { + const log = [] as any[]; + + ev.on('requestQueueStatsLog', x => { + ev.emit(`queueStatsLog:${x.id}`, log.slice(0, x.length || 50)); + }); + + let activeDeliverJobs = 0; + let activeInboxJobs = 0; + + deliverQueue.on('global:active', () => { + activeDeliverJobs++; + }); + + inboxQueue.on('global:active', () => { + activeInboxJobs++; + }); + + async function tick() { + const deliverJobCounts = await deliverQueue.getJobCounts(); + const inboxJobCounts = await inboxQueue.getJobCounts(); + + const stats = { + deliver: { + activeSincePrevTick: activeDeliverJobs, + active: deliverJobCounts.active, + waiting: deliverJobCounts.waiting, + delayed: deliverJobCounts.delayed + }, + inbox: { + activeSincePrevTick: activeInboxJobs, + active: inboxJobCounts.active, + waiting: inboxJobCounts.waiting, + delayed: inboxJobCounts.delayed + }, + }; + + ev.emit('queueStats', stats); + + log.unshift(stats); + if (log.length > 200) log.pop(); + + activeDeliverJobs = 0; + activeInboxJobs = 0; + } + + tick(); + + setInterval(tick, interval); +} -- cgit v1.2.3-freya