Sharkey/packages/backend/src/daemons/QueueStatsService.ts

81 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-09-17 21:27:08 +03:00
import { Inject, Injectable } from '@nestjs/common';
import Xev from 'xev';
import { DI } from '@/di-symbols.js';
import { QueueService } from '@/core/QueueService.js';
import { bindThis } from '@/decorators.js';
2022-09-17 21:27:08 +03:00
import type { OnApplicationShutdown } from '@nestjs/common';
const ev = new Xev();
const interval = 10000;
@Injectable()
export class QueueStatsService implements OnApplicationShutdown {
2022-09-18 21:11:50 +03:00
private intervalId: NodeJS.Timer;
2022-09-17 21:27:08 +03:00
constructor(
private queueService: QueueService,
) {
}
/**
* Report queue stats regularly
*/
@bindThis
2022-09-17 21:27:08 +03:00
public start(): void {
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;
this.queueService.deliverQueue.on('global:active', () => {
activeDeliverJobs++;
});
this.queueService.inboxQueue.on('global:active', () => {
activeInboxJobs++;
});
const tick = async () => {
const deliverJobCounts = await this.queueService.deliverQueue.getJobCounts();
const inboxJobCounts = await this.queueService.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();
2022-09-18 21:11:50 +03:00
this.intervalId = setInterval(tick, interval);
2022-09-17 21:27:08 +03:00
}
@bindThis
2022-09-17 21:27:08 +03:00
public onApplicationShutdown(signal?: string | undefined) {
2022-09-18 21:11:50 +03:00
clearInterval(this.intervalId);
2022-09-17 21:27:08 +03:00
}
}