improve power management status update

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-02-10 22:48:52 +00:00
parent 5d42dbe637
commit 6c2d6e9ca2

View File

@ -1,7 +1,5 @@
import {requestPVE, goToPage, instances} from "./utils.js"; import {requestPVE, goToPage, instances} from "./utils.js";
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
export class Instance extends HTMLElement { export class Instance extends HTMLElement {
constructor () { constructor () {
super(); super();
@ -91,38 +89,29 @@ export class Instance extends HTMLElement {
this.update(); 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 { const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
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;
}
while (true) { 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 if(taskStatus.data.status === "stopped" && taskStatus.data.exitstatus === "OK") { // task stopped and was successful
this.status = targetStatus; this.status = targetStatus;
this.update(); this.update();
this.actionLock = false; this.actionLock = false;
return; break;
} }
else if (taskStatus.data.status === "stopped") { // task stopped but was not successful else if (taskStatus.data.status === "stopped") { // task stopped but was not successful
this.status = prevStatus; 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.update();
this.actionLock = false; this.actionLock = false;
return; break;
} }
else{ // task has not stopped else{ // task has not stopped
await waitFor(1000); await waitFor(1000);
} }
} }
} }
} }