exit child process while parent process has exited - tip: to make disconnect event being trigger, a ipc channel must be established between parent and child event

This commit is contained in:
geekgeekrun
2024-02-23 10:02:16 +08:00
parent 2c73660625
commit c97ca5f585
4 changed files with 23 additions and 8 deletions

View File

@@ -307,11 +307,15 @@ export async function mainLoop (hooks) {
// ;await browser.close()
} catch (err) {
browser.close()
browse = null
page = null
closeBrowserWindow()
console.error(err)
throw err
}
}
export async function closeBrowserWindow () {
browser?.close()
browse = null
page = null
}

View File

@@ -7,6 +7,7 @@ export enum DOWNLOAD_ERROR_EXIT_CODE {
DOWNLOAD_ERROR = 1
}
export const checkAndDownloadDependenciesForInit = async () => {
process.on('disconnect', () => app.exit())
app.dock.hide()
let pipe: null | net.Socket = null
try {

View File

@@ -13,7 +13,17 @@ const initPlugins = (hooks) => {
new DingtalkPlugin(dingTalkAccessToken).apply(hooks)
}
let isParentProcessDisconnect = false
export const runAutoChat = async () => {
const { initPuppeteer, mainLoop, closeBrowserWindow } = await import(
'@geekgeekrun/geek-auto-start-chat-with-boss/index.mjs'
)
process.on('disconnect', () => {
isParentProcessDisconnect = true
closeBrowserWindow()
app.exit()
})
app.dock.hide()
let pipe: null | net.Socket = null
try {
@@ -27,7 +37,7 @@ export const runAutoChat = async () => {
}) + '\r\n'
)
try {
await (await import('@geekgeekrun/geek-auto-start-chat-with-boss/index.mjs')).initPuppeteer()
await initPuppeteer()
pipe?.write(
JSON.stringify({
type: 'PUPPETEER_INITIALIZE_SUCCESSFULLY'
@@ -44,7 +54,6 @@ export const runAutoChat = async () => {
return
}
const mainLoop = (await import('@geekgeekrun/geek-auto-start-chat-with-boss/index.mjs')).mainLoop
const hooks = {
puppeteerLaunched: new SyncHook(),
pageLoaded: new SyncHook(),
@@ -61,7 +70,7 @@ export const runAutoChat = async () => {
type: 'GEEK_AUTO_START_CHAT_WITH_BOSS_STARTED' //geek-auto-start-chat-with-boss-started
}) + '\r\n'
)
while (true) {
while (!([isParentProcessDisconnect].includes(true))) {
try {
await mainLoop(hooks)
} catch (err) {
@@ -73,4 +82,5 @@ export const runAutoChat = async () => {
// }
}
}
closeBrowserWindow()
}

View File

@@ -99,7 +99,7 @@ export function createMainWindow(): void {
}
subProcessOfPuppeteer = childProcess.spawn(process.argv[0], process.argv.slice(1), {
env: subProcessEnv,
stdio: [null, null, null, 'pipe']
stdio: [null, null, null, 'pipe', 'ipc']
})
console.log(subProcessOfPuppeteer)
return new Promise((resolve, reject) => {
@@ -151,7 +151,7 @@ export function createMainWindow(): void {
process.argv.slice(1),
{
env: subProcessEnv,
stdio: [null, null, null, 'pipe']
stdio: [null, null, null, 'pipe', 'ipc']
}
)
return new Promise((resolve, reject) => {