diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2022-09-21 02:35:49 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2022-09-21 02:35:49 +0900 |
| commit | dc43fc68ef2b8e0d6f2cfd94911b367fd915f445 (patch) | |
| tree | 0ee0971bd64cad08fa493745e9c6b43786452eb0 /packages/backend/src/core/MetaService.ts | |
| parent | fix(backend): add missing noteEntityService dep (diff) | |
| download | sharkey-dc43fc68ef2b8e0d6f2cfd94911b367fd915f445.tar.gz sharkey-dc43fc68ef2b8e0d6f2cfd94911b367fd915f445.tar.bz2 sharkey-dc43fc68ef2b8e0d6f2cfd94911b367fd915f445.zip | |
enhance(backend): metaのポーリング頻度を減らし、redisで更新を受け取るように
Diffstat (limited to 'packages/backend/src/core/MetaService.ts')
| -rw-r--r-- | packages/backend/src/core/MetaService.ts | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/packages/backend/src/core/MetaService.ts b/packages/backend/src/core/MetaService.ts index 4099e340be..5ae9a6fc83 100644 --- a/packages/backend/src/core/MetaService.ts +++ b/packages/backend/src/core/MetaService.ts @@ -1,6 +1,6 @@ import { Inject, Injectable } from '@nestjs/common'; import { DataSource } from 'typeorm'; -import type { UsersRepository } from '@/models/index.js'; +import Redis from 'ioredis'; import { DI } from '@/di-symbols.js'; import { Meta } from '@/models/entities/Meta.js'; import type { OnApplicationShutdown } from '@nestjs/common'; @@ -11,19 +11,27 @@ export class MetaService implements OnApplicationShutdown { private intervalId: NodeJS.Timer; constructor( + @Inject(DI.redisSubscriber) + private redisSubscriber: Redis.Redis, + @Inject(DI.db) private db: DataSource, ) { + this.onMessage = this.onMessage.bind(this); + if (process.env.NODE_ENV !== 'test') { this.intervalId = setInterval(() => { this.fetch(true).then(meta => { + // fetch内でもセットしてるけど仕様変更の可能性もあるため一応 this.cache = meta; }); - }, 1000 * 10); + }, 1000 * 60 * 5); } + + this.redisSubscriber.on('message', this.onMessage); } - async fetch(noCache = false): Promise<Meta> { + public async fetch(noCache = false): Promise<Meta> { if (!noCache && this.cache) return this.cache; return await this.db.transaction(async transactionalEntityManager => { @@ -56,8 +64,25 @@ export class MetaService implements OnApplicationShutdown { } }); } - + + private async onMessage(_, data) { + const obj = JSON.parse(data); + + if (obj.channel === 'internal') { + const { type, body } = obj.message; + switch (type) { + case 'metaUpdated': { + this.cache = body; + break; + } + default: + break; + } + } + } + public onApplicationShutdown(signal?: string | undefined) { clearInterval(this.intervalId); + this.redisSubscriber.off('message', this.onMessage); } } |