diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2022-09-21 02:52:19 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2022-09-21 02:52:19 +0900 |
| commit | 3bc6205150c627699a9597158bc662ae8a2c0afe (patch) | |
| tree | 3e3c6092af80144b7afc8cb41912545d09c65660 /packages/backend/src/core/MetaService.ts | |
| parent | enhance(backend): metaのポーリング頻度を減らし、redisで更新... (diff) | |
| download | sharkey-3bc6205150c627699a9597158bc662ae8a2c0afe.tar.gz sharkey-3bc6205150c627699a9597158bc662ae8a2c0afe.tar.bz2 sharkey-3bc6205150c627699a9597158bc662ae8a2c0afe.zip | |
refactor(backend): ロジックをサービスに切り出す
Diffstat (limited to 'packages/backend/src/core/MetaService.ts')
| -rw-r--r-- | packages/backend/src/core/MetaService.ts | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/packages/backend/src/core/MetaService.ts b/packages/backend/src/core/MetaService.ts index 5ae9a6fc83..1744f64217 100644 --- a/packages/backend/src/core/MetaService.ts +++ b/packages/backend/src/core/MetaService.ts @@ -3,6 +3,7 @@ import { DataSource } from 'typeorm'; import Redis from 'ioredis'; import { DI } from '@/di-symbols.js'; import { Meta } from '@/models/entities/Meta.js'; +import { GlobalEventService } from '@/core/GlobalEventService.js'; import type { OnApplicationShutdown } from '@nestjs/common'; @Injectable() @@ -16,6 +17,8 @@ export class MetaService implements OnApplicationShutdown { @Inject(DI.db) private db: DataSource, + + private globalEventService: GlobalEventService, ) { this.onMessage = this.onMessage.bind(this); @@ -31,6 +34,22 @@ export class MetaService implements OnApplicationShutdown { this.redisSubscriber.on('message', this.onMessage); } + private async onMessage(_, data): Promise<void> { + 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 async fetch(noCache = false): Promise<Meta> { if (!noCache && this.cache) return this.cache; @@ -65,20 +84,34 @@ export class MetaService implements OnApplicationShutdown { }); } - private async onMessage(_, data) { - const obj = JSON.parse(data); + public async update(data: Partial<Meta>): Promise<Meta> { + const updated = await this.db.transaction(async transactionalEntityManager => { + const metas = await transactionalEntityManager.find(Meta, { + order: { + id: 'DESC', + }, + }); - if (obj.channel === 'internal') { - const { type, body } = obj.message; - switch (type) { - case 'metaUpdated': { - this.cache = body; - break; - } - default: - break; + const meta = metas[0]; + + if (meta) { + await transactionalEntityManager.update(Meta, meta.id, data); + + const metas = await transactionalEntityManager.find(Meta, { + order: { + id: 'DESC', + }, + }); + + return metas[0]; + } else { + return await transactionalEntityManager.save(Meta, data); } - } + }); + + this.globalEventService.publishInternalEvent('metaUpdated', updated); + + return updated; } public onApplicationShutdown(signal?: string | undefined) { |