fix: http.get error handler, separate stop/exit kill logic
This commit is contained in:
1 parent
1e92cc85fe
commit
3436d5bb6a
1 file changed
+14
-10
+14
-10
@@ -1,5 +1,6 @@
|
||||
const { app, Tray, Menu, nativeImage } = require('electron')
|
||||
const { spawn, exec } = require('child_process')
|
||||
const http = require('http')
|
||||
const path = require('path')
|
||||
|
||||
const PROJECT_DIR = path.resolve(__dirname, '../../project')
|
||||
@@ -18,9 +19,9 @@ function updateMenu() {
|
||||
{ label: '打开页面', click: () => exec('start http://localhost:3000') },
|
||||
{ type: 'separator' },
|
||||
{ label: '启动服务', enabled: !isRunning, click: startBackend },
|
||||
{ label: '停止服务', enabled: isRunning, click: stopBackend },
|
||||
{ label: '停止服务', enabled: isRunning, click: () => stopBackend() },
|
||||
{ type: 'separator' },
|
||||
{ label: '退出', click: () => { stopBackend(); app.quit() } },
|
||||
{ label: '退出', click: () => { killBackend(); app.quit() } },
|
||||
]))
|
||||
}
|
||||
|
||||
@@ -39,13 +40,16 @@ function startBackend() {
|
||||
|
||||
function stopBackend() {
|
||||
if (!backend) return
|
||||
try { require('http').get('http://localhost:3000/api/shutdown') } catch {}
|
||||
setTimeout(() => {
|
||||
try { backend.kill('SIGTERM') } catch {}
|
||||
backend = null
|
||||
isRunning = false
|
||||
updateMenu()
|
||||
}, 1000)
|
||||
http.get('http://localhost:3000/api/shutdown').on('error', () => {})
|
||||
setTimeout(killBackend, 1000)
|
||||
}
|
||||
|
||||
function killBackend() {
|
||||
if (!backend) return
|
||||
try { backend.kill('SIGTERM') } catch {}
|
||||
backend = null
|
||||
isRunning = false
|
||||
updateMenu()
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
@@ -56,4 +60,4 @@ app.whenReady().then(() => {
|
||||
})
|
||||
|
||||
app.on('window-all-closed', () => {})
|
||||
app.on('before-quit', () => stopBackend())
|
||||
app.on('before-quit', () => killBackend())
|
||||
Reference in new issue
Block a user