make daemon can pass different run mode

This commit is contained in:
geekgeekrun
2024-11-23 13:30:17 +08:00
parent 719545fb0a
commit 72e935643f
2 changed files with 29 additions and 10 deletions
@@ -1,7 +1,7 @@
import { sleep } from '@geekgeekrun/utils/sleep.mjs' 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, dialog } from 'electron'
import fs, { WriteStream } 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'
@@ -10,6 +10,7 @@ import gtag from '../../utils/gtag'
import { initDb } from '@geekgeekrun/sqlite-plugin' import { initDb } from '@geekgeekrun/sqlite-plugin'
import { getPublicDbFilePath } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' import { getPublicDbFilePath } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs'
import { AutoStartChatRunRecord } from '@geekgeekrun/sqlite-plugin/dist/entity/AutoStartChatRunRecord' import { AutoStartChatRunRecord } from '@geekgeekrun/sqlite-plugin/dist/entity/AutoStartChatRunRecord'
import minimist from 'minimist'
const rerunInterval = (() => { const rerunInterval = (() => {
let v = Number(process.env.MAIN_BOSSGEEKGO_RERUN_INTERVAL) let v = Number(process.env.MAIN_BOSSGEEKGO_RERUN_INTERVAL)
@@ -19,7 +20,7 @@ const rerunInterval = (() => {
return v return v
})() })()
function runWithDaemon({ runRecordId }) { function runWithDaemon({ runRecordId, runMode }) {
const subProcessOfCore = childProcess.spawn( const subProcessOfCore = childProcess.spawn(
process.argv[0], process.argv[0],
[...process.argv.slice(1), `--run-record-id=${runRecordId}`], [...process.argv.slice(1), `--run-record-id=${runRecordId}`],
@@ -27,7 +28,7 @@ function runWithDaemon({ runRecordId }) {
stdio: ['inherit', 'inherit', 'inherit', 'pipe', 'ipc'], stdio: ['inherit', 'inherit', 'inherit', 'pipe', 'ipc'],
env: { env: {
...process.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.` `[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) await sleep(rerunInterval)
runWithDaemon({ runRecordId }) runWithDaemon({ runRecordId, runMode })
}) })
} }
@@ -83,7 +84,21 @@ const clearSuicideTimer = () => {
suicideTimer = null 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() app.dock?.hide()
process.on('disconnect', () => { process.on('disconnect', () => {
app.exit() app.exit()
@@ -115,7 +130,7 @@ export function runAutoChatWithDaemon() {
autoStartChatRunRecord.date = new Date() autoStartChatRunRecord.date = new Date()
const autoStartChatRunRecordRepository = ds.getRepository(AutoStartChatRunRecord) const autoStartChatRunRecordRepository = ds.getRepository(AutoStartChatRunRecord)
const result = await autoStartChatRunRecordRepository.save(autoStartChatRunRecord) const result = await autoStartChatRunRecordRepository.save(autoStartChatRunRecord)
runWithDaemon({ runRecordId: result.id }) runWithDaemon({ runRecordId: result.id, runMode })
} }
}) })
process.on('SIGINT', () => { process.on('SIGINT', () => {
@@ -104,10 +104,14 @@ export default function initIpc() {
MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBossDaemon', MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBossDaemon',
PUPPETEER_EXECUTABLE_PATH: puppeteerExecutable.executablePath PUPPETEER_EXECUTABLE_PATH: puppeteerExecutable.executablePath
} }
subProcessOfPuppeteer = childProcess.spawn(process.argv[0], process.argv.slice(1), { subProcessOfPuppeteer = childProcess.spawn(
env: subProcessEnv, process.argv[0],
stdio: ['inherit', 'inherit', 'inherit', 'pipe', 'ipc'] [...process.argv.slice(1), '--mode-to-daemon=geekAutoStartWithBossMain'],
}) {
env: subProcessEnv,
stdio: ['inherit', 'inherit', 'inherit', 'pipe', 'ipc']
}
)
// console.log(subProcessOfPuppeteer) // console.log(subProcessOfPuppeteer)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
subProcessOfPuppeteer!.stdio[3]!.pipe(JSONStream.parse()).on('data', async (raw) => { subProcessOfPuppeteer!.stdio[3]!.pipe(JSONStream.parse()).on('data', async (raw) => {