more logging, less server load

This commit is contained in:
ryzetech 2023-01-20 09:15:19 +01:00
parent e704f43648
commit ffaa9fc59f

29
app.js
View file

@ -61,47 +61,58 @@ app.use('/graphql', graphqlHTTP({
})); }));
app.listen(config.port || 4000); app.listen(config.port || 4000);
console.log(`Running a GraphQL API server at localhost:${config.port || 4000}/graphql`);
// CHECK LOOP // CHECK LOOP
(async function () { (async function () {
await loop(); await loop();
for await (const time of setInterval(config.checkperiod * 1000)) { for await (const time of setInterval(config.checkperiod * 1000)) {
//console.log("Checking for spaces...");
await loop(); await loop();
} }
})(); })();
async function loop() { async function loop() {
console.log(new Date(), "Checking for spaces...");
let changecount = 0;
for (const space of spaces) { for (const space of spaces) {
//console.log(`Checking ${space.id}...`); //console.log(`Checking ${space.id}...`);
let o = await checkSpace(space); let response = await checkSpace(space);
//console.log(`Space ${space.id} is ${o ? "open" : "closed"}`); //console.log(`Space ${space.id} is ${o ? "open" : "closed"}`);
if (typeof o === "undefined") { continue; } if (typeof response.open === "undefined") {
if (o !== cache.get(space.id)) { console.error(`The space ${space.id} might not be reachable. Please check the endpoint.`);
cache.set(space.id, o); continue;
}
if (JSON.stringify(response) != JSON.stringify(cache.get(space.id))) {
cache.set(space.id, response);
let update = await prisma.space.upsert({ let update = await prisma.space.upsert({
where: { id: space.id }, where: { id: space.id },
update: { open: o }, update: { open: response.open, lastChange: response.lastchange },
create: { id: space.id, open: o, name: space.name } create: { id: space.id, open: response.open, lastChange: response.lastchange, name: space.name }
}); });
changecount++;
} }
} }
console.log(new Date(), "Check complete, updated", changecount, "spaces.");
} }
// HELPER FUNCTIONS // HELPER FUNCTIONS
async function checkSpace(space) { async function checkSpace(space) {
let response, data, open; let response, data, open;
let lastchange = null;
try { try {
response = await fetch(space.endpoint); response = await fetch(space.endpoint);
data = await response.json(); data = await response.json();
} catch (e) { console.error(`The space ${space.id} might not be reachable. Please check the endpoint. Error: ${e}`); } } catch (e) { console.error(`The space ${space.id} might not be reachable. Please check the endpoint. Error: ${e}`); }
if (!space.path) { if (!space.path) {
try { open = data.state.open; } try {
open = data.state.open;
lastchange = new Date(data.state.lastchange*1000);
}
catch { console.error(`The space ${space.id} is not using the SpaceAPI standard. Please specify a path.`); } catch { console.error(`The space ${space.id} is not using the SpaceAPI standard. Please specify a path.`); }
} else { } else {
try { open = (jp.query(data, space.path) == (space.expected ? space.expected : true)); } try { open = (jp.query(data, space.path) == (space.expected ? space.expected : true)); }
catch { console.error(`The space ${space.id} has an invalid JSONPath to the target value. Please use https://jsonpath.com/ to evaluate the path.`); } catch { console.error(`The space ${space.id} has an invalid JSONPath to the target value. Please use https://jsonpath.com/ to evaluate the path.`); }
} }
return open; return { open, lastchange };
} }