Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend): ServerStatsService start within running app #11342

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/backend/src/core/CoreModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -374,6 +377,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
ApPersonService,
ApQuestionService,
QueueService,
ServerStatsService,

//#region 文字列ベースでのinjection用(循環参照対応のため)
$LoggerService,
Expand Down Expand Up @@ -494,6 +498,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$ApNoteService,
$ApPersonService,
$ApQuestionService,
$ServerStatsService,
//#endregion
],
exports: [
Expand Down Expand Up @@ -616,6 +621,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
ApPersonService,
ApQuestionService,
QueueService,
ServerStatsService,

//#region 文字列ベースでのinjection用(循環参照対応のため)
$LoggerService,
Expand Down Expand Up @@ -735,6 +741,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$ApNoteService,
$ApPersonService,
$ApQuestionService,
$ServerStatsService,
//#endregion
],
})
Expand Down
15 changes: 15 additions & 0 deletions packages/backend/src/core/MetaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/backend/src/daemons/ServerStatsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

Expand Down