This commit is contained in:
binwiederhier
2026-01-18 10:46:15 -05:00
parent 5a1aa68ead
commit 856f150958
9 changed files with 117 additions and 91 deletions

View File

@@ -19,7 +19,11 @@ class Pruner {
}
stopWorker() {
clearTimeout(this.timer);
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
console.log("[VersionChecker] Stopped pruner checker");
}
async prune() {

View File

@@ -1,5 +1,5 @@
/**
* VersionChecker polls the /v1/version endpoint to detect server restarts
* VersionChecker polls the /v1/config endpoint to detect new server versions
* or configuration changes, prompting users to refresh the page.
*/
@@ -9,7 +9,8 @@ class VersionChecker {
constructor() {
this.initialConfigHash = null;
this.listener = null;
this.intervalId = null;
console.log("XXXXXXxxxx set listener null");
this.timer = null;
}
/**
@@ -18,73 +19,52 @@ class VersionChecker {
*/
startWorker() {
// Store initial config hash from the config loaded at page load
this.initialConfigHash = window.config?.config_hash || null;
if (!this.initialConfigHash) {
console.log("[VersionChecker] No initial config_hash found, version checking disabled");
return;
}
console.log("[VersionChecker] Starting version checker with initial hash:", this.initialConfigHash);
// Start polling
this.intervalId = setInterval(() => this.checkVersion(), CHECK_INTERVAL);
this.initialConfigHash = window.config?.config_hash || "";
console.log("[VersionChecker] Starting version checker");
this.timer = setInterval(() => this.checkVersion(), CHECK_INTERVAL);
}
/**
* Stops the version checker worker.
*/
stopWorker() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
console.log("[VersionChecker] Stopped version checker");
}
/**
* Registers a listener that will be called when a version change is detected.
* @param {function} listener - Callback function that receives no arguments
*/
registerListener(listener) {
this.listener = listener;
}
/**
* Resets the listener.
*/
resetListener() {
this.listener = null;
}
/**
* Fetches the current version from the server and compares it with the initial config hash.
*/
async checkVersion() {
if (!this.initialConfigHash) {
return;
}
try {
const response = await fetch(`${window.config?.base_url || ""}/v1/version`);
const response = await fetch(`${window.config?.base_url || ""}/v1/config`);
if (!response.ok) {
console.log("[VersionChecker] Failed to fetch version:", response.status);
console.log("[VersionChecker] Failed to fetch config:", response.status);
return;
}
const data = await response.json();
const currentHash = data.config_hash;
console.log("[VersionChecker] Checked version, initial:", this.initialConfigHash, "current:", currentHash);
if (currentHash && currentHash !== this.initialConfigHash) {
console.log("[VersionChecker] Config hash changed, notifying listener");
console.log("[VersionChecker] Version or config changed, showing banner");
if (this.listener) {
this.listener();
}
} else {
console.log("[VersionChecker] No version change detected");
}
} catch (error) {
console.log("[VersionChecker] Error checking version:", error);
console.log("[VersionChecker] Error checking config:", error);
}
}
}