fix issues with /start /restart and status during restore of status

This commit is contained in:
MCHost
2025-06-22 18:49:03 -04:00
parent bea92f554e
commit 35d6acee9a

View File

@ -403,7 +403,7 @@ document.addEventListener('DOMContentLoaded', () => {
function updateDockerUI(message) {
if (message.error) {
if (elements.serverStatus) elements.serverStatus.textContent = 'Not Running';
toggleSections('error');
toggleSections('Not Running');
return;
}
@ -425,10 +425,10 @@ document.addEventListener('DOMContentLoaded', () => {
}
const status = message.data?.status || 'Unknown';
if (state.serverStatus !== status && elements.serverStatus) {
if (elements.serverStatus) {
elements.serverStatus.textContent = status;
state.serverStatus = status;
toggleSections(status);
toggleSections(status); // Always call toggleSections
}
}
@ -1365,11 +1365,15 @@ document.addEventListener('DOMContentLoaded', () => {
const messageHandler = (event) => {
try {
const message = JSON.parse(event.data);
if (message.type === 'docker' && message.data?.status === 'running') {
if (message.type === 'docker') {
state.serverStatus = message.data?.status || 'Unknown'; // Force update state
elements.serverStatus.textContent = state.serverStatus; // Update UI
toggleSections(state.serverStatus); // Update section visibility
if (message.data?.status === 'running') {
updateNotification(notification, 'Server started successfully', 'success');
toggleSections('running');
ws.removeEventListener('message', messageHandler);
}
}
} catch (error) {
console.error('Error parsing WebSocket message:', error);
}
@ -1380,6 +1384,7 @@ document.addEventListener('DOMContentLoaded', () => {
ws.removeEventListener('message', messageHandler);
if (state.serverStatus !== 'running') {
updateNotification(notification, 'Server failed to start', 'error');
toggleSections(state.serverStatus); // Ensure UI reflects failure
}
}
}, 30000);
@ -1402,13 +1407,43 @@ document.addEventListener('DOMContentLoaded', () => {
document.getElementById('restartBtn').addEventListener('click', async () => {
try {
const notification = showNotification('Restarting server...');
await wsRequest('/restart');
initializeTerminal();
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: 'subscribe', endpoints: ['docker-logs'] }));
ws.send(JSON.stringify({ type: 'subscribe', endpoints: ['docker', 'docker-logs'] }));
const messageHandler = (event) => {
try {
const message = JSON.parse(event.data);
if (message.type === 'docker') {
state.serverStatus = message.data?.status || 'Unknown'; // Force update state
elements.serverStatus.textContent = state.serverStatus; // Update UI
toggleSections(state.serverStatus); // Update section visibility
if (message.data?.status === 'running') {
updateNotification(notification, 'Server restarted successfully', 'success');
ws.removeEventListener('message', messageHandler);
}
}
} catch (error) {
console.error('Error parsing WebSocket message:', error);
}
};
ws.addEventListener('message', messageHandler);
setTimeout(() => {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.removeEventListener('message', messageHandler);
if (state.serverStatus !== 'running') {
updateNotification(notification, 'Server failed to restart', 'error');
toggleSections(state.serverStatus); // Ensure UI reflects failure
}
}
}, 60000); // Increased timeout for restart
} else {
updateNotification(notification, 'WebSocket not connected', 'error');
}
} catch (error) {
console.error('Restart server error:', error);
showNotification(`Failed to restart server: ${error.message}`, 'error');
}
});