forked from imbytecat/fullstack-starter
fix(desktop): ensure sidecar process stops on app shutdown
This commit is contained in:
@@ -44,6 +44,21 @@ const waitForServer = async (
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const stopServerProcess = () => {
|
||||||
|
if (!serverProcess) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const runningServer = serverProcess
|
||||||
|
serverProcess = null
|
||||||
|
|
||||||
|
if (!runningServer.pid || runningServer.exitCode !== null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
runningServer.kill()
|
||||||
|
}
|
||||||
|
|
||||||
const spawnServer = (port: number): string => {
|
const spawnServer = (port: number): string => {
|
||||||
const binaryName = process.platform === 'win32' ? 'server.exe' : 'server'
|
const binaryName = process.platform === 'win32' ? 'server.exe' : 'server'
|
||||||
const binaryPath = join(process.resourcesPath, binaryName)
|
const binaryPath = join(process.resourcesPath, binaryName)
|
||||||
@@ -69,6 +84,10 @@ const spawnServer = (port: number): string => {
|
|||||||
console.error('Failed to start server:', err)
|
console.error('Failed to start server:', err)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
serverProcess.on('exit', () => {
|
||||||
|
serverProcess = null
|
||||||
|
})
|
||||||
|
|
||||||
return `http://127.0.0.1:${port}`
|
return `http://127.0.0.1:${port}`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +115,10 @@ const createWindow = async () => {
|
|||||||
return { action: 'deny' }
|
return { action: 'deny' }
|
||||||
})
|
})
|
||||||
|
|
||||||
|
mainWindow.on('closed', () => {
|
||||||
|
mainWindow = null
|
||||||
|
})
|
||||||
|
|
||||||
if (process.env.ELECTRON_RENDERER_URL) {
|
if (process.env.ELECTRON_RENDERER_URL) {
|
||||||
mainWindow.loadURL(process.env.ELECTRON_RENDERER_URL)
|
mainWindow.loadURL(process.env.ELECTRON_RENDERER_URL)
|
||||||
} else {
|
} else {
|
||||||
@@ -118,6 +141,9 @@ const createWindow = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Loading ${serverUrl}`)
|
console.log(`Loading ${serverUrl}`)
|
||||||
|
if (!mainWindow || mainWindow.isDestroyed()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
mainWindow.loadURL(serverUrl)
|
mainWindow.loadURL(serverUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,6 +151,7 @@ app.whenReady().then(createWindow)
|
|||||||
|
|
||||||
app.on('window-all-closed', () => {
|
app.on('window-all-closed', () => {
|
||||||
if (process.platform !== 'darwin') {
|
if (process.platform !== 'darwin') {
|
||||||
|
stopServerProcess()
|
||||||
app.quit()
|
app.quit()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -136,8 +163,5 @@ app.on('activate', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
app.on('before-quit', () => {
|
app.on('before-quit', () => {
|
||||||
if (serverProcess) {
|
stopServerProcess()
|
||||||
serverProcess.kill()
|
|
||||||
serverProcess = null
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user