From a66048d76816853f7b25cf38a5c5272569a0c49a Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Thu, 22 Feb 2024 19:20:22 +0100 Subject: [PATCH] Potentially fix problems with refreshing HTML/CSS --- README.md | 5 ++-- nodemon.json | 2 +- src/back/html.ts | 64 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a25a895..c3c6dc6 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ - docker - docker-compose (version 2.6 or up) +- tmux ## Installation @@ -11,8 +12,6 @@ npm install ``` -Always use ./npm.sh when installing dependencies. - ## Running the app in development mode ``` @@ -40,5 +39,5 @@ npx playwright install firefox And then ``` -./npm.sh run test +npm run test ``` diff --git a/nodemon.json b/nodemon.json index 7748fd3..5c989ad 100644 --- a/nodemon.json +++ b/nodemon.json @@ -1,3 +1,3 @@ { - "delay": "100" + "delay": "500" } diff --git a/src/back/html.ts b/src/back/html.ts index 7aa383c..e931161 100644 --- a/src/back/html.ts +++ b/src/back/html.ts @@ -85,25 +85,69 @@ export default function html( setTimeout(resolve, time); }); - let last_known_start_timestamp = 0; + const APP_DOWN_ERROR_MESSAGE = "App is currently down"; + + function get_status() { + return fetch("/status.json").then((r) => r.json()); + } + + async function wait_for_run_id_to_change() { + let first_timestamp; + try { + const { started_at, status } = await get_status(); + first_timestamp = started_at; + } catch (e) { + await wait_for_app_to_be_stable(); + return; + } + + if (!first_timestamp) { + throw new Error(APP_DOWN_ERROR_MESSAGE); + } - async function wait_for_app_restart() { while (true) { - const { started_at, status } = await fetch( - "/status.json" - ) - .then((r) => r.json()) - .catch(() => ({ - started_at: last_known_start_timestamp, + const { started_at, status } = + await get_status().catch(() => ({ + started_at: first_timestamp, })); - if (started_at !== last_known_start_timestamp) { - last_known_start_timestamp = started_at; + if (started_at !== first_timestamp) { return; } await sleep(100); } } + async function wait_for_app_to_be_stable(n = 3) { + console.log("Waiting for app to be stable...."); + let counter = 0; + while (true) { + const { status } = await get_status().catch((e) => ({ + status: "down", + })); + if (status == "running") { + console.log(counter); + counter++; + } else { + counter = 0; + } + if (counter == n) { + return; + } + await sleep(100); + } + } + + async function wait_for_app_restart() { + try { + await wait_for_run_id_to_change(); + } catch (e) { + if (e.message !== APP_DOWN_ERROR_MESSAGE) { + throw e; + } + } + await wait_for_app_to_be_stable(); + } + (async function () { const { started_at, status } = await fetch( "/status.json"