From a4d2c54bf2e86eceb8915b7c095b9a8f0ca4909c Mon Sep 17 00:00:00 2001 From: ZerglingGo Date: Sat, 22 Jul 2023 04:02:41 +0900 Subject: [PATCH 1/3] fix(backend): ServerStatsService start within running app --- packages/backend/src/server/api/EndpointsModule.ts | 2 ++ .../src/server/api/endpoints/admin/update-meta.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/packages/backend/src/server/api/EndpointsModule.ts b/packages/backend/src/server/api/EndpointsModule.ts index 4e6bc46e6778..44e9a684b19f 100644 --- a/packages/backend/src/server/api/EndpointsModule.ts +++ b/packages/backend/src/server/api/EndpointsModule.ts @@ -344,6 +344,7 @@ import * as ep___fetchRss from './endpoints/fetch-rss.js'; import * as ep___retention from './endpoints/retention.js'; import { GetterService } from './GetterService.js'; import { ApiLoggerService } from './ApiLoggerService.js'; +import { ServerStatsService } from '@/daemons/ServerStatsService.js'; import type { Provider } from '@nestjs/common'; const $admin_meta: Provider = { provide: 'ep:admin/meta', useClass: ep___admin_meta.default }; @@ -695,6 +696,7 @@ const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention providers: [ GetterService, ApiLoggerService, + ServerStatsService, $admin_meta, $admin_abuseUserReports, $admin_accounts_create, diff --git a/packages/backend/src/server/api/endpoints/admin/update-meta.ts b/packages/backend/src/server/api/endpoints/admin/update-meta.ts index 144360a9218c..b29a4e839846 100644 --- a/packages/backend/src/server/api/endpoints/admin/update-meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/update-meta.ts @@ -1,10 +1,12 @@ import { Inject, Injectable } from '@nestjs/common'; +import { ModuleRef } from '@nestjs/core'; import { DataSource } from 'typeorm'; import type { Meta } from '@/models/entities/Meta.js'; import { ModerationLogService } from '@/core/ModerationLogService.js'; import { Endpoint } from '@/server/api/endpoint-base.js'; import { DI } from '@/di-symbols.js'; import { MetaService } from '@/core/MetaService.js'; +import { ServerStatsService } from '@/daemons/ServerStatsService.js'; export const meta = { tags: ['admin'], @@ -109,6 +111,8 @@ export const paramDef = { @Injectable() export default class extends Endpoint { constructor( + private moduleRef: ModuleRef, + @Inject(DI.db) private db: DataSource, @@ -424,6 +428,14 @@ export default class extends Endpoint { await this.metaService.update(set); this.moderationLogService.insertModerationLog(me, 'updateMeta'); + + if (set.enableServerMachineStats === true) { + const serverStatsService: ServerStatsService = await this.moduleRef.resolve(ServerStatsService); + await serverStatsService.start(); + } else { + const serverStatsService: ServerStatsService = await this.moduleRef.resolve(ServerStatsService); + serverStatsService.dispose(); + } }); } } From 4d71ad50e6f37f9965ac0483e58250cb00601515 Mon Sep 17 00:00:00 2001 From: ZerglingGo Date: Fri, 25 Aug 2023 03:01:01 +0900 Subject: [PATCH 2/3] Revert "fix(backend): ServerStatsService start within running app" This reverts commit a4d2c54bf2e86eceb8915b7c095b9a8f0ca4909c. --- packages/backend/src/server/api/EndpointsModule.ts | 2 -- .../src/server/api/endpoints/admin/update-meta.ts | 12 ------------ 2 files changed, 14 deletions(-) diff --git a/packages/backend/src/server/api/EndpointsModule.ts b/packages/backend/src/server/api/EndpointsModule.ts index 48f1de701e36..799ba4498d73 100644 --- a/packages/backend/src/server/api/EndpointsModule.ts +++ b/packages/backend/src/server/api/EndpointsModule.ts @@ -350,7 +350,6 @@ import * as ep___fetchRss from './endpoints/fetch-rss.js'; import * as ep___retention from './endpoints/retention.js'; import { GetterService } from './GetterService.js'; import { ApiLoggerService } from './ApiLoggerService.js'; -import { ServerStatsService } from '@/daemons/ServerStatsService.js'; import type { Provider } from '@nestjs/common'; const $admin_meta: Provider = { provide: 'ep:admin/meta', useClass: ep___admin_meta.default }; @@ -703,7 +702,6 @@ const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention providers: [ GetterService, ApiLoggerService, - ServerStatsService, $admin_meta, $admin_abuseUserReports, $admin_accounts_create, diff --git a/packages/backend/src/server/api/endpoints/admin/update-meta.ts b/packages/backend/src/server/api/endpoints/admin/update-meta.ts index 392a4a2c2ae8..17d5a1f9180c 100644 --- a/packages/backend/src/server/api/endpoints/admin/update-meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/update-meta.ts @@ -4,12 +4,10 @@ */ import { Injectable } from '@nestjs/common'; -import { ModuleRef } from '@nestjs/core'; import type { MiMeta } from '@/models/entities/Meta.js'; import { ModerationLogService } from '@/core/ModerationLogService.js'; import { Endpoint } from '@/server/api/endpoint-base.js'; import { MetaService } from '@/core/MetaService.js'; -import { ServerStatsService } from '@/daemons/ServerStatsService.js'; export const meta = { tags: ['admin'], @@ -113,8 +111,6 @@ export const paramDef = { @Injectable() export default class extends Endpoint { // eslint-disable-line import/no-default-export constructor( - private moduleRef: ModuleRef, - private metaService: MetaService, private moderationLogService: ModerationLogService, ) { @@ -427,14 +423,6 @@ export default class extends Endpoint { // eslint- await this.metaService.update(set); this.moderationLogService.insertModerationLog(me, 'updateMeta'); - - if (set.enableServerMachineStats === true) { - const serverStatsService: ServerStatsService = await this.moduleRef.resolve(ServerStatsService); - await serverStatsService.start(); - } else { - const serverStatsService: ServerStatsService = await this.moduleRef.resolve(ServerStatsService); - serverStatsService.dispose(); - } }); } } From 573af1b9babe63f8d4f25ff902bdaa45ebbf4eac Mon Sep 17 00:00:00 2001 From: ZerglingGo Date: Tue, 19 Sep 2023 02:04:14 +0900 Subject: [PATCH 3/3] fix(backend): ServerStatsService start within running app --- packages/backend/src/core/CoreModule.ts | 7 +++++++ packages/backend/src/core/MetaService.ts | 15 +++++++++++++++ .../backend/src/daemons/ServerStatsService.ts | 3 +++ 3 files changed, 25 insertions(+) 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)); });