diff --git a/packages/backend/src/core/CoreModule.ts b/packages/backend/src/core/CoreModule.ts index 863f1a2fd585..83d9bb3b963b 100644 --- a/packages/backend/src/core/CoreModule.ts +++ b/packages/backend/src/core/CoreModule.ts @@ -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 ], }) diff --git a/packages/backend/src/core/MetaService.ts b/packages/backend/src/core/MetaService.ts index 42ce4de43223..00abb03ebacb 100644 --- a/packages/backend/src/core/MetaService.ts +++ b/packages/backend/src/core/MetaService.ts @@ -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 { 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; } diff --git a/packages/backend/src/daemons/ServerStatsService.ts b/packages/backend/src/daemons/ServerStatsService.ts index d29462874066..ec890f8e73b5 100644 --- a/packages/backend/src/daemons/ServerStatsService.ts +++ b/packages/backend/src/daemons/ServerStatsService.ts @@ -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)); });