fixup server: refactor meta caching

fix: setting meta does not keep cache synced.
fix: handle initially empty meta table.
This commit is contained in:
Johann150 2022-11-16 20:36:22 +01:00
parent 9f6be8d557
commit b958be77b6
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -15,22 +15,36 @@ export async function setMeta(meta: Meta): Promise<void> {
db.manager.clear(Meta); db.manager.clear(Meta);
db.manager.insert(Meta, meta); db.manager.insert(Meta, meta);
cache = meta;
unlock(); unlock();
} }
/** /**
* Performs the primitive database operation to fetch server configuration. * Performs the primitive database operation to fetch server configuration.
* If there is no entry yet, inserts a new one.
* Writes to `cache` instead of returning. * Writes to `cache` instead of returning.
*/ */
async function getMeta(): Promise<void> { async function getMeta(): Promise<void> {
const unlock = await getFetchInstanceMetadataLock('localhost'); const unlock = await getFetchInstanceMetadataLock('localhost');
// new IDs are prioritised because multiple records may have been created due to past bugs // new IDs are prioritised because multiple records may have been created due to past bugs
cache = db.manager.findOne(Meta, { let metas = await db.manager.find(Meta, {
order: { order: {
id: 'DESC', id: 'DESC',
}, },
}); });
if (metas.length === 0) {
db.manager.insert(Meta, {
id: 'x',
});
metas = await db.manager.find(Meta, {
order: {
id: 'DESC',
},
});
}
cache = metas[0];
unlock(); unlock();
} }