From 623b2087afe5d2b8f9ff60adbe76fc12a4af8d6a Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Mon, 13 Feb 2023 04:30:41 +0000 Subject: [PATCH] add upid wait for actions which take significant time --- index.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/index.js b/index.js index 66227fa..a7c694f 100644 --- a/index.js +++ b/index.js @@ -38,6 +38,7 @@ app.post("/api/disk/detach", async (req, res) => { if (auth) { let method = req.body.type === "qemu" ? "POST" : "PUT"; let result = await requestPVE(`${vmpath}/config`, method, req.cookies, req.body.action, pveAPIToken); + await handleResponse(req.body.node, result); res.send({auth: auth, status: result.status, data: result.data.data}); } else { @@ -52,6 +53,7 @@ app.post("/api/disk/attach", async (req, res) => { if (auth) { let method = req.body.type === "qemu" ? "POST" : "PUT"; let result = await requestPVE(`${vmpath}/config`, method, req.cookies, req.body.action, pveAPIToken); + await handleResponse(req.body.node, result); res.send({auth: auth, status: result.status, data: result.data.data}); } else { @@ -66,6 +68,7 @@ app.post("/api/disk/resize", async (req, res) => { if (auth) { let method = "PUT"; let result = await requestPVE(`${vmpath}/resize`, method, req.cookies, req.body.action, pveAPIToken); + await handleResponse(req.body.node, result); res.send({auth: auth, status: result.status, data: result.data.data}); } else { @@ -81,6 +84,7 @@ app.post("/api/disk/move", async (req, res) => { if (auth) { let method = "POST"; let result = await requestPVE(`${vmpath}/${route}`, method, req.cookies, req.body.action, pveAPIToken); + await handleResponse(req.body.node, result); res.send({auth: auth, status: result.status, data: result.data.data}); } else { @@ -95,6 +99,7 @@ app.post("/api/disk/delete", async (req, res) => { if (auth) { let method = req.body.type === "qemu" ? "POST" : "PUT"; let result = await requestPVE(`${vmpath}/config`, method, req.cookies, req.body.action, pveAPIToken); + await handleResponse(req.body.node, result); res.send({auth: auth, status: result.status, data: result.data.data}); } else { @@ -150,6 +155,28 @@ async function requestPVE (path, method, cookies, body = null, token = null) { } } +async function handleResponse (node, response) { + const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); + if (response.data.data) { + let upid = response.data.data; + while (true) { + let taskStatus = await requestPVE(`/nodes/${node}/tasks/${upid}/status`, "GET", null, null, pveAPIToken); + if (taskStatus.data.data.status === "stopped" && taskStatus.data.data.exitStatus === "OK") { + return true; + } + else if (taskStatus.data.data.status === "stopped") { + return false; + } + else { + await waitFor(1000); + } + } + } + else { + return true; + } +} + app.listen(listenPort, () => { console.log(`proxmoxaas-api v${api.version} listening on port ${listenPort}`); }); \ No newline at end of file