mirror of
https://github.com/geekgeekrun/geekgeekrun.git
synced 2026-09-05 23:47:54 +08:00
add socketToWorkerIdSetMap to fix when user process exit, worker is still running; add isReset for ensureIpcPipeName to fix daemon may not be connected
This commit is contained in:
@@ -1,10 +1,37 @@
|
||||
import { app } from 'electron'
|
||||
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../common/enums/auto-start-chat'
|
||||
import { sendToDaemon } from '../flow/OPEN_SETTING_WINDOW/connect-to-daemon'
|
||||
import { daemonEE, sendToDaemon } from '../flow/OPEN_SETTING_WINDOW/connect-to-daemon'
|
||||
import { saveAndGetCurrentRunRecord } from '../flow/OPEN_SETTING_WINDOW/utils/db'
|
||||
import minimist from 'minimist'
|
||||
|
||||
export async function runCommon({ mode }) {
|
||||
app.dock?.hide()
|
||||
await sendToDaemon(
|
||||
{
|
||||
type: 'user-process-register'
|
||||
},
|
||||
{
|
||||
needCallback: true
|
||||
}
|
||||
)
|
||||
const taskList = (
|
||||
await sendToDaemon(
|
||||
{
|
||||
type: 'get-status'
|
||||
},
|
||||
{
|
||||
needCallback: true
|
||||
}
|
||||
)
|
||||
)?.workers
|
||||
const runningTask = taskList?.find((it) => it.workerId === mode)
|
||||
if (runningTask) {
|
||||
const commandlineArgs = minimist(runningTask.args ?? [])
|
||||
const runRecordId = Number(commandlineArgs['run-record-id'])
|
||||
console.log('任务已在运行中')
|
||||
return {
|
||||
runRecordId,
|
||||
isAlreadyRunning: true
|
||||
}
|
||||
}
|
||||
const currentRunRecord = (await saveAndGetCurrentRunRecord())?.data
|
||||
const subProcessEnv = {
|
||||
...process.env,
|
||||
@@ -30,13 +57,16 @@ export async function runCommon({ mode }) {
|
||||
needCallback: true
|
||||
}
|
||||
)
|
||||
;['SIGINT', 'SIGTERM'].forEach((evName) => {
|
||||
process.on(evName, () => {
|
||||
sendToDaemon({
|
||||
type: 'stop-worker',
|
||||
workerId: mode
|
||||
})
|
||||
})
|
||||
daemonEE.on('message', (message) => {
|
||||
if (message.type === 'worker-exited') {
|
||||
if (
|
||||
message.workerId === mode &&
|
||||
!message.restarting &&
|
||||
globalThis.GEEKGEEKRUN_PROCESS_ROLE !== 'ui'
|
||||
) {
|
||||
process.exit(0)
|
||||
}
|
||||
}
|
||||
})
|
||||
return {
|
||||
runRecordId: currentRunRecord?.id
|
||||
|
||||
@@ -5,9 +5,7 @@ import './app-menu'
|
||||
import initIpc from './ipc'
|
||||
import gtag from '../../utils/gtag'
|
||||
import initPublicIpc from '../../utils/initPublicIpc'
|
||||
import { launchDaemon } from './launch-daemon'
|
||||
import { connectToDaemon, sendToDaemon, closeDaemonClient } from './connect-to-daemon'
|
||||
import { sleep } from "@geekgeekrun/utils/sleep.mjs"
|
||||
import { sendToDaemon, closeDaemonClient } from './connect-to-daemon'
|
||||
|
||||
export function openSettingWindow() {
|
||||
// TODO: singleton lock; how can we check if there is another process should run as singleton with arguments?
|
||||
@@ -81,7 +79,7 @@ export function openSettingWindow() {
|
||||
)
|
||||
await sendToDaemon(
|
||||
{
|
||||
type: 'gui-register'
|
||||
type: 'user-process-register'
|
||||
},
|
||||
{
|
||||
needCallback: true
|
||||
|
||||
@@ -8,7 +8,10 @@ import { randomUUID } from 'node:crypto'
|
||||
import { connectToDaemon } from './connect-to-daemon'
|
||||
|
||||
const isUiDev = process.env.NODE_ENV === 'development'
|
||||
export async function ensureIpcPipeName() {
|
||||
export async function ensureIpcPipeName({ isReset } = {}) {
|
||||
if (isReset) {
|
||||
await writeStorageFile('ipc-pipe-name', '', { isJson: false })
|
||||
}
|
||||
let ipcPipeName = readStorageFile('ipc-pipe-name', { isJson: false })
|
||||
if (!ipcPipeName) {
|
||||
ipcPipeName = `geekgeekrun-d_${randomUUID()}`
|
||||
@@ -20,10 +23,11 @@ export async function ensureIpcPipeName() {
|
||||
}
|
||||
|
||||
export async function launchDaemon() {
|
||||
let daemonProcess
|
||||
async function startDaemon() {
|
||||
console.log('启动守护进程...')
|
||||
// 添加参数使守护进程在后台运行,不显示 UI
|
||||
const daemonProcess = spawn(
|
||||
daemonProcess = spawn(
|
||||
process.argv[0],
|
||||
isUiDev ? [process.argv[1], `--mode=launchDaemon`] : [`--mode=launchDaemon`],
|
||||
{
|
||||
@@ -59,11 +63,29 @@ export async function launchDaemon() {
|
||||
})
|
||||
})
|
||||
}
|
||||
await ensureIpcPipeName()
|
||||
try {
|
||||
await connectToDaemon()
|
||||
} catch (err) {
|
||||
let isDaemonLaunched = false
|
||||
console.log('cannot connect to daemon, try to launch it', err)
|
||||
// 启动守护进程
|
||||
await startDaemon()
|
||||
await connectToDaemon()
|
||||
try {
|
||||
await startDaemon()
|
||||
isDaemonLaunched = true
|
||||
} catch (err) {
|
||||
console.log('cannot launch to daemon, try to change port', err)
|
||||
daemonProcess?.kill('SIGKILL')
|
||||
await ensureIpcPipeName({ isReset: true })
|
||||
try {
|
||||
await startDaemon()
|
||||
isDaemonLaunched = true
|
||||
} catch (err) {
|
||||
console.log('cannot launch to daemon, try to change port failed', err)
|
||||
}
|
||||
}
|
||||
if (isDaemonLaunched) {
|
||||
await connectToDaemon()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import minimist from 'minimist'
|
||||
import { runCommon } from './features/run-common';
|
||||
import { ensureIpcPipeName, launchDaemon } from './flow/OPEN_SETTING_WINDOW/launch-daemon';
|
||||
import { launchDaemon } from './flow/OPEN_SETTING_WINDOW/launch-daemon';
|
||||
import { app } from 'electron';
|
||||
|
||||
// 捕获未处理的 EPIPE 错误
|
||||
process.on('uncaughtException', (err) => {
|
||||
@@ -58,19 +59,25 @@ const runMode = commandlineArgs['mode'];
|
||||
|
||||
// #region user entry
|
||||
case 'geekAutoStartWithBoss': {
|
||||
await ensureIpcPipeName()
|
||||
app.dock?.hide()
|
||||
await launchDaemon()
|
||||
await runCommon({ mode: 'geekAutoStartWithBossMain' })
|
||||
const { isAlreadyRunning } = await runCommon({ mode: 'geekAutoStartWithBossMain' })
|
||||
if (isAlreadyRunning) {
|
||||
process.exit(0)
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'readNoReplyAutoReminder': {
|
||||
await ensureIpcPipeName()
|
||||
app.dock?.hide()
|
||||
await launchDaemon()
|
||||
await runCommon({ mode: 'readNoReplyAutoReminderMain' })
|
||||
const { isAlreadyRunning } = await runCommon({ mode: 'readNoReplyAutoReminderMain' })
|
||||
if (isAlreadyRunning) {
|
||||
process.exit(0)
|
||||
}
|
||||
break
|
||||
}
|
||||
default: {
|
||||
await ensureIpcPipeName()
|
||||
globalThis.GEEKGEEKRUN_PROCESS_ROLE = 'ui'
|
||||
await launchDaemon()
|
||||
const { openSettingWindow } = await import('./flow/OPEN_SETTING_WINDOW/index')
|
||||
openSettingWindow()
|
||||
|
||||
Reference in New Issue
Block a user