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

78 lines
1.8 KiB
TypeScript
Raw Normal View History

/*
2024-02-12 03:37:45 +01:00
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable } from '@nestjs/common';
2022-09-17 20:27:08 +02:00
import Xev from 'xev';
import * as Bull from 'bullmq';
2022-09-17 20:27:08 +02:00
import { QueueService } from '@/core/QueueService.js';
import { bindThis } from '@/decorators.js';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { QUEUE, baseQueueOptions } from '@/queue/const.js';
2022-09-17 20:27:08 +02:00
import type { OnApplicationShutdown } from '@nestjs/common';
const ev = new Xev();
const interval = 30000;
2022-09-17 20:27:08 +02:00
@Injectable()
export class QueueStatsService implements OnApplicationShutdown {
private intervalId: NodeJS.Timeout;
2022-09-17 20:27:08 +02:00
constructor(
@Inject(DI.config)
private config: Config,
2022-09-17 20:27:08 +02:00
private queueService: QueueService,
) {
}
/**
* Report queue stats regularly
*/
@bindThis
2022-09-17 20:27:08 +02:00
public start(): void {
ev.on('requestQueueStatsLog', x => {
ev.emit(`queueStatsLog:${x.id}`, []);
2022-09-17 20:27:08 +02:00
});
const tick = async () => {
const deliverJobCounts = await this.queueService.deliverQueue.getJobCounts();
const inboxJobCounts = await this.queueService.inboxQueue.getJobCounts();
const stats = {
deliver: {
activeSincePrevTick: 0, // it's removed for performance reason
2022-09-17 20:27:08 +02:00
active: deliverJobCounts.active,
waiting: deliverJobCounts.waiting,
delayed: deliverJobCounts.delayed,
},
inbox: {
activeSincePrevTick: 0, // it's removed for performance reason
2022-09-17 20:27:08 +02:00
active: inboxJobCounts.active,
waiting: inboxJobCounts.waiting,
delayed: inboxJobCounts.delayed,
},
};
ev.emit('queueStats', stats);
};
tick();
2022-09-18 20:11:50 +02:00
this.intervalId = setInterval(tick, interval);
2022-09-17 20:27:08 +02:00
}
@bindThis
2023-05-29 06:21:26 +02:00
public dispose(): void {
2022-09-18 20:11:50 +02:00
clearInterval(this.intervalId);
2022-09-17 20:27:08 +02:00
}
2023-05-29 06:21:26 +02:00
@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
2022-09-17 20:27:08 +02:00
}