mirror of
https://github.com/geekgeekrun/geekgeekrun.git
synced 2026-09-08 17:09:07 +08:00
add suicide timer in auto chat main for parent and child process don't have any communication after child process spawned in 10s.
This commit is contained in:
@@ -5,5 +5,6 @@ export enum AUTO_CHAT_ERROR_EXIT_CODE {
|
|||||||
ERR_INTERNET_DISCONNECTED = 83,
|
ERR_INTERNET_DISCONNECTED = 83,
|
||||||
ACCESS_IS_DENIED = 84,
|
ACCESS_IS_DENIED = 84,
|
||||||
PUPPETEER_IS_NOT_EXECUTABLE = 85,
|
PUPPETEER_IS_NOT_EXECUTABLE = 85,
|
||||||
DAEMON_PROCESS_SUICIDE = 86
|
AUTO_START_CHAT_DAEMON_PROCESS_SUICIDE = 86,
|
||||||
|
AUTO_START_CHAT_MAIN_PROCESS_SUICIDE = 87,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { sleep } from '@geekgeekrun/utils/sleep.mjs'
|
|||||||
import childProcess from 'node:child_process'
|
import childProcess from 'node:child_process'
|
||||||
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../common/enums/auto-start-chat'
|
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../common/enums/auto-start-chat'
|
||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import fs from 'node:fs'
|
import fs, { WriteStream } from 'node:fs'
|
||||||
import { pipeWriteRegardlessError } from '../utils/pipe'
|
import { pipeWriteRegardlessError } from '../utils/pipe'
|
||||||
import * as JSONStream from 'JSONStream'
|
import * as JSONStream from 'JSONStream'
|
||||||
|
|
||||||
@@ -23,6 +23,24 @@ function runWithDaemon() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
subProcessOfCore!.stdio[3]!.pipe(JSONStream.parse()).on('data', async (raw) => {
|
||||||
|
const data = raw
|
||||||
|
switch (data.type) {
|
||||||
|
case 'AUTO_START_CHAT_MAIN_PROCESS_STARTUP': {
|
||||||
|
pipeWriteRegardlessError(
|
||||||
|
subProcessOfCore!.stdio[3]! as WriteStream,
|
||||||
|
JSON.stringify({
|
||||||
|
type: 'GEEK_AUTO_START_CHAT_CAN_BE_RUN'
|
||||||
|
})
|
||||||
|
)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
subProcessOfCore.once('exit', async (exitCode: number) => {
|
subProcessOfCore.once('exit', async (exitCode: number) => {
|
||||||
if (
|
if (
|
||||||
[...Object.values(AUTO_CHAT_ERROR_EXIT_CODE)]
|
[...Object.values(AUTO_CHAT_ERROR_EXIT_CODE)]
|
||||||
@@ -47,7 +65,7 @@ function runWithDaemon() {
|
|||||||
let suicideTimer: NodeJS.Timeout | null = null
|
let suicideTimer: NodeJS.Timeout | null = null
|
||||||
const setSuicideTimer = () =>
|
const setSuicideTimer = () =>
|
||||||
(suicideTimer = setTimeout(() => {
|
(suicideTimer = setTimeout(() => {
|
||||||
app.exit(AUTO_CHAT_ERROR_EXIT_CODE.DAEMON_PROCESS_SUICIDE)
|
app.exit(AUTO_CHAT_ERROR_EXIT_CODE.AUTO_START_CHAT_DAEMON_PROCESS_SUICIDE)
|
||||||
}, 10000))
|
}, 10000))
|
||||||
const clearSuicideTimer = () => {
|
const clearSuicideTimer = () => {
|
||||||
if (suicideTimer) {
|
if (suicideTimer) {
|
||||||
@@ -90,7 +108,7 @@ export function runAutoChatWithDaemon() {
|
|||||||
pipeWriteRegardlessError(
|
pipeWriteRegardlessError(
|
||||||
pipe,
|
pipe,
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: 'DAEMON_PROCESS_STARTUP'
|
type: 'AUTO_START_CHAT_DAEMON_PROCESS_STARTUP'
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { pipeWriteRegardlessError } from '../utils/pipe'
|
|||||||
import { getAnyAvailablePuppeteerExecutable } from '../CHECK_AND_DOWNLOAD_DEPENDENCIES/utils/puppeteer-executable'
|
import { getAnyAvailablePuppeteerExecutable } from '../CHECK_AND_DOWNLOAD_DEPENDENCIES/utils/puppeteer-executable'
|
||||||
import { sleep } from '@geekgeekrun/utils/sleep.mjs'
|
import { sleep } from '@geekgeekrun/utils/sleep.mjs'
|
||||||
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../common/enums/auto-start-chat'
|
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../common/enums/auto-start-chat'
|
||||||
|
import * as JSONStream from 'JSONStream'
|
||||||
|
|
||||||
import SqlitePluginModule from '@geekgeekrun/sqlite-plugin'
|
import SqlitePluginModule from '@geekgeekrun/sqlite-plugin'
|
||||||
const { default: SqlitePlugin } = SqlitePluginModule
|
const { default: SqlitePlugin } = SqlitePluginModule
|
||||||
@@ -32,13 +33,15 @@ const initPlugins = (hooks) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let isParentProcessDisconnect = false
|
let isParentProcessDisconnect = false
|
||||||
|
process.once('disconnect', () => {
|
||||||
|
isParentProcessDisconnect = true
|
||||||
|
})
|
||||||
|
|
||||||
export const runAutoChat = async () => {
|
const runAutoChat = async () => {
|
||||||
const { initPuppeteer, mainLoop, closeBrowserWindow, autoStartChatEventBus } = await import(
|
const { initPuppeteer, mainLoop, closeBrowserWindow, autoStartChatEventBus } = await import(
|
||||||
'@geekgeekrun/geek-auto-start-chat-with-boss/index.mjs'
|
'@geekgeekrun/geek-auto-start-chat-with-boss/index.mjs'
|
||||||
)
|
)
|
||||||
process.on('disconnect', () => {
|
process.on('disconnect', () => {
|
||||||
isParentProcessDisconnect = true
|
|
||||||
closeBrowserWindow()
|
closeBrowserWindow()
|
||||||
app.exit()
|
app.exit()
|
||||||
})
|
})
|
||||||
@@ -130,3 +133,46 @@ export const runAutoChat = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// suicide timer for parent and child process don't have any communication after child process spawned.
|
||||||
|
let suicideTimer: NodeJS.Timeout | null = null
|
||||||
|
const setSuicideTimer = () =>
|
||||||
|
(suicideTimer = setTimeout(() => {
|
||||||
|
app.exit(AUTO_CHAT_ERROR_EXIT_CODE.AUTO_START_CHAT_MAIN_PROCESS_SUICIDE)
|
||||||
|
}, 10000))
|
||||||
|
const clearSuicideTimer = () => {
|
||||||
|
if (suicideTimer) {
|
||||||
|
clearTimeout(suicideTimer)
|
||||||
|
}
|
||||||
|
suicideTimer = null
|
||||||
|
}
|
||||||
|
|
||||||
|
export const waitForProcessHandShakeAndRunAutoChat = () => {
|
||||||
|
setSuicideTimer()
|
||||||
|
|
||||||
|
const pipeForRead: fs.ReadStream = fs.createReadStream(null, { fd: 3 })
|
||||||
|
const pipeForReadWithJsonParser = pipeForRead.pipe(JSONStream.parse())
|
||||||
|
pipeForReadWithJsonParser?.on('data', function waitForCanRun(data) {
|
||||||
|
if (data.type === 'GEEK_AUTO_START_CHAT_CAN_BE_RUN') {
|
||||||
|
pipeForReadWithJsonParser.off('data', waitForCanRun)
|
||||||
|
clearSuicideTimer()
|
||||||
|
runAutoChat()
|
||||||
|
|
||||||
|
// if don't call close, when kill child process, child process will ANR.
|
||||||
|
pipeForRead.close()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
let pipe: null | fs.WriteStream = null
|
||||||
|
try {
|
||||||
|
pipe = fs.createWriteStream(null, { fd: 3 })
|
||||||
|
} catch {
|
||||||
|
console.error('pipe is not available')
|
||||||
|
app.exit(1)
|
||||||
|
}
|
||||||
|
pipeWriteRegardlessError(
|
||||||
|
pipe,
|
||||||
|
JSON.stringify({
|
||||||
|
type: 'AUTO_START_CHAT_MAIN_PROCESS_STARTUP'
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ const runMode = process.env.MAIN_BOSSGEEKGO_UI_RUN_MODE
|
|||||||
;(async () => {
|
;(async () => {
|
||||||
switch (runMode) {
|
switch (runMode) {
|
||||||
case 'geekAutoStartWithBossMain': {
|
case 'geekAutoStartWithBossMain': {
|
||||||
const { runAutoChat } = await import('./flow/GEEK_AUTO_START_CHAT_WITH_BOSS_MAIN/index')
|
const { waitForProcessHandShakeAndRunAutoChat } = await import(
|
||||||
runAutoChat()
|
'./flow/GEEK_AUTO_START_CHAT_WITH_BOSS_MAIN/index'
|
||||||
|
)
|
||||||
|
waitForProcessHandShakeAndRunAutoChat()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case 'geekAutoStartWithBossDaemon': {
|
case 'geekAutoStartWithBossDaemon': {
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ export function createMainWindow(): void {
|
|||||||
subProcessOfPuppeteer!.stdio[3]!.pipe(JSONStream.parse()).on('data', async (raw) => {
|
subProcessOfPuppeteer!.stdio[3]!.pipe(JSONStream.parse()).on('data', async (raw) => {
|
||||||
const data = raw
|
const data = raw
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case 'DAEMON_PROCESS_STARTUP': {
|
case 'AUTO_START_CHAT_DAEMON_PROCESS_STARTUP': {
|
||||||
subProcessOfPuppeteer!.stdio[3]!.write(
|
subProcessOfPuppeteer!.stdio[3]!.write(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: 'GEEK_AUTO_START_CHAT_CAN_BE_RUN'
|
type: 'GEEK_AUTO_START_CHAT_CAN_BE_RUN'
|
||||||
|
|||||||
Reference in New Issue
Block a user