WIP: for ui package, move the core logic of run core to child process, and rerun run child process in daemon process when uncaught error makes process terminated. TODO: test the flow

This commit is contained in:
geekgeekrun
2024-03-16 03:25:44 +08:00
parent f0e7eadfbc
commit 76db85d01e
6 changed files with 70 additions and 10 deletions
@@ -0,0 +1,8 @@
export enum AUTO_CHAT_ERROR_EXIT_CODE {
NORMAL = 0,
COOKIE_INVALID = 81,
LOGIN_STATUS_INVALID = 82,
ERR_INTERNET_DISCONNECTED = 83,
ACCESS_IS_DENIED = 84,
PUPPETEER_IS_NOT_EXECUTABLE = 85
}
@@ -0,0 +1,50 @@
import path from 'node:path'
import * as url from 'url'
import { sleep } from '@geekgeekrun/utils/sleep.mjs'
import childProcess from 'node:child_process'
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../common/enums/auto-start-chat'
import { app } from 'electron'
const rerunInterval = (() => {
let v = Number(process.env.MAIN_BOSSGEEKGO_RERUN_INTERVAL)
if (isNaN(v)) {
v = 3000
}
return v
})()
function runWithDaemon() {
const subProcessOfCore = childProcess.spawn(process.argv[0], process.argv.slice(1), {
stdio: ['inherit', 'inherit', 'inherit', 'pipe', 'ipc'],
env: {
...process.env,
MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBossMain'
}
})
subProcessOfCore.once('exit', async (exitCode: number) => {
if (
[...Object.values(AUTO_CHAT_ERROR_EXIT_CODE)]
.filter((it) => typeof it === 'number')
.includes(exitCode)
) {
console.log(
`[Run core daemon] Child process exit with reason ${AUTO_CHAT_ERROR_EXIT_CODE[exitCode]}.`
)
process.exit(exitCode)
return
}
console.log(
`[Run core daemon] Child process exit with code ${exitCode}, an internal may not be caught, and will be restarted in ${rerunInterval}ms.`
)
await sleep(rerunInterval)
runWithDaemon()
})
}
export function runAutoChatWithDaemon() {
app.dock?.hide()
process.on('disconnect', () => {
app.exit()
})
runWithDaemon()
}
@@ -6,11 +6,7 @@ import * as fs from 'fs'
import { pipeWriteRegardlessError } from '../utils/pipe'
import { getAnyAvailablePuppeteerExecutable } from '../CHECK_AND_DOWNLOAD_DEPENDENCIES/utils/puppeteer-executable'
import { sleep } from '@geekgeekrun/utils/sleep.mjs'
export enum AUTO_CHAT_ERROR_EXIT_CODE {
PUPPETEER_IS_NOT_EXECUTABLE = 81,
LOGIN_STATUS_INVALID = 82
}
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../common/enums/auto-start-chat'
const { groupRobotAccessToken: dingTalkAccessToken } = readConfigFile('dingtalk.json')
+7 -2
View File
@@ -1,14 +1,19 @@
import { runAutoChat } from './flow/GEEK_AUTO_START_CHAT_WITH_BOSS/index'
import { runAutoChat } from './flow/GEEK_AUTO_START_CHAT_WITH_BOSS_MAIN/index'
import { runAutoChatWithDaemon } from './flow/GEEK_AUTO_START_CHAT_WITH_BOSS_DAEMON/index'
import { openSettingWindow } from './flow/OPEN_SETTING_WINDOW'
import { checkAndDownloadDependenciesForInit } from './flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index'
import { launchBossZhipinLoginPageWithPreloadExtension } from './flow/LAUNCH_BOSS_ZHIPIN_LOGIN_PAGE_WITH_PRELOAD_EXTENSION'
const runMode = process.env.MAIN_BOSSGEEKGO_UI_RUN_MODE
switch (runMode) {
case 'geekAutoStartWithBoss': {
case 'geekAutoStartWithBossMain': {
runAutoChat()
break
}
case 'geekAutoStartWithBossDaemon': {
runAutoChatWithDaemon()
break
}
case 'checkAndDownloadDependenciesForInit': {
checkAndDownloadDependenciesForInit()
break
+2 -2
View File
@@ -16,7 +16,7 @@ import { checkCookieListFormat } from '../../common/utils/cookie'
import { getAnyAvailablePuppeteerExecutable } from '../flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/utils/puppeteer-executable/index'
import { DOWNLOAD_ERROR_EXIT_CODE } from '../flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/index'
import { sleep } from '@geekgeekrun/utils/sleep.mjs'
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../flow/GEEK_AUTO_START_CHAT_WITH_BOSS'
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../common/enums/auto-start-chat'
let mainWindow: BrowserWindow | null = null
export function createMainWindow(): void {
@@ -123,7 +123,7 @@ export function createMainWindow(): void {
}
const subProcessEnv = {
...process.env,
MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBoss',
MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBossDaemon',
PUPPETEER_EXECUTABLE_PATH: puppeteerExecutable.executablePath
}
subProcessOfPuppeteer = childProcess.spawn(process.argv[0], process.argv.slice(1), {