From aa4ea3e0def7ed903bc5564925259a4e6e82d9ee Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Sun, 18 Dec 2022 20:32:05 -0800 Subject: [PATCH] handle instance action update by process id --- scripts/elements.js | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/scripts/elements.js b/scripts/elements.js index dce3983..0fcb953 100644 --- a/scripts/elements.js +++ b/scripts/elements.js @@ -78,25 +78,40 @@ class Instance extends HTMLElement { powerButton.src = "images/actions/loading.svg"; powerButton.alt = `instance is ${targetActionDesc}`; - await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node, vmid: this.vmid}); + let task = await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node, vmid: this.vmid}); while (true) { - let data = await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/current`); - if(data.data.status === targetStatus) { + let taskStatus = await request(`/nodes/${this.node}/tasks/${task.data}/status`); + if(taskStatus.data.status === "stopped" && taskStatus.data.exitstatus === "OK") { + this.status = targetStatus; + + typeImg.src = `images/instances/${this.type}/${this.status}.svg`; + typeImg.alt = `${this.status} instance`; + + powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg"; + powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance"; + + this.actionLock = false; + break; } - await waitFor(1000); - } + else if (taskStatus.data.status === "stopped") { // stopped but not OK -> instance did not change state + typeImg.src = `images/instances/${this.type}/${this.status}.svg`; + typeImg.alt = `${this.status} instance`; - this.status = targetStatus; + powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg"; + powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance"; - typeImg.src = `images/instances/${this.type}/${this.status}.svg`; - typeImg.alt = `${this.status} instance`; + this.actionLock = false; - powerButton.src = this.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg"; - powerButton.alt = this.status === "running" ? "shutdown instance" : "start instance"; + console.error(`attempted to ${targetAction} ${this.vmid} but process returned stopped:${taskStatus.data.exitstatus}`); - this.actionLock = false; + break; + } + else{ + await waitFor(1000); + } + } } });