From 2395d7fe97f611a3df95492018223f429b1f1def Mon Sep 17 00:00:00 2001 From: dswbx Date: Wed, 11 Dec 2024 15:51:00 +0100 Subject: [PATCH 1/2] fix: only sync config table once certain it has to happen --- app/src/modules/ModuleManager.ts | 33 ++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/app/src/modules/ModuleManager.ts b/app/src/modules/ModuleManager.ts index e43dfa7..5196c1f 100644 --- a/app/src/modules/ModuleManager.ts +++ b/app/src/modules/ModuleManager.ts @@ -7,6 +7,7 @@ import { type Static, StringEnum, Type, + mark, objectEach, stripMark, transformObject @@ -23,7 +24,7 @@ import { } from "data"; import { TransformPersistFailedException } from "data/errors"; import { Hono } from "hono"; -import { type Kysely, sql } from "kysely"; +import type { Kysely } from "kysely"; import { mergeWith } from "lodash-es"; import { CURRENT_VERSION, TABLE_NAME, migrate } from "modules/migrations"; import { AppServer } from "modules/server/AppServer"; @@ -73,6 +74,7 @@ export type ModuleManagerOptions = { ) => Promise; // base path for the hono instance basePath?: string; + trustFetched?: boolean; }; type ConfigTable = { @@ -187,7 +189,10 @@ export class ModuleManager { } async syncConfigTable() { - return await this.__em.schema().sync({ force: true }); + this.logger.context("sync").log("start"); + const result = await this.__em.schema().sync({ force: true }); + this.logger.log("done").clear(); + return result; } private rebuildServer() { @@ -237,7 +242,7 @@ export class ModuleManager { throw BkndError.with("no config"); } - this.logger.log("took", performance.now() - startTime, "ms", result).clear(); + this.logger.log("took", performance.now() - startTime, "ms", result.version).clear(); return result as ConfigTable; } @@ -359,9 +364,6 @@ export class ModuleManager { configs = _configs; this.setConfigs(configs); - /* objectEach(configs, (config, key) => { - this.get(key as any).setConfig(config); - }); */ this._version = version; this.logger.log("migrated to", version); @@ -395,10 +397,11 @@ export class ModuleManager { return; } + this.logger.log("building"); const ctx = this.ctx(true); for (const key in this.modules) { - this.logger.log(`building "${key}"`); await this.modules[key].setContext(ctx).build(); + this.logger.log("built", key); } this._built = true; @@ -409,11 +412,6 @@ export class ModuleManager { this.logger.context("build").log("version", this.version()); this.logger.log("booted with", this._booted_with); - // @todo: check this, because you could start without an initial config - if (this.version() !== CURRENT_VERSION) { - await this.syncConfigTable(); - } - // if no config provided, try fetch from db if (this.version() === 0) { this.logger.context("no version").log("version is 0"); @@ -422,6 +420,16 @@ export class ModuleManager { // set version and config from fetched this._version = result.version; + + if (this.version() !== CURRENT_VERSION) { + await this.syncConfigTable(); + } + + if (this.options?.trustFetched === true) { + this.logger.log("trusting fetched config (mark)"); + mark(result.json); + } + this.setConfigs(result.json); } catch (e: any) { this.logger.clear(); // fetch couldn't clear @@ -431,6 +439,7 @@ export class ModuleManager { // we can safely build modules, since config version is up to date // it's up to date because we use default configs (no fetch result) this._version = CURRENT_VERSION; + await this.syncConfigTable(); await this.buildModules(); await this.save(); From d81b3acb94d97211d9215b548280519767dd41e7 Mon Sep 17 00:00:00 2001 From: dswbx Date: Wed, 11 Dec 2024 18:25:31 +0100 Subject: [PATCH 2/2] mm: disable console as it might error trying to fetch config --- app/src/core/utils/test.ts | 11 ++++++++ app/src/data/entities/query/Repository.ts | 5 +++- app/src/modules/ModuleManager.ts | 31 +++++++++++++---------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/app/src/core/utils/test.ts b/app/src/core/utils/test.ts index 3753944..cf33e1a 100644 --- a/app/src/core/utils/test.ts +++ b/app/src/core/utils/test.ts @@ -5,10 +5,21 @@ const _oldConsoles = { error: console.error }; +export async function withDisabledConsole( + fn: () => Promise, + severities: ConsoleSeverity[] = ["log"] +): Promise { + const enable = disableConsoleLog(severities); + const result = await fn(); + enable(); + return result; +} + export function disableConsoleLog(severities: ConsoleSeverity[] = ["log"]) { severities.forEach((severity) => { console[severity] = () => null; }); + return enableConsoleLog; } export function enableConsoleLog() { diff --git a/app/src/data/entities/query/Repository.ts b/app/src/data/entities/query/Repository.ts index 8156869..f5b576c 100644 --- a/app/src/data/entities/query/Repository.ts +++ b/app/src/data/entities/query/Repository.ts @@ -201,7 +201,10 @@ export class Repository implements EmitsEve } }; } catch (e) { - console.error("many error", e, compiled); + if (e instanceof Error) { + console.error("[ERROR] Repository.performQuery", e.message); + } + throw e; } } diff --git a/app/src/modules/ModuleManager.ts b/app/src/modules/ModuleManager.ts index 5196c1f..db7b285 100644 --- a/app/src/modules/ModuleManager.ts +++ b/app/src/modules/ModuleManager.ts @@ -10,7 +10,8 @@ import { mark, objectEach, stripMark, - transformObject + transformObject, + withDisabledConsole } from "core/utils"; import { type Connection, @@ -231,19 +232,23 @@ export class ModuleManager { private async fetch(): Promise { this.logger.context("fetch").log("fetching"); - const startTime = performance.now(); - const { data: result } = await this.repo().findOne( - { type: "config" }, - { - sort: { by: "version", dir: "desc" } - } - ); - if (!result) { - throw BkndError.with("no config"); - } + // disabling console log, because the table might not exist yet + return await withDisabledConsole(async () => { + const startTime = performance.now(); + const { data: result } = await this.repo().findOne( + { type: "config" }, + { + sort: { by: "version", dir: "desc" } + } + ); - this.logger.log("took", performance.now() - startTime, "ms", result.version).clear(); - return result as ConfigTable; + if (!result) { + throw BkndError.with("no config"); + } + + this.logger.log("took", performance.now() - startTime, "ms", result.version).clear(); + return result as ConfigTable; + }, ["log", "error", "warn"]); } async save() {