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 { 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();

View File

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

View File

@@ -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);
}

View File

@@ -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<Array<Partial<ServiceData>>> = [];
export const serviceData: Array<Array<Partial<ServiceData> | Promise<Partial<ServiceData>>>> = [];
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);
}

View File

@@ -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}`);
}
}
})
);
}