From fdf10196a05fd263b9ffb904c814cd359d0fdebd Mon Sep 17 00:00:00 2001 From: Alexandre Tuleu Date: Fri, 22 Sep 2023 17:46:20 +0200 Subject: [PATCH] Refactors Initinialization. Now we should take care to initialize in the right order. --- src/hooks.server.ts | 5 ++- src/lib/components/ServiceCard.svelte | 2 -- src/lib/server/config.ts | 19 +++++++--- src/lib/server/serviceDataPolling.ts | 6 ++-- src/lib/services/services.ts | 50 ++++++++++++--------------- 5 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 6d3f501..e60c4f5 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,5 +1,5 @@ import { dev } from '$app/environment'; -import { watchDymamicConfig } from '$lib/server/config'; +import { initConfig, watchDymamicConfig } from '$lib/server/config'; import { initServicePolling } from '$lib/server/serviceDataPolling'; import { initComponents, initServices } from '$lib/services/services'; @@ -7,6 +7,9 @@ if (!dev) { watchDymamicConfig(); } +// Order does matter here, we want the default data , loaded by initServices to be available when +// the config is built. and polling need a built config too. await initServices(); await initComponents(); +initConfig(); initServicePolling(); diff --git a/src/lib/components/ServiceCard.svelte b/src/lib/components/ServiceCard.svelte index 4a192ed..ee6e1b0 100644 --- a/src/lib/components/ServiceCard.svelte +++ b/src/lib/components/ServiceCard.svelte @@ -8,8 +8,6 @@ export let status: ServiceStatus | undefined; export let data: Record; - console.log(service); - const component = getServiceComponent(service.type || ''); function computeClasses(): string { diff --git a/src/lib/server/config.ts b/src/lib/server/config.ts index 03c95e7..76a6ad1 100644 --- a/src/lib/server/config.ts +++ b/src/lib/server/config.ts @@ -94,6 +94,7 @@ function injectDefaultServiceConfigs(config: Config) { continue; } const def = getServiceRecord(service.type).config; + console.warn(service, def); for (const [key, value] of Object.entries(def)) { const isUnknownKey = !(key in requiredService); const keyAlreadyDefined = key in service && service[key] != false; @@ -183,9 +184,19 @@ export function clientConfig(): Config { return _clientConfig; } -let _serverConfig = mergeConfig(defaultConfig, configData); +let _serverConfig: Config = defaultConfig; -let _clientConfig = stripPrivateFields(_serverConfig); +let _clientConfig: Config = defaultConfig; + +function buildServerAndClientConfig(config: SPOJO) { + _serverConfig = mergeConfig(defaultConfig, config); + _clientConfig = stripPrivateFields(_serverConfig); + initializeServiceData(); +} + +export function initConfig() { + buildServerAndClientConfig(configData); +} export function watchDymamicConfig() { const __filepath = '/dynamic/config.yml'; @@ -193,9 +204,7 @@ export function watchDymamicConfig() { const reloadConfig = async () => { try { const dynamicConfig = yml.load(await readFile(__filepath, 'utf8')); - _serverConfig = mergeConfig(defaultConfig, dynamicConfig as SPOJO); - _clientConfig = stripPrivateFields(_serverConfig); - initializeServiceData(); + buildServerAndClientConfig(dynamicConfig as SPOJO); } catch (err) { console.error('could not read or parse config: ' + err); } diff --git a/src/lib/server/serviceDataPolling.ts b/src/lib/server/serviceDataPolling.ts index 3b9e807..be89288 100644 --- a/src/lib/server/serviceDataPolling.ts +++ b/src/lib/server/serviceDataPolling.ts @@ -3,7 +3,7 @@ import type { ServiceData, ServicePoller } from '$lib/services/service'; import { getServiceRecord } from '$lib/services/services'; import { serverConfig } from './config'; -export const serviceData: Array>> = []; +export const serviceData: Array | Promise>>> = []; function pollServices() { const config: Config = serverConfig(); @@ -11,8 +11,9 @@ function pollServices() { for (const [i, group] of config.services.entries()) { for (const [j, service] of group.items.entries()) { const poller: ServicePoller = getServiceRecord(service.type || '').poll; - poller(service).then((data: ServiceData) => { + serviceData[i][j] = poller(service).then((data: ServiceData) => { serviceData[i][j] = data; + return data; }); } } @@ -30,7 +31,6 @@ export function initializeServiceData() { } export function initServicePolling() { - initializeServiceData(); pollServices(); setInterval(pollServices, 30000); } diff --git a/src/lib/services/services.ts b/src/lib/services/services.ts index d199450..0708b68 100644 --- a/src/lib/services/services.ts +++ b/src/lib/services/services.ts @@ -45,38 +45,34 @@ export function getServiceComponent(type: string): any { } export async function initServices() { - const services = import.meta.glob('/src/lib/services/**/+service.ts'); + const modules = import.meta.glob('/src/lib/services/**/+service.ts'); - for (const [modulePath, load] of Object.entries(services)) { - try { - //eslint-disable-next-line @typescript-eslint/no-explicit-any - const { poll, config } = (await load()) as any; - if (poll == undefined && config == undefined) { - throw new Error(`${modulePath} does not export 'poll' or 'config'`); + return Promise.allSettled( + Object.entries(modules).map(async ([path, load]) => { + try { + const { poll, config } = (await load()) as any; + registerService(path.slice(18, -12), { poll, config }); + } catch (error) { + console.warn(`could not load ${path}: ${error}`); } - const typeName = modulePath.slice(18, -12); - registerService(typeName, { poll, config }); - } catch (err) { - console.error(`Could not load service definition from '${modulePath}': ${err}`); - } - } + }) + ); } export async function initComponents() { - const services = import.meta.glob('/src/lib/services/**/+content.svelte'); + const modules = import.meta.glob('/src/lib/services/**/+content.svelte'); - for (const [componentPath, load] of Object.entries(services)) { - try { - //eslint-disable-next-line @typescript-eslint/no-explicit-any - const module = (await load()) as any; - - if (module == undefined) { - throw new Error(`${componentPath} does not load`); + return Promise.allSettled( + Object.entries(modules).map(async ([path, load]) => { + try { + const module = (await load()) as any; + if (module == undefined) { + throw new Error('load returned and undefined value'); + } + registerComponent(path.slice(18, -16), module.default); + } catch (error) { + console.warn(`could not load ${path}: ${error}`); } - const typeName = componentPath.slice(18, -16); - registerComponent(typeName, module.default); - } catch (err) { - console.error(`Could not load component definition from '${componentPath}': ${err}`); - } - } + }) + ); }