add upid wait for actions which take significant time

This commit is contained in:
Arthur Lu 2023-02-13 04:30:41 +00:00
parent b3379bfa3c
commit 623b2087af

View File

@ -38,6 +38,7 @@ app.post("/api/disk/detach", async (req, res) => {
if (auth) { if (auth) {
let method = req.body.type === "qemu" ? "POST" : "PUT"; let method = req.body.type === "qemu" ? "POST" : "PUT";
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, req.body.action, pveAPIToken); 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}); res.send({auth: auth, status: result.status, data: result.data.data});
} }
else { else {
@ -52,6 +53,7 @@ app.post("/api/disk/attach", async (req, res) => {
if (auth) { if (auth) {
let method = req.body.type === "qemu" ? "POST" : "PUT"; let method = req.body.type === "qemu" ? "POST" : "PUT";
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, req.body.action, pveAPIToken); 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}); res.send({auth: auth, status: result.status, data: result.data.data});
} }
else { else {
@ -66,6 +68,7 @@ app.post("/api/disk/resize", async (req, res) => {
if (auth) { if (auth) {
let method = "PUT"; let method = "PUT";
let result = await requestPVE(`${vmpath}/resize`, method, req.cookies, req.body.action, pveAPIToken); 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}); res.send({auth: auth, status: result.status, data: result.data.data});
} }
else { else {
@ -81,6 +84,7 @@ app.post("/api/disk/move", async (req, res) => {
if (auth) { if (auth) {
let method = "POST"; let method = "POST";
let result = await requestPVE(`${vmpath}/${route}`, method, req.cookies, req.body.action, pveAPIToken); 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}); res.send({auth: auth, status: result.status, data: result.data.data});
} }
else { else {
@ -95,6 +99,7 @@ app.post("/api/disk/delete", async (req, res) => {
if (auth) { if (auth) {
let method = req.body.type === "qemu" ? "POST" : "PUT"; let method = req.body.type === "qemu" ? "POST" : "PUT";
let result = await requestPVE(`${vmpath}/config`, method, req.cookies, req.body.action, pveAPIToken); 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}); res.send({auth: auth, status: result.status, data: result.data.data});
} }
else { 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, () => { app.listen(listenPort, () => {
console.log(`proxmoxaas-api v${api.version} listening on port ${listenPort}`); console.log(`proxmoxaas-api v${api.version} listening on port ${listenPort}`);
}); });