fix(backend): ServerStatsService start within running app

This commit is contained in:
ZerglingGo 2023-09-19 02:04:14 +09:00
parent 4fdf5e6add
commit 573af1b9ba
No known key found for this signature in database
GPG Key ID: 3919613C1147B4BF
3 changed files with 25 additions and 0 deletions

View File

@ -4,6 +4,7 @@
*/
import { Module } from '@nestjs/common';
import { ServerStatsService } from '../daemons/ServerStatsService.js';
import { AccountMoveService } from './AccountMoveService.js';
import { AccountUpdateService } from './AccountUpdateService.js';
import { AiService } from './AiService.js';
@ -248,6 +249,8 @@ const $ApMentionService: Provider = { provide: 'ApMentionService', useExisting:
const $ApNoteService: Provider = { provide: 'ApNoteService', useExisting: ApNoteService };
const $ApPersonService: Provider = { provide: 'ApPersonService', useExisting: ApPersonService };
const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting: ApQuestionService };
const $ServerStatsService: Provider = { provide: 'ServerStatsService', useExisting: ServerStatsService };
//#endregion
@Module({
@ -374,6 +377,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
ApPersonService,
ApQuestionService,
QueueService,
ServerStatsService,
//#region 文字列ベースでのinjection用(循環参照対応のため)
$LoggerService,
@ -494,6 +498,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$ApNoteService,
$ApPersonService,
$ApQuestionService,
$ServerStatsService,
//#endregion
],
exports: [
@ -616,6 +621,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
ApPersonService,
ApQuestionService,
QueueService,
ServerStatsService,
//#region 文字列ベースでのinjection用(循環参照対応のため)
$LoggerService,
@ -735,6 +741,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$ApNoteService,
$ApPersonService,
$ApQuestionService,
$ServerStatsService,
//#endregion
],
})

View File

@ -6,19 +6,24 @@
import { Inject, Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import * as Redis from 'ioredis';
import { ModuleRef } from '@nestjs/core';
import { DI } from '@/di-symbols.js';
import { MiMeta } from '@/models/entities/Meta.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { ServerStatsService } from '@/daemons/ServerStatsService.js';
import { bindThis } from '@/decorators.js';
import { StreamMessages } from '@/server/api/stream/types.js';
import type { OnApplicationShutdown } from '@nestjs/common';
@Injectable()
export class MetaService implements OnApplicationShutdown {
private serverStatsService: ServerStatsService;
private cache: MiMeta | undefined;
private intervalId: NodeJS.Timeout;
constructor(
private moduleRef: ModuleRef,
@Inject(DI.redisForSub)
private redisForSub: Redis.Redis,
@ -41,6 +46,10 @@ export class MetaService implements OnApplicationShutdown {
this.redisForSub.on('message', this.onMessage);
}
onModuleInit() {
this.serverStatsService = this.moduleRef.get('ServerStatsService');
}
@bindThis
private async onMessage(_: string, data: string): Promise<void> {
const obj = JSON.parse(data);
@ -121,6 +130,12 @@ export class MetaService implements OnApplicationShutdown {
this.globalEventService.publishInternalEvent('metaUpdated', updated);
if (updated.enableServerMachineStats === true) {
await this.serverStatsService.start();
} else {
this.serverStatsService.dispose();
}
return updated;
}

View File

@ -37,6 +37,9 @@ export class ServerStatsService implements OnApplicationShutdown {
const log = [] as any[];
ev.on('requestServerStatsLog', x => {
// skip when service deactivated
if (this.intervalId === null) return;
ev.emit(`serverStatsLog:${x.id}`, log.slice(0, x.length ?? 50));
});