From 72e935643faa4066e5f5afbaa21ac36090929ba9 Mon Sep 17 00:00:00 2001 From: geekgeekrun Date: Sun, 17 Nov 2024 13:55:51 +0800 Subject: [PATCH] make daemon can pass different run mode --- .../index.ts | 27 ++++++++++++++----- .../flow/OPEN_SETTING_WINDOW/ipc/index.ts | 12 ++++++--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS_DAEMON/index.ts b/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS_DAEMON/index.ts index 28bff81..53a6801 100644 --- a/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS_DAEMON/index.ts +++ b/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS_DAEMON/index.ts @@ -1,7 +1,7 @@ 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' +import { app, dialog } from 'electron' import fs, { WriteStream } from 'node:fs' import { pipeWriteRegardlessError } from '../utils/pipe' import * as JSONStream from 'JSONStream' @@ -10,6 +10,7 @@ import gtag from '../../utils/gtag' import { initDb } from '@geekgeekrun/sqlite-plugin' import { getPublicDbFilePath } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' import { AutoStartChatRunRecord } from '@geekgeekrun/sqlite-plugin/dist/entity/AutoStartChatRunRecord' +import minimist from 'minimist' const rerunInterval = (() => { let v = Number(process.env.MAIN_BOSSGEEKGO_RERUN_INTERVAL) @@ -19,7 +20,7 @@ const rerunInterval = (() => { return v })() -function runWithDaemon({ runRecordId }) { +function runWithDaemon({ runRecordId, runMode }) { const subProcessOfCore = childProcess.spawn( process.argv[0], [...process.argv.slice(1), `--run-record-id=${runRecordId}`], @@ -27,7 +28,7 @@ function runWithDaemon({ runRecordId }) { stdio: ['inherit', 'inherit', 'inherit', 'pipe', 'ipc'], env: { ...process.env, - MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBossMain' + MAIN_BOSSGEEKGO_UI_RUN_MODE: runMode } } ) @@ -66,7 +67,7 @@ function runWithDaemon({ runRecordId }) { `[Run core daemon] Child process exit with code ${exitCode}, an internal error may not be caught, and will be restarted in ${rerunInterval}ms.` ) await sleep(rerunInterval) - runWithDaemon({ runRecordId }) + runWithDaemon({ runRecordId, runMode }) }) } @@ -83,7 +84,21 @@ const clearSuicideTimer = () => { suicideTimer = null } -export function runAutoChatWithDaemon() { +export async function runAutoChatWithDaemon() { + const commandlineArgs = minimist(process.argv.slice(2)) + if (!['geekAutoStartWithBossMain'].includes(commandlineArgs['mode-to-daemon'])) { + await new Promise((resolve) => { + app.once('ready', () => resolve(undefined)) + }) + + dialog.showMessageBoxSync({ + type: 'error', + message: `守护进程不支持 ${commandlineArgs['mode-to-daemon'] ?? '(默认)'} 模式` + }) + app.exit() + return + } + app.dock?.hide() process.on('disconnect', () => { app.exit() @@ -115,7 +130,7 @@ export function runAutoChatWithDaemon() { autoStartChatRunRecord.date = new Date() const autoStartChatRunRecordRepository = ds.getRepository(AutoStartChatRunRecord) const result = await autoStartChatRunRecordRepository.save(autoStartChatRunRecord) - runWithDaemon({ runRecordId: result.id }) + runWithDaemon({ runRecordId: result.id, runMode }) } }) process.on('SIGINT', () => { diff --git a/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/ipc/index.ts b/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/ipc/index.ts index fb1d757..cb5d2af 100644 --- a/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/ipc/index.ts +++ b/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/ipc/index.ts @@ -104,10 +104,14 @@ export default function initIpc() { MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBossDaemon', PUPPETEER_EXECUTABLE_PATH: puppeteerExecutable.executablePath } - subProcessOfPuppeteer = childProcess.spawn(process.argv[0], process.argv.slice(1), { - env: subProcessEnv, - stdio: ['inherit', 'inherit', 'inherit', 'pipe', 'ipc'] - }) + subProcessOfPuppeteer = childProcess.spawn( + process.argv[0], + [...process.argv.slice(1), '--mode-to-daemon=geekAutoStartWithBossMain'], + { + env: subProcessEnv, + stdio: ['inherit', 'inherit', 'inherit', 'pipe', 'ipc'] + } + ) // console.log(subProcessOfPuppeteer) return new Promise((resolve, reject) => { subProcessOfPuppeteer!.stdio[3]!.pipe(JSONStream.parse()).on('data', async (raw) => {