move runRecordId to daemon

This commit is contained in:
geekgeekrun
2024-11-23 13:29:49 +08:00
parent 7a3bddbd0c
commit ac83c3c94c
5 changed files with 31 additions and 27 deletions
+2 -14
View File
@@ -23,6 +23,7 @@ import sqlite3 from 'sqlite3';
import * as cliHighlight from 'cli-highlight';
import { saveChatStartupRecord, saveJobInfoFromRecommendPage, saveMarkAsNotSuitRecord } from "./handlers";
import { UpdateChatStartupLogTable1729182577167 } from "./migrations/1729182577167-UpdateChatStartupLogTable";
import minimist from 'minimist'
Boolean(cliHighlight);
@@ -67,25 +68,12 @@ export default class SqlitePlugin {
constructor(dbFilePath) {
this.initPromise = initDb(dbFilePath);
this.runRecordId = minimist(process.argv.slice(2))['run-record-id'] ?? 0
}
userInfo = null
apply(hooks) {
hooks.daemonInitialized.tapPromise(
"SqlitePlugin",
async () => {
const ds = await this.initPromise;
const autoStartChatRunRecord = new AutoStartChatRunRecord();
autoStartChatRunRecord.date = new Date();
const autoStartChatRunRecordRepository = ds.getRepository(AutoStartChatRunRecord)
const result = await autoStartChatRunRecordRepository.save(autoStartChatRunRecord);
this.runRecordId = result.id;
}
);
hooks.userInfoResponse.tapPromise(
"SqlitePlugin",
async (userInfoResponse) => {
@@ -7,6 +7,9 @@ import { pipeWriteRegardlessError } from '../utils/pipe'
import * as JSONStream from 'JSONStream'
import { initPowerSaveBlocker } from './power-saver-blocker'
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'
const rerunInterval = (() => {
let v = Number(process.env.MAIN_BOSSGEEKGO_RERUN_INTERVAL)
@@ -16,14 +19,18 @@ const rerunInterval = (() => {
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'
function runWithDaemon({ runRecordId }) {
const subProcessOfCore = childProcess.spawn(
process.argv[0],
[...process.argv.slice(1), `--run-record-id=${runRecordId}`],
{
stdio: ['inherit', 'inherit', 'inherit', 'pipe', 'ipc'],
env: {
...process.env,
MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBossMain'
}
}
})
)
subProcessOfCore!.stdio[3]!.pipe(JSONStream.parse()).on('data', async (raw) => {
const data = raw
@@ -59,7 +66,7 @@ function runWithDaemon() {
`[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()
runWithDaemon({ runRecordId })
})
}
@@ -96,11 +103,19 @@ export function runAutoChatWithDaemon() {
const pipeForRead: fs.ReadStream = fs.createReadStream(null, { fd: 3 })
const pipeForReadWithJsonParser = pipeForRead.pipe(JSONStream.parse())
pipeForReadWithJsonParser?.on('data', function waitForCanRun(data) {
pipeForReadWithJsonParser?.on('data', async function waitForCanRun(data) {
if (data.type === 'GEEK_AUTO_START_CHAT_CAN_BE_RUN') {
const ds = await initDb(getPublicDbFilePath())
const autoStartChatRunRecord = new AutoStartChatRunRecord()
autoStartChatRunRecord.date = new Date()
const autoStartChatRunRecordRepository = ds.getRepository(AutoStartChatRunRecord)
const result = await autoStartChatRunRecordRepository.save(autoStartChatRunRecord)
pipeForReadWithJsonParser.off('data', waitForCanRun)
clearSuicideTimer()
runWithDaemon()
runWithDaemon({ runRecordId: result.id })
// if don't call close, when kill child process, child process will ANR.
pipeForRead.close()
@@ -80,7 +80,6 @@ const runAutoChat = async () => {
}
const hooks = {
daemonInitialized: new AsyncSeriesHook(),
puppeteerLaunched: new SyncHook(),
pageLoaded: new SyncHook(),
cookieWillSet: new SyncHook(['cookies']),
@@ -94,7 +93,6 @@ const runAutoChat = async () => {
errorEncounter: new SyncHook(['errorInfo'])
}
initPlugins(hooks)
await hooks.daemonInitialized.promise()
gtag('run_auto_chat_with_boss_main_ready')
pipeWriteRegardlessError(