Files
flanders/src/lib/server/serviceData.ts
2023-09-21 09:38:46 +02:00

46 lines
1.1 KiB
TypeScript

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