Merge pull request #26 from bknd-io/fix/mm-sync

fix: only sync config table once certain it has to happen
This commit is contained in:
dswbx
2024-12-11 18:29:22 +01:00
committed by GitHub
3 changed files with 53 additions and 25 deletions

View File

@@ -5,10 +5,21 @@ const _oldConsoles = {
error: console.error error: console.error
}; };
export async function withDisabledConsole<R>(
fn: () => Promise<R>,
severities: ConsoleSeverity[] = ["log"]
): Promise<R> {
const enable = disableConsoleLog(severities);
const result = await fn();
enable();
return result;
}
export function disableConsoleLog(severities: ConsoleSeverity[] = ["log"]) { export function disableConsoleLog(severities: ConsoleSeverity[] = ["log"]) {
severities.forEach((severity) => { severities.forEach((severity) => {
console[severity] = () => null; console[severity] = () => null;
}); });
return enableConsoleLog;
} }
export function enableConsoleLog() { export function enableConsoleLog() {

View File

@@ -201,7 +201,10 @@ export class Repository<DB = any, TB extends keyof DB = any> implements EmitsEve
} }
}; };
} catch (e) { } catch (e) {
console.error("many error", e, compiled); if (e instanceof Error) {
console.error("[ERROR] Repository.performQuery", e.message);
}
throw e; throw e;
} }
} }

View File

@@ -7,9 +7,11 @@ import {
type Static, type Static,
StringEnum, StringEnum,
Type, Type,
mark,
objectEach, objectEach,
stripMark, stripMark,
transformObject transformObject,
withDisabledConsole
} from "core/utils"; } from "core/utils";
import { import {
type Connection, type Connection,
@@ -23,7 +25,7 @@ import {
} from "data"; } from "data";
import { TransformPersistFailedException } from "data/errors"; import { TransformPersistFailedException } from "data/errors";
import { Hono } from "hono"; import { Hono } from "hono";
import { type Kysely, sql } from "kysely"; import type { Kysely } from "kysely";
import { mergeWith } from "lodash-es"; import { mergeWith } from "lodash-es";
import { CURRENT_VERSION, TABLE_NAME, migrate } from "modules/migrations"; import { CURRENT_VERSION, TABLE_NAME, migrate } from "modules/migrations";
import { AppServer } from "modules/server/AppServer"; import { AppServer } from "modules/server/AppServer";
@@ -73,6 +75,7 @@ export type ModuleManagerOptions = {
) => Promise<void>; ) => Promise<void>;
// base path for the hono instance // base path for the hono instance
basePath?: string; basePath?: string;
trustFetched?: boolean;
}; };
type ConfigTable<Json = ModuleConfigs> = { type ConfigTable<Json = ModuleConfigs> = {
@@ -187,7 +190,10 @@ export class ModuleManager {
} }
async syncConfigTable() { 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() { private rebuildServer() {
@@ -226,6 +232,8 @@ export class ModuleManager {
private async fetch(): Promise<ConfigTable> { private async fetch(): Promise<ConfigTable> {
this.logger.context("fetch").log("fetching"); this.logger.context("fetch").log("fetching");
// disabling console log, because the table might not exist yet
return await withDisabledConsole(async () => {
const startTime = performance.now(); const startTime = performance.now();
const { data: result } = await this.repo().findOne( const { data: result } = await this.repo().findOne(
{ type: "config" }, { type: "config" },
@@ -233,12 +241,14 @@ export class ModuleManager {
sort: { by: "version", dir: "desc" } sort: { by: "version", dir: "desc" }
} }
); );
if (!result) { if (!result) {
throw BkndError.with("no config"); 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; return result as ConfigTable;
}, ["log", "error", "warn"]);
} }
async save() { async save() {
@@ -359,9 +369,6 @@ export class ModuleManager {
configs = _configs; configs = _configs;
this.setConfigs(configs); this.setConfigs(configs);
/* objectEach(configs, (config, key) => {
this.get(key as any).setConfig(config);
}); */
this._version = version; this._version = version;
this.logger.log("migrated to", version); this.logger.log("migrated to", version);
@@ -395,10 +402,11 @@ export class ModuleManager {
return; return;
} }
this.logger.log("building");
const ctx = this.ctx(true); const ctx = this.ctx(true);
for (const key in this.modules) { for (const key in this.modules) {
this.logger.log(`building "${key}"`);
await this.modules[key].setContext(ctx).build(); await this.modules[key].setContext(ctx).build();
this.logger.log("built", key);
} }
this._built = true; this._built = true;
@@ -409,11 +417,6 @@ export class ModuleManager {
this.logger.context("build").log("version", this.version()); this.logger.context("build").log("version", this.version());
this.logger.log("booted with", this._booted_with); 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 no config provided, try fetch from db
if (this.version() === 0) { if (this.version() === 0) {
this.logger.context("no version").log("version is 0"); this.logger.context("no version").log("version is 0");
@@ -422,6 +425,16 @@ export class ModuleManager {
// set version and config from fetched // set version and config from fetched
this._version = result.version; 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); this.setConfigs(result.json);
} catch (e: any) { } catch (e: any) {
this.logger.clear(); // fetch couldn't clear this.logger.clear(); // fetch couldn't clear
@@ -431,6 +444,7 @@ export class ModuleManager {
// we can safely build modules, since config version is up to date // 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) // it's up to date because we use default configs (no fetch result)
this._version = CURRENT_VERSION; this._version = CURRENT_VERSION;
await this.syncConfigTable();
await this.buildModules(); await this.buildModules();
await this.save(); await this.save();