From 0aec9f7c6f34ba9ec48589894db54a7a45bdc2ed Mon Sep 17 00:00:00 2001 From: BLCK <123077751+BLCK-B@users.noreply.github.com> Date: Fri, 25 Oct 2024 12:31:35 +0200 Subject: [PATCH] fix #98 - race condition - polling (#102) * fix - polling * lifecycle --- .../MusicReleaseTracker/Core/ValueStore.java | 8 +++++- .../FrontendAPI/ApiController.java | 5 ++++ .../MusicReleaseTracker/GUIController.java | 4 +++ .../com/blck/MusicReleaseTracker/Main.java | 1 + vue/electron-main.js | 26 +++++++++++++------ 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/blck/MusicReleaseTracker/Core/ValueStore.java b/src/main/java/com/blck/MusicReleaseTracker/Core/ValueStore.java index dd2abea..1bcd0ab 100644 --- a/src/main/java/com/blck/MusicReleaseTracker/Core/ValueStore.java +++ b/src/main/java/com/blck/MusicReleaseTracker/Core/ValueStore.java @@ -29,8 +29,14 @@ public class ValueStore { private Path errorLogsPath; private String scrapeDate; private String appVersion; + private boolean backendReady = false; - public ValueStore() { + public boolean isBackendReady() { + return backendReady; + } + + public void setBackendReady() { + backendReady = true; } public String getAppVersion() { diff --git a/src/main/java/com/blck/MusicReleaseTracker/FrontendAPI/ApiController.java b/src/main/java/com/blck/MusicReleaseTracker/FrontendAPI/ApiController.java index 4a0e1f5..e8a37bc 100644 --- a/src/main/java/com/blck/MusicReleaseTracker/FrontendAPI/ApiController.java +++ b/src/main/java/com/blck/MusicReleaseTracker/FrontendAPI/ApiController.java @@ -38,6 +38,11 @@ public ApiController(GUIController guiController) { this.sendRequest = guiController; } + @GetMapping("/isBackendReady") + public boolean isBackendReady() { + return sendRequest.isBackendReady(); + } + @GetMapping("/loadList") public List loadList() { return sendRequest.loadList(); diff --git a/src/main/java/com/blck/MusicReleaseTracker/GUIController.java b/src/main/java/com/blck/MusicReleaseTracker/GUIController.java index 30158fe..e296b6d 100644 --- a/src/main/java/com/blck/MusicReleaseTracker/GUIController.java +++ b/src/main/java/com/blck/MusicReleaseTracker/GUIController.java @@ -55,6 +55,10 @@ public GUIController(ValueStore valueStore, ErrorLogging errorLogging, ScrapePro this.manageDB = manageDB; } + public boolean isBackendReady() { + return store.isBackendReady(); + } + public List loadList() { return DB.getArtistList(); } diff --git a/src/main/java/com/blck/MusicReleaseTracker/Main.java b/src/main/java/com/blck/MusicReleaseTracker/Main.java index ec712d4..c61f2f1 100644 --- a/src/main/java/com/blck/MusicReleaseTracker/Main.java +++ b/src/main/java/com/blck/MusicReleaseTracker/Main.java @@ -84,6 +84,7 @@ public void run(String... args) { startSetup.createPathsAndDirs(); manageDB.migrateDB(); settingsIO.updateSettings(); + store.setBackendReady(); } } diff --git a/vue/electron-main.js b/vue/electron-main.js index e248a3a..324686b 100644 --- a/vue/electron-main.js +++ b/vue/electron-main.js @@ -3,6 +3,8 @@ import { app, BrowserWindow, Menu, shell } from "electron"; import path from "path"; import { fileURLToPath } from "url"; import { spawn } from "child_process"; +import axios from "axios"; +axios.defaults.baseURL = "http://localhost:57782"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -44,18 +46,26 @@ function createWindow() { }); } -app.whenReady().then(() => { - if (process.env.NODE_ENV !== "development") { - externalEXE = spawn("buildResources/MusicReleaseTracker", { - detached: true, - stdio: "ignore", // ignore stdio to prevent blocking - }); +// could be better than this polling +async function checkBackendReady() { + while (true) { + try { + const response = await axios.get("/api/isBackendReady"); + if (response.data === true) break; + } catch (error) { + console.error(); + } + await new Promise((resolve) => setTimeout(resolve, 5)); + } +} - externalEXE.unref(); // allow the parent process to exit independently +app.whenReady().then(async () => { + if (process.env.NODE_ENV !== "development") { + externalEXE = spawn("buildResources/MusicReleaseTracker", { detached: true, stdio: "ignore" }); } + await checkBackendReady(); createWindow(); - app.on("activate", () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow();