2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-05-29 11:54:49 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-18 03:27:08 +09:00
|
|
|
import Xev from 'xev';
|
2023-05-29 11:54:49 +09:00
|
|
|
import * as Bull from 'bullmq';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { QueueService } from '@/core/QueueService.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-05-29 11:54:49 +09:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import type { Config } from '@/config.js';
|
|
|
|
import { QUEUE, baseQueueOptions } from '@/queue/const.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type { OnApplicationShutdown } from '@nestjs/common';
|
|
|
|
|
|
|
|
const ev = new Xev();
|
|
|
|
|
|
|
|
const interval = 10000;
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class QueueStatsService implements OnApplicationShutdown {
|
2023-09-04 13:33:38 +09:00
|
|
|
private intervalId: NodeJS.Timeout;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
constructor(
|
2023-05-29 11:54:49 +09:00
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
private queueService: QueueService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Report queue stats regularly
|
|
|
|
*/
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09: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;
|
|
|
|
|
2023-05-29 11:54:49 +09:00
|
|
|
const deliverQueueEvents = new Bull.QueueEvents(QUEUE.DELIVER, baseQueueOptions(this.config, QUEUE.DELIVER));
|
|
|
|
const inboxQueueEvents = new Bull.QueueEvents(QUEUE.INBOX, baseQueueOptions(this.config, QUEUE.INBOX));
|
|
|
|
|
|
|
|
deliverQueueEvents.on('active', () => {
|
2022-09-18 03:27:08 +09:00
|
|
|
activeDeliverJobs++;
|
|
|
|
});
|
|
|
|
|
2023-05-29 11:54:49 +09:00
|
|
|
inboxQueueEvents.on('active', () => {
|
2022-09-18 03:27:08 +09:00
|
|
|
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-19 03:11:50 +09:00
|
|
|
this.intervalId = setInterval(tick, interval);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2023-05-29 13:21:26 +09:00
|
|
|
public dispose(): void {
|
2022-09-19 03:11:50 +09:00
|
|
|
clearInterval(this.intervalId);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
2023-05-29 13:21:26 +09:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|