feat: graceful shutdown via /api/shutdown
This commit is contained in:
1 parent
44e03d8e9a
commit
1e92cc85fe
2 files changed
+18
-5
No files matched your search
+7
-4
@@ -39,10 +39,13 @@ function startBackend() {
|
|||||||
|
|
||||||
function stopBackend() {
|
function stopBackend() {
|
||||||
if (!backend) return
|
if (!backend) return
|
||||||
backend.kill('SIGTERM')
|
try { require('http').get('http://localhost:3000/api/shutdown') } catch {}
|
||||||
backend = null
|
setTimeout(() => {
|
||||||
isRunning = false
|
try { backend.kill('SIGTERM') } catch {}
|
||||||
updateMenu()
|
backend = null
|
||||||
|
isRunning = false
|
||||||
|
updateMenu()
|
||||||
|
}, 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
app.whenReady().then(() => {
|
app.whenReady().then(() => {
|
||||||
|
|||||||
+11
-1
@@ -63,9 +63,19 @@ app.get('/web/*', (c) => {
|
|||||||
return c.newResponse(content, 200, { 'Content-Type': MIME[ext] || 'application/octet-stream' })
|
return c.newResponse(content, 200, { 'Content-Type': MIME[ext] || 'application/octet-stream' })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.get('/api/shutdown', (c) => {
|
||||||
|
logger.info('Server', '收到关闭请求,正在停止...')
|
||||||
|
scanner.stop()
|
||||||
|
setTimeout(() => {
|
||||||
|
server.close()
|
||||||
|
process.exit(0)
|
||||||
|
}, 500)
|
||||||
|
return c.json({ success: true, message: '正在关闭' })
|
||||||
|
})
|
||||||
|
|
||||||
const PORT = parseInt(process.env.PORT || '3000', 10)
|
const PORT = parseInt(process.env.PORT || '3000', 10)
|
||||||
|
|
||||||
serve({
|
const server = serve({
|
||||||
fetch: app.fetch,
|
fetch: app.fetch,
|
||||||
port: PORT,
|
port: PORT,
|
||||||
}, (info) => {
|
}, (info) => {
|
||||||
|
|||||||
Reference in new issue
Block a user