2022-03-26 06:34:00 +00:00
|
|
|
import { db } from '@/db/postgre.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { Meta } from '@/models/entities/meta.js';
|
2018-11-05 22:14:43 +00:00
|
|
|
|
2019-04-23 23:11:19 +00:00
|
|
|
let cache: Meta;
|
|
|
|
|
|
|
|
export async function fetchMeta(noCache = false): Promise<Meta> {
|
|
|
|
if (!noCache && cache) return cache;
|
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
return await db.transaction(async transactionalEntityManager => {
|
2020-02-05 06:41:14 +00:00
|
|
|
// 過去のバグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する
|
2022-03-27 07:16:13 +00:00
|
|
|
const metas = await transactionalEntityManager.find(Meta, {
|
2019-04-16 15:45:33 +00:00
|
|
|
order: {
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'DESC',
|
|
|
|
},
|
2019-04-16 15:45:33 +00:00
|
|
|
});
|
|
|
|
|
2022-03-27 07:16:13 +00:00
|
|
|
const meta = metas[0];
|
|
|
|
|
2019-04-16 15:45:33 +00:00
|
|
|
if (meta) {
|
2019-04-23 23:11:19 +00:00
|
|
|
cache = meta;
|
2019-04-16 15:45:33 +00:00
|
|
|
return meta;
|
|
|
|
} else {
|
2022-05-14 06:16:45 +00:00
|
|
|
// metaが空のときfetchMetaが同時に呼ばれるとここが同時に呼ばれてしまうことがあるのでフェイルセーフなupsertを使う
|
|
|
|
const saved = await transactionalEntityManager
|
|
|
|
.upsert(
|
|
|
|
Meta,
|
|
|
|
{
|
|
|
|
id: 'x',
|
|
|
|
},
|
|
|
|
['id'],
|
|
|
|
)
|
|
|
|
.then((x) => transactionalEntityManager.findOneByOrFail(Meta, x.identifiers[0]));
|
2019-04-23 23:11:19 +00:00
|
|
|
|
|
|
|
cache = saved;
|
|
|
|
return saved;
|
2019-04-16 15:45:33 +00:00
|
|
|
}
|
|
|
|
});
|
2018-11-05 22:14:43 +00:00
|
|
|
}
|
2019-04-23 23:11:19 +00:00
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
fetchMeta(true).then(meta => {
|
|
|
|
cache = meta;
|
|
|
|
});
|
2021-03-19 09:22:34 +00:00
|
|
|
}, 1000 * 10);
|