move find browser logic into util process

This commit is contained in:
geekgeekrun
2026-01-17 23:32:19 +08:00
parent 351f30b824
commit ce32dc4c2f
3 changed files with 15 additions and 21 deletions

View File

@@ -1,17 +1,11 @@
import { AUTO_CHAT_ERROR_EXIT_CODE } from "../../common/enums/auto-start-chat"
import { getAnyAvailablePuppeteerExecutable } from "../flow/CHECK_AND_DOWNLOAD_DEPENDENCIES/utils/puppeteer-executable"
import { sendToDaemon } from "../flow/OPEN_SETTING_WINDOW/connect-to-daemon"
import { saveAndGetCurrentRunRecord } from "../flow/OPEN_SETTING_WINDOW/utils/db"
export async function runCommon ({ mode }) {
const puppeteerExecutable = await getAnyAvailablePuppeteerExecutable()
if (!puppeteerExecutable) {
return Promise.reject('NEED_TO_CHECK_RUNTIME_DEPENDENCIES')
}
const currentRunRecord = (await saveAndGetCurrentRunRecord())?.data
const subProcessEnv = {
...process.env,
PUPPETEER_EXECUTABLE_PATH: puppeteerExecutable.executablePath,
GEEKGEEKRUND_NO_AUTO_RESTART_EXIT_CODE: [
AUTO_CHAT_ERROR_EXIT_CODE.PUPPETEER_IS_NOT_EXECUTABLE,
AUTO_CHAT_ERROR_EXIT_CODE.LOGIN_STATUS_INVALID,

View File

@@ -38,6 +38,13 @@ const initPlugins = (hooks) => {
}
const runAutoChat = async () => {
app.dock?.hide()
const puppeteerExecutable = await getAnyAvailablePuppeteerExecutable()
if (!puppeteerExecutable) {
app.exit(AUTO_CHAT_ERROR_EXIT_CODE.PUPPETEER_IS_NOT_EXECUTABLE)
return
}
process.env.PUPPETEER_EXECUTABLE_PATH = puppeteerExecutable.executablePath
const { initPuppeteer, mainLoop, closeBrowserWindow, autoStartChatEventBus } = await import(
'@geekgeekrun/geek-auto-start-chat-with-boss/index.mjs'
)
@@ -45,20 +52,7 @@ const runAutoChat = async () => {
closeBrowserWindow()
app.exit()
})
app.dock?.hide()
try {
await initPuppeteer()
} catch (err) {
console.error(err)
app.exit(AUTO_CHAT_ERROR_EXIT_CODE.PUPPETEER_IS_NOT_EXECUTABLE)
return
}
const isPuppeteerExecutable = !!(await getAnyAvailablePuppeteerExecutable())
if (!isPuppeteerExecutable) {
app.exit(AUTO_CHAT_ERROR_EXIT_CODE.PUPPETEER_IS_NOT_EXECUTABLE)
return
}
await initPuppeteer()
const hooks = {
puppeteerLaunched: new SyncHook(['browser']),

View File

@@ -27,6 +27,7 @@ import cheerio from 'cheerio'
import { connectToDaemon, sendToDaemon } from '../OPEN_SETTING_WINDOW/connect-to-daemon'
import { pushCurrentPageScreenshot, SCREENSHOT_INTERVAL_MS } from '../../utils/screenshot'
import { checkShouldExit } from '../../utils/worker'
import { getAnyAvailablePuppeteerExecutable } from '../CHECK_AND_DOWNLOAD_DEPENDENCIES/utils/puppeteer-executable'
const throttleIntervalMinutes =
readConfigFile('boss.json').autoReminder?.throttleIntervalMinutes ?? 10
@@ -479,6 +480,11 @@ const rerunInterval = (() => {
export async function runEntry() {
app.dock?.hide()
const puppeteerExecutable = await getAnyAvailablePuppeteerExecutable()
if (!puppeteerExecutable) {
throw new Error(`PUPPETEER_IS_NOT_EXECUTABLE`)
}
process.env.PUPPETEER_EXECUTABLE_PATH = puppeteerExecutable.executablePath
await connectToDaemon()
await sendToDaemon({
type: 'ping'
@@ -519,7 +525,7 @@ export async function runEntry() {
process.exit(AUTO_CHAT_ERROR_EXIT_CODE.ACCESS_IS_DENIED)
break
}
if (err.message.includes(`Could not find Chrome`) || err.message.includes(`no executable was found`)) {
if (err.message.includes(`PUPPETEER_IS_NOT_EXECUTABLE`) || err.message.includes(`Could not find Chrome`) || err.message.includes(`no executable was found`)) {
process.exit(AUTO_CHAT_ERROR_EXIT_CODE.PUPPETEER_IS_NOT_EXECUTABLE)
break
}