This commit is contained in:
2023-09-21 09:38:46 +02:00
parent 006c09ce66
commit e0be278630
2 changed files with 55 additions and 5 deletions

View File

@@ -0,0 +1,45 @@
import type { Config, ServiceConfig } from '$lib/config';
import type {
ServiceData,
ServiceHandler,
ServiceHandlerArgs,
ServiceStatus
} from '$lib/services/service';
import { getServiceHandler } from '$lib/services/services';
import { serverConfig } from './config';
export const serviceData: Array<Array<unknown>> = [];
function wrapGeneric(handler: ServiceHandler): ServiceHandler {
return handler;
}
function pollServices() {
const config: Config = serverConfig();
for (const [i, group] of config.services.entries()) {
if (serviceData[i] == undefined) {
serviceData[i] = [];
}
for (const [j, service] of group.items.entries()) {
let handler = getServiceHandler(service.type || '');
if (handler == undefined) {
handler = wrapGeneric(() => {
return {};
});
}
if (handler.constructor.name !== 'AsyncFucntion') {
handler = wrapGeneric(handler);
}
(handler({ config: service }) as Promise<ServiceData>).then((value) => {
serviceData[i][j] = value;
});
}
}
}
export function initServicePolling() {
setInterval(pollServices, 30000);
}

View File

@@ -1,10 +1,15 @@
import type { ServiceConfig } from '$lib/config';
interface ServiceHandlerArgs {
fetch: typeof fetch;
export interface ServiceHandlerArgs {
config: ServiceConfig;
}
export type ServiceHandler = (
input: ServiceHandlerArgs
) => Record<string, unknown> | Promise<Record<string, unknown>>;
export type ServiceStatus = 'online' | 'offline';
export interface ServiceData {
status?: ServiceStatus;
[x: string]: unknown;
}
export type ServiceHandler = (input: ServiceHandlerArgs) => ServiceData | Promise<ServiceData>;