2022-09-17 13:27:08 -05:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DataSource } from 'typeorm';
|
2022-09-20 12:35:49 -05:00
|
|
|
import Redis from 'ioredis';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { Meta } from '@/models/entities/Meta.js';
|
2022-09-20 12:52:19 -05:00
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2022-12-04 00:03:09 -06:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-13 17:27:23 -06:00
|
|
|
import { StreamMessages } from '@/server/api/stream/types.js';
|
|
|
|
import type { OnApplicationShutdown } from '@nestjs/common';
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class MetaService implements OnApplicationShutdown {
|
2022-09-18 13:11:50 -05:00
|
|
|
private cache: Meta | undefined;
|
|
|
|
private intervalId: NodeJS.Timer;
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
constructor(
|
2022-09-20 12:35:49 -05:00
|
|
|
@Inject(DI.redisSubscriber)
|
|
|
|
private redisSubscriber: Redis.Redis,
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
@Inject(DI.db)
|
|
|
|
private db: DataSource,
|
2022-09-20 12:52:19 -05:00
|
|
|
|
|
|
|
private globalEventService: GlobalEventService,
|
2022-09-17 13:27:08 -05:00
|
|
|
) {
|
2022-12-04 00:03:09 -06:00
|
|
|
//this.onMessage = this.onMessage.bind(this);
|
2022-09-20 12:35:49 -05:00
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
2022-09-18 13:11:50 -05:00
|
|
|
this.intervalId = setInterval(() => {
|
2022-09-17 13:27:08 -05:00
|
|
|
this.fetch(true).then(meta => {
|
2022-09-20 12:35:49 -05:00
|
|
|
// fetch内でもセットしてるけど仕様変更の可能性もあるため一応
|
2022-09-18 13:11:50 -05:00
|
|
|
this.cache = meta;
|
2022-09-17 13:27:08 -05:00
|
|
|
});
|
2022-09-20 12:35:49 -05:00
|
|
|
}, 1000 * 60 * 5);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
2022-09-20 12:35:49 -05:00
|
|
|
|
|
|
|
this.redisSubscriber.on('message', this.onMessage);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-23 17:12:11 -05:00
|
|
|
private async onMessage(_: string, data: string): Promise<void> {
|
2022-09-20 12:52:19 -05:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
2023-01-13 17:27:23 -06:00
|
|
|
const { type, body } = obj.message as StreamMessages['internal']['payload'];
|
2022-09-20 12:52:19 -05:00
|
|
|
switch (type) {
|
|
|
|
case 'metaUpdated': {
|
|
|
|
this.cache = body;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-20 12:35:49 -05:00
|
|
|
public async fetch(noCache = false): Promise<Meta> {
|
2022-09-18 13:11:50 -05:00
|
|
|
if (!noCache && this.cache) return this.cache;
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
return await this.db.transaction(async transactionalEntityManager => {
|
|
|
|
// 過去のバグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する
|
|
|
|
const metas = await transactionalEntityManager.find(Meta, {
|
|
|
|
order: {
|
|
|
|
id: 'DESC',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const meta = metas[0];
|
|
|
|
|
|
|
|
if (meta) {
|
2022-09-18 13:11:50 -05:00
|
|
|
this.cache = meta;
|
2022-09-17 13:27:08 -05:00
|
|
|
return meta;
|
|
|
|
} else {
|
|
|
|
// metaが空のときfetchMetaが同時に呼ばれるとここが同時に呼ばれてしまうことがあるのでフェイルセーフなupsertを使う
|
|
|
|
const saved = await transactionalEntityManager
|
|
|
|
.upsert(
|
|
|
|
Meta,
|
|
|
|
{
|
|
|
|
id: 'x',
|
|
|
|
},
|
|
|
|
['id'],
|
|
|
|
)
|
|
|
|
.then((x) => transactionalEntityManager.findOneByOrFail(Meta, x.identifiers[0]));
|
|
|
|
|
2022-09-18 13:11:50 -05:00
|
|
|
this.cache = saved;
|
2022-09-17 13:27:08 -05:00
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-09-20 12:35:49 -05:00
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-20 12:52:19 -05:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
});
|
2022-09-20 12:35:49 -05:00
|
|
|
|
2022-09-20 12:52:19 -05:00
|
|
|
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);
|
2022-09-20 12:35:49 -05:00
|
|
|
}
|
2022-09-20 12:52:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.globalEventService.publishInternalEvent('metaUpdated', updated);
|
|
|
|
|
|
|
|
return updated;
|
2022-09-20 12:35:49 -05:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-09-17 13:27:08 -05:00
|
|
|
public onApplicationShutdown(signal?: string | undefined) {
|
2022-09-18 13:11:50 -05:00
|
|
|
clearInterval(this.intervalId);
|
2022-09-20 12:35:49 -05:00
|
|
|
this.redisSubscriber.off('message', this.onMessage);
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|
|
|
|
}
|