Skip to content

Commit

Permalink
fixed VM deletion not being detected
Browse files Browse the repository at this point in the history
  • Loading branch information
nishogi committed Jun 6, 2024
1 parent a6a6c4c commit cf10cd3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
50 changes: 33 additions & 17 deletions website/students/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,31 @@ function showSuccessSwal(text) {
}

function pollVMStatus(vmName, expectedStatus) {
return new Promise((resolve) => {
const checkStatus = setInterval(() => {
fetch(`ajax_functions.php?action=checkVMStatus&name=${encodeURIComponent(vmName)}`)
.then(response => response.json())
.then(data => {
if (data.status === expectedStatus) {
clearInterval(checkStatus);
resolve();
}
});
}, 2000); // Check status every 2 seconds
});
}
return new Promise((resolve, reject) => {
const checkStatus = setInterval(() => {
fetch(`ajax_functions.php?action=checkVMStatus&name=${encodeURIComponent(vmName)}`)
.then(response => {
if (response.status === 404) {
clearInterval(checkStatus);
resolve({ status: 'notfound' });
} else if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data.status === expectedStatus) {
clearInterval(checkStatus);
resolve(data);
}
})
.catch(error => {
clearInterval(checkStatus);
reject(error);
});
}, 2000); // Check status every 2 seconds
});
}

function confirmStartVM(name) {
Swal.fire({
Expand Down Expand Up @@ -143,12 +155,16 @@ function confirmDeleteVM(name) {

fetch(`ajax_functions.php?action=deleteVM&name=${encodeURIComponent(name)}`)
.then(() => {
return (existingVM(name) == false);
return pollVMStatus(name, 'notfound');
})
.then(() => {
.then((data) => {
clearTimeout(timeout);
Swal.close();
showSuccessSwal('VM supprimée !');
if (data.status === 'notfound') {
Swal.close();
showSuccessSwal('VM supprimée !');
} else {
throw new Error('Unexpected status: ' + data.status);
}
})
.catch((error) => {
clearTimeout(timeout);
Expand Down
15 changes: 12 additions & 3 deletions website/students/students_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,29 @@ function checkVMStatus($VMId) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$vmStatusResult = curl_exec($curl);
$httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($vmStatusResult === false) {
error_log("CURL error: " . curl_error($curl));
curl_close($curl);
return false;
return ['status' => 'error', 'message' => curl_error($curl)];
}
curl_close($curl);

if ($httpStatus === 404) {
return ['status' => 'notfound'];
}

$statusData = json_decode($vmStatusResult, true);
if (json_last_error() !== JSON_ERROR_NONE) {
error_log("JSON decode error: " . json_last_error_msg());
return false;
return ['status' => 'error', 'message' => json_last_error_msg()];
}

return $statusData['data']['status'];
if (isset($statusData['data']['status'])) {
return ['status' => $statusData['data']['status']];
} else {
return ['status' => 'error', 'message' => 'Unexpected response format'];
}
}

// Function to delete the VM by its name
Expand Down

0 comments on commit cf10cd3

Please sign in to comment.