Refactors Initinialization.

Now we should take care to initialize in the right order.
This commit is contained in:
2023-09-22 17:46:20 +02:00
parent 8450226211
commit fdf10196a0
5 changed files with 44 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
import { dev } from '$app/environment'; 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 { initServicePolling } from '$lib/server/serviceDataPolling';
import { initComponents, initServices } from '$lib/services/services'; import { initComponents, initServices } from '$lib/services/services';
@@ -7,6 +7,9 @@ if (!dev) {
watchDymamicConfig(); 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 initServices();
await initComponents(); await initComponents();
initConfig();
initServicePolling(); initServicePolling();

View File

@@ -8,8 +8,6 @@
export let status: ServiceStatus | undefined; export let status: ServiceStatus | undefined;
export let data: Record<string, unknown>; export let data: Record<string, unknown>;
console.log(service);
const component = getServiceComponent(service.type || ''); const component = getServiceComponent(service.type || '');
function computeClasses(): string { function computeClasses(): string {

View File

@@ -94,6 +94,7 @@ function injectDefaultServiceConfigs(config: Config) {
continue; continue;
} }
const def = getServiceRecord(service.type).config; const def = getServiceRecord(service.type).config;
console.warn(service, def);
for (const [key, value] of Object.entries(def)) { for (const [key, value] of Object.entries(def)) {
const isUnknownKey = !(key in requiredService); const isUnknownKey = !(key in requiredService);
const keyAlreadyDefined = key in service && service[key] != false; const keyAlreadyDefined = key in service && service[key] != false;
@@ -183,9 +184,19 @@ export function clientConfig(): Config {
return _clientConfig; 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() { export function watchDymamicConfig() {
const __filepath = '/dynamic/config.yml'; const __filepath = '/dynamic/config.yml';
@@ -193,9 +204,7 @@ export function watchDymamicConfig() {
const reloadConfig = async () => { const reloadConfig = async () => {
try { try {
const dynamicConfig = yml.load(await readFile(__filepath, 'utf8')); const dynamicConfig = yml.load(await readFile(__filepath, 'utf8'));
_serverConfig = mergeConfig(defaultConfig, dynamicConfig as SPOJO); buildServerAndClientConfig(dynamicConfig as SPOJO);
_clientConfig = stripPrivateFields(_serverConfig);
initializeServiceData();
} catch (err) { } catch (err) {
console.error('could not read or parse config: ' + err); console.error('could not read or parse config: ' + err);
} }

View File

@@ -3,7 +3,7 @@ import type { ServiceData, ServicePoller } from '$lib/services/service';
import { getServiceRecord } from '$lib/services/services'; import { getServiceRecord } from '$lib/services/services';
import { serverConfig } from './config'; import { serverConfig } from './config';
export const serviceData: Array<Array<Partial<ServiceData>>> = []; export const serviceData: Array<Array<Partial<ServiceData> | Promise<Partial<ServiceData>>>> = [];
function pollServices() { function pollServices() {
const config: Config = serverConfig(); const config: Config = serverConfig();
@@ -11,8 +11,9 @@ function pollServices() {
for (const [i, group] of config.services.entries()) { for (const [i, group] of config.services.entries()) {
for (const [j, service] of group.items.entries()) { for (const [j, service] of group.items.entries()) {
const poller: ServicePoller = getServiceRecord(service.type || '').poll; const poller: ServicePoller = getServiceRecord(service.type || '').poll;
poller(service).then((data: ServiceData) => { serviceData[i][j] = poller(service).then((data: ServiceData) => {
serviceData[i][j] = data; serviceData[i][j] = data;
return data;
}); });
} }
} }
@@ -30,7 +31,6 @@ export function initializeServiceData() {
} }
export function initServicePolling() { export function initServicePolling() {
initializeServiceData();
pollServices(); pollServices();
setInterval(pollServices, 30000); setInterval(pollServices, 30000);
} }

View File

@@ -45,38 +45,34 @@ export function getServiceComponent(type: string): any {
} }
export async function initServices() { 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)) { return Promise.allSettled(
try { Object.entries(modules).map(async ([path, load]) => {
//eslint-disable-next-line @typescript-eslint/no-explicit-any try {
const { poll, config } = (await load()) as any; const { poll, config } = (await load()) as any;
if (poll == undefined && config == undefined) { registerService(path.slice(18, -12), { poll, config });
throw new Error(`${modulePath} does not export 'poll' or '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() { 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)) { return Promise.allSettled(
try { Object.entries(modules).map(async ([path, load]) => {
//eslint-disable-next-line @typescript-eslint/no-explicit-any try {
const module = (await load()) as any; const module = (await load()) as any;
if (module == undefined) {
if (module == undefined) { throw new Error('load returned and undefined value');
throw new Error(`${componentPath} does not load`); }
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}`);
}
}
} }