Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions electron/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,28 @@ if (!gotLock) {
}

// Wait until server is ready
function waitForServer(url) {
return new Promise((resolve) => {
function waitForServer(url, maxRetries = 60) {
return new Promise((resolve, reject) => {
let attempts = 0;
const check = () => {
attempts++;
http
.get(url, () => resolve())
.on('error', () => setTimeout(check, 500));
.on('error', () => {
if (attempts >= maxRetries) {
reject(new Error(`Server failed to start after ${maxRetries} attempts (${maxRetries * 0.5}s)`));
} else {
setTimeout(check, 500);
}
});
};
check();
});
}

// Start Nitro server (production)
function startServer() {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const serverPath = path.join(
process.resourcesPath,
'app.asar.unpacked',
Expand All @@ -62,7 +70,9 @@ function startServer() {
},
});

waitForServer(`http://localhost:${serverPort}`).then(resolve);
waitForServer(`http://localhost:${serverPort}`)
.then(resolve)
.catch(reject);
});
}

Expand Down Expand Up @@ -91,12 +101,30 @@ function createWindow() {

// App start
app.whenReady().then(async () => {
await startServer();
createWindow();
try {
await startServer();
createWindow();
} catch (error) {
console.error('Failed to start server:', error.message);
app.quit();
}
});

// Cleanup
app.on('window-all-closed', () => {
if (serverProcess) serverProcess.kill();
if (process.platform !== 'darwin') app.quit();
});

// Handle process termination signals for clean shutdown
process.on('SIGTERM', () => {
console.log('Received SIGTERM, shutting down gracefully');
if (serverProcess) serverProcess.kill();
app.quit();
});

process.on('SIGINT', () => {
console.log('Received SIGINT, shutting down gracefully');
if (serverProcess) serverProcess.kill();
app.quit();
});
Loading