From 806b3198a1123828e14c25be72cf399eaee90ef3 Mon Sep 17 00:00:00 2001 From: ryzetech Date: Thu, 19 Jan 2023 08:30:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20moved=20checkloop=20outside=20ma?= =?UTF-8?q?in=20loop=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 30 +++++++++++++++++------------- package.json | 5 +++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app.js b/app.js index 1dadea5..08fd23c 100644 --- a/app.js +++ b/app.js @@ -42,22 +42,26 @@ app.listen(4000); (async function () { for await (const time of setInterval(config.checkperiod * 10)) { console.log("Checking for spaces..."); - for (const space of spaces) { - console.log(`Checking ${space.id}...`); - let o = await checkSpace(space); - console.log(`Space ${space.id} is ${o ? "open" : "closed"}`); - if (o !== cache.get(space.id)) { - cache.set(space.id, o); - let update = await prisma.space.upsert({ - where: { id: space.id }, - update: { open: o }, - create: { id: space.id, open: o } - }); - } - } + await loop(); } })(); +async function loop() { + for (const space of spaces) { + console.log(`Checking ${space.id}...`); + let o = await checkSpace(space); + console.log(`Space ${space.id} is ${o ? "open" : "closed"}`); + if (o !== cache.get(space.id)) { + cache.set(space.id, o); + let update = await prisma.space.upsert({ + where: { id: space.id }, + update: { open: o }, + create: { id: space.id, open: o } + }); + } + } +} + // HELPER FUNCTIONS async function checkSpace(space) { let response, data, open; diff --git a/package.json b/package.json index 7886294..8e362f0 100644 --- a/package.json +++ b/package.json @@ -8,5 +8,10 @@ "node-cache": "^5.1.2", "node-fetch": "2", "prisma": "^4.9.0" + }, + "scripts": { + "start": "node app.js", + "generate": "npx prisma generate dev", + "migrate": "npx prisma migrate" } }