From c4fb1f7fd883adb51bfb3f3e11b4fbf166eb75c3 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Sat, 18 Oct 2025 18:26:38 -0600 Subject: [PATCH] fixes for polling restore --- themes/grav/app/tools/restore.js | 35 ++++++++++++++++++++++++++++++++ themes/grav/js/admin.min.js | 30 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/themes/grav/app/tools/restore.js b/themes/grav/app/tools/restore.js index 8b5205b7..ca8604a4 100644 --- a/themes/grav/app/tools/restore.js +++ b/themes/grav/app/tools/restore.js @@ -17,6 +17,7 @@ class RestoreManager { constructor() { this.job = null; this.pollTimer = null; + this.pollFailures = 0; $(document).on('click', '[data-restore-snapshot]', (event) => { event.preventDefault(); @@ -62,6 +63,7 @@ class RestoreManager { id: jobId, snapshot, }; + this.pollFailures = 0; const runningMessage = translations.PLUGIN_ADMIN?.RESTORE_GRAV_RUNNING ? translations.PLUGIN_ADMIN.RESTORE_GRAV_RUNNING.replace('%s', snapshot) @@ -89,7 +91,12 @@ class RestoreManager { } const jobId = this.job.id; + let handled = false; + request(`${urls.status}?job=${encodeURIComponent(jobId)}`, { silentErrors: true }, (response) => { + handled = true; + this.pollFailures = 0; + if (!response || response.status !== 'success') { this.schedulePoll(); return; @@ -129,8 +136,36 @@ class RestoreManager { } this.schedulePoll(); + }).then(() => { + if (!handled) { + this.handleSilentFailure(); + } }); } + + handleSilentFailure() { + if (!this.job) { + return; + } + + this.pollFailures += 1; + const snapshot = this.job.snapshot || ''; + + if (this.pollFailures >= 3) { + const message = snapshot + ? `Snapshot ${snapshot} restore is completing. Reloading...` + : 'Snapshot restore is completing. Reloading...'; + toastr.info(message); + this.job = null; + this.clearPoll(); + setTimeout(() => window.location.reload(), 1500); + + return; + } + + const delay = Math.min(5000, 1200 * this.pollFailures); + this.schedulePoll(delay); + } } // Initialize restore manager when tools view loads. diff --git a/themes/grav/js/admin.min.js b/themes/grav/js/admin.min.js index 44e7d338..7b8d2036 100644 --- a/themes/grav/js/admin.min.js +++ b/themes/grav/js/admin.min.js @@ -10988,6 +10988,7 @@ var RestoreManager = /*#__PURE__*/function () { restore_classCallCheck(this, RestoreManager); this.job = null; this.pollTimer = null; + this.pollFailures = 0; external_jQuery_default()(document).on('click', '[data-restore-snapshot]', function (event) { event.preventDefault(); var button = external_jQuery_default()(event.currentTarget); @@ -11037,6 +11038,7 @@ var RestoreManager = /*#__PURE__*/function () { id: jobId, snapshot: snapshot }; + _this2.pollFailures = 0; var runningMessage = (_translations$PLUGIN_4 = external_GravAdmin_namespaceObject.translations.PLUGIN_ADMIN) !== null && _translations$PLUGIN_4 !== void 0 && _translations$PLUGIN_4.RESTORE_GRAV_RUNNING ? external_GravAdmin_namespaceObject.translations.PLUGIN_ADMIN.RESTORE_GRAV_RUNNING.replace('%s', snapshot) : "Restoring snapshot ".concat(snapshot, "..."); utils_toastr.info(runningMessage); _this2.schedulePoll(); @@ -11068,9 +11070,12 @@ var RestoreManager = /*#__PURE__*/function () { return; } var jobId = this.job.id; + var handled = false; utils_request("".concat(urls.status, "?job=").concat(encodeURIComponent(jobId)), { silentErrors: true }, function (response) { + handled = true; + _this4.pollFailures = 0; if (!response || response.status !== 'success') { _this4.schedulePoll(); return; @@ -11109,8 +11114,33 @@ var RestoreManager = /*#__PURE__*/function () { return; } _this4.schedulePoll(); + }).then(function () { + if (!handled) { + _this4.handleSilentFailure(); + } }); } + }, { + key: "handleSilentFailure", + value: function handleSilentFailure() { + if (!this.job) { + return; + } + this.pollFailures += 1; + var snapshot = this.job.snapshot || ''; + if (this.pollFailures >= 3) { + var message = snapshot ? "Snapshot ".concat(snapshot, " restore is completing. Reloading...") : 'Snapshot restore is completing. Reloading...'; + utils_toastr.info(message); + this.job = null; + this.clearPoll(); + setTimeout(function () { + return window.location.reload(); + }, 1500); + return; + } + var delay = Math.min(5000, 1200 * this.pollFailures); + this.schedulePoll(delay); + } }]); }(); // Initialize restore manager when tools view loads. external_jQuery_default()(document).ready(function () {