fix the issue that launchBossSite process is not exited when main process exited

This commit is contained in:
geekgeekrun
2024-10-27 01:07:15 +08:00
parent 6f61501dfa
commit c4e6503b25

View File

@@ -25,6 +25,7 @@ import { pipeWriteRegardlessError } from '../utils/pipe'
import * as JSONStream from 'JSONStream'
import { ChatStartupFrom } from '@geekgeekrun/sqlite-plugin/dist/entity/ChatStartupLog'
import gtag from '../../utils/gtag'
import { sleep } from '@geekgeekrun/utils/sleep.mjs'
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const isRunFromUi = Boolean(process.env.MAIN_BOSSGEEKGO_UI_RUN_MODE)
@@ -252,3 +253,23 @@ export async function launchBossSite() {
await page.close()
page = tempPage
}
// #region period check is parent process existed
// Store the parent process ID
const parentPID = process.ppid
// Function to check if the parent process is alive
async function periodCheckParentProcess() {
// eslint-disable-next-line no-constant-condition
while (true) {
try {
// Try sending signal 0 to the parent process (this does not terminate the process)
process.kill(parentPID, 0)
} catch (err) {
// If an error is thrown, the parent process doesn't exist anymore
process.exit(0)
}
await sleep(1000)
}
}
periodCheckParentProcess()
// #endregion