Skip to content

Commit

Permalink
fix #98 - race condition - polling (#102)
Browse files Browse the repository at this point in the history
* fix - polling

* lifecycle
  • Loading branch information
BLCK-B authored Oct 25, 2024
1 parent 7ff6a01 commit 0aec9f7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public ApiController(GUIController guiController) {
this.sendRequest = guiController;
}

@GetMapping("/isBackendReady")
public boolean isBackendReady() {
return sendRequest.isBackendReady();
}

@GetMapping("/loadList")
public List<String> loadList() {
return sendRequest.loadList();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/blck/MusicReleaseTracker/GUIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public GUIController(ValueStore valueStore, ErrorLogging errorLogging, ScrapePro
this.manageDB = manageDB;
}

public boolean isBackendReady() {
return store.isBackendReady();
}

public List<String> loadList() {
return DB.getArtistList();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/blck/MusicReleaseTracker/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void run(String... args) {
startSetup.createPathsAndDirs();
manageDB.migrateDB();
settingsIO.updateSettings();
store.setBackendReady();
}
}

Expand Down
26 changes: 18 additions & 8 deletions vue/electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0aec9f7

Please sign in to comment.