From 6c2d6e9ca2a8ce5a83b88a0997085d37c1b15e2b Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Fri, 10 Feb 2023 22:48:52 +0000 Subject: [PATCH] improve power management status update Signed-off-by: Arthur Lu --- scripts/elements.js | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/scripts/elements.js b/scripts/elements.js index 114ecea..d03e629 100644 --- a/scripts/elements.js +++ b/scripts/elements.js @@ -1,7 +1,5 @@ import {requestPVE, goToPage, instances} from "./utils.js"; -const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); - export class Instance extends HTMLElement { constructor () { super(); @@ -91,38 +89,29 @@ export class Instance extends HTMLElement { this.update(); - let task; + let result = await requestPVE(`/nodes/${this.node.name}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node.name, vmid: this.vmid}); - try { - task = await requestPVE(`/nodes/${this.node.name}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node.name, vmid: this.vmid}); - } - catch (error) { - this.status = prevStatus; - this.update(); - this.actionLock = false; - console.error(error); - return; - } + const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); while (true) { - let taskStatus = await requestPVE(`/nodes/${this.node.name}/tasks/${task.data}/status`); + let taskStatus = await requestPVE(`/nodes/${this.node.name}/tasks/${result.data}/status`); if(taskStatus.data.status === "stopped" && taskStatus.data.exitstatus === "OK") { // task stopped and was successful this.status = targetStatus; this.update(); this.actionLock = false; - return; + break; } else if (taskStatus.data.status === "stopped") { // task stopped but was not successful this.status = prevStatus; - console.error(`attempted to ${targetAction} ${this.vmid} but process returned stopped:${taskStatus.data.exitstatus}`); + console.error(`attempted to ${targetAction} ${this.vmid} but process returned stopped:${result.data.exitstatus}`); this.update(); this.actionLock = false; - return; + break; } else{ // task has not stopped await waitFor(1000); } - } + } } }