From 54f4c3b9231377910fcee2c06bde9475681dc75c Mon Sep 17 00:00:00 2001 From: larssand Date: Thu, 5 Nov 2026 19:48:32 +0100 Subject: [PATCH] fix(sync): retry failed saves every 5 s; restart the decoder simulator with the server (1.65.5) --- CHANGELOG.md | 8 ++++++++ package-lock.json | 4 ++-- package.json | 2 +- server.js | 15 +++++++++++++++ src/app.js | 10 +++++++++- src/decoder_sim_view.js | 10 ++++++++-- src/runtime_services.js | 1 + 7 files changed, 44 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index faff587..b2c3943 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,14 @@ The application version is maintained in `package.json` and displayed in the adm No changes recorded yet. +## 1.65.5 - 2026-11-05 + +### Fixed + +- A save that failed (e.g. while the server restarted) was only tried again at the next change or on "Try again", so "Changes waiting to save" could hang. Failed saves are now tried again every 5 s until they get through. +- The decoder simulator runs inside the server, so `npm restart` stopped it and the app was left pointing at nothing. The server now starts it again on boot when it was on (with the same lap time and spread). +- Stopping the simulator after it had been stopped and started again left the decoder address on the simulator; it now falls back to ws://127.0.0.1:9000 when the real address is unknown. + ## 1.65.4 - 2026-11-05 ### Fixed diff --git a/package-lock.json b/package-lock.json index c90881f..3ccffea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "rc-racecore", - "version": "1.65.4", + "version": "1.65.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "rc-racecore", - "version": "1.65.4", + "version": "1.65.5", "hasInstallScript": true, "dependencies": { "better-sqlite3": "^12.11.1", diff --git a/package.json b/package.json index 1207d86..5b4f438 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rc-racecore", - "version": "1.65.4", + "version": "1.65.5", "private": true, "description": "RC RaceCore live timing and race control with AMMC ingest and SQLite persistence", "engines": { diff --git a/server.js b/server.js index 0303df4..a70af3e 100644 --- a/server.js +++ b/server.js @@ -773,7 +773,22 @@ app.use((_req, res) => { res.sendFile(path.join(__dirname, "index.html")); }); +// The simulator was on before a restart (Settings keep the real decoder address while it is): +// start it again so the app finds it. +function restartDecoderSimIfOn() { + try { + const row = db.prepare("SELECT state_json FROM app_state WHERE id = 1").get(); + const settings = row ? JSON.parse(row.state_json)?.settings : null; + if (!settings?.simPreviousWsUrl && !/:9099\/?$/.test(String(settings?.wsUrl || ""))) return; + decoderSim.start({ port: 9099, host: HOST, lapSec: settings.simLapSec || 18, spreadSec: settings.simSpreadSec ?? 1.5 }) + .catch((error) => console.error(`Decoder simulator could not start again: ${error.message}`)); + } catch (error) { + console.error(`Decoder simulator check failed: ${error.message}`); + } +} + app.listen(PORT, HOST, () => { + restartDecoderSimIfOn(); writePidFile(); console.log(`RC Timing server listening on http://${HOST}:${PORT}`); const lanUrls = getLanUrls(PORT); diff --git a/src/app.js b/src/app.js index 1046332..ec333b7 100644 --- a/src/app.js +++ b/src/app.js @@ -166,7 +166,15 @@ const hydrateFromBackend = ({ initial = false } = {}) => hydrateFromBackendHelpe const isRunningRaceHere = () => !overlayMode && Boolean(getPrologueSession() || pendingStartSessionId || startsInProgress > 0 || (state.decoder?.connected && state.sessions.some((session) => ["running", "paused"].includes(session.status)))); const scheduleBackendSync = () => scheduleBackendSyncHelper({ getBackendSyncTimer: () => backendSyncTimer, setBackendSyncTimer: (value) => { backendSyncTimer = value; }, syncStateToBackend }); -const syncStateToBackend = () => syncStateToBackendHelper({ backend, t, getBackendUrl, buildPersistableState, getLocalStateVersion: () => localStateVersion, getSyncedStateVersion: () => syncedStateVersion, setSyncedStateVersion: (value) => { syncedStateVersion = value; }, getLocalStateDirty: () => localStateDirty, setLocalStateDirty: (value) => { localStateDirty = value; } }); +const syncStateToBackendOnce = () => syncStateToBackendHelper({ backend, t, getBackendUrl, buildPersistableState, getLocalStateVersion: () => localStateVersion, getSyncedStateVersion: () => syncedStateVersion, setSyncedStateVersion: (value) => { syncedStateVersion = value; }, getLocalStateDirty: () => localStateDirty, setLocalStateDirty: (value) => { localStateDirty = value; } }); +// A save that fails (server restarting, network gone) is tried again every 5 s until it gets +// through, so "Changes waiting to save" never hangs when nothing else changes. +let syncRetryTimer = null; +const syncStateToBackend = async () => { + await syncStateToBackendOnce(); + clearTimeout(syncRetryTimer); + syncRetryTimer = backend.writeError && localStateDirty ? setTimeout(() => { if (!backend.pendingWrites) void syncStateToBackend(); }, 5000) : null; +}; const pingBackend = () => pingBackendHelper({ backend, t, getBackendUrl }); const checkAppVersion = () => checkAppVersionHelper({ onContact: (online) => { backend.available = online; diff --git a/src/decoder_sim_view.js b/src/decoder_sim_view.js index 984d6ac..56e4015 100644 --- a/src/decoder_sim_view.js +++ b/src/decoder_sim_view.js @@ -37,10 +37,16 @@ export function mountDecoderSimSettings(root, { state, t, escapeHtml, getBackend if (running) { await call("stop", {}); // Back to the real decoder address. - state.settings.wsUrl = state.settings.simPreviousWsUrl || state.settings.wsUrl; + const fallback = state.settings.wsUrl === simUrl() ? "ws://127.0.0.1:9000" : state.settings.wsUrl; + state.settings.wsUrl = state.settings.simPreviousWsUrl || fallback; delete state.settings.simPreviousWsUrl; } else { - await call("start", { lapSec: Number(root.querySelector("#simLapSec").value), spreadSec: Number(root.querySelector("#simSpreadSec").value) }); + const lapSec = Number(root.querySelector("#simLapSec").value); + const spreadSec = Number(root.querySelector("#simSpreadSec").value); + await call("start", { lapSec, spreadSec }); + // Kept with the settings so the server starts it again after a restart. + state.settings.simLapSec = lapSec; + state.settings.simSpreadSec = spreadSec; if (state.settings.wsUrl !== simUrl()) state.settings.simPreviousWsUrl = state.settings.wsUrl; state.settings.wsUrl = simUrl(); } diff --git a/src/runtime_services.js b/src/runtime_services.js index 18a0af1..6a90032 100644 --- a/src/runtime_services.js +++ b/src/runtime_services.js @@ -134,6 +134,7 @@ export function applyPersistedStateHelper(persisted, deps) { wsUrl: persisted.settings?.wsUrl || state.settings.wsUrl || "ws://127.0.0.1:9000", // The real decoder address while the decoder simulator is on (decoder_sim_view.js). ...(persisted.settings?.simPreviousWsUrl ? { simPreviousWsUrl: String(persisted.settings.simPreviousWsUrl) } : {}), + ...(persisted.settings?.simLapSec ? { simLapSec: Number(persisted.settings.simLapSec), simSpreadSec: Number(persisted.settings.simSpreadSec ?? 1.5) } : {}), splitDecoderEnabled: persisted.settings?.splitDecoderEnabled === true, splitWsUrl: String(persisted.settings?.splitWsUrl || state.settings.splitWsUrl || "ws://127.0.0.1:9001"), pitDecoderEnabled: persisted.settings?.pitDecoderEnabled === true,