move runRecordId to daemon

This commit is contained in:
geekgeekrun
2024-10-24 09:36:24 +08:00
parent 7a3bddbd0c
commit ac83c3c94c
5 changed files with 31 additions and 27 deletions

View File

@@ -13,6 +13,7 @@
"dependencies": {
"dayjs": "^1.11.10",
"json5": "^2.2.3",
"minimist": "^1.2.8",
"puppeteer": "20.1.0",
"puppeteer-extra": "3.3.6",
"puppeteer-extra-plugin-stealth": "2.11.2",

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) => {

View File

@@ -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()

View File

@@ -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(

4
pnpm-lock.yaml generated
View File

@@ -14,6 +14,9 @@ importers:
json5:
specifier: ^2.2.3
version: 2.2.3
minimist:
specifier: ^1.2.8
version: 1.2.8
puppeteer:
specifier: 20.1.0
version: 20.1.0
@@ -4437,7 +4440,6 @@ packages:
/minimist@1.2.8:
resolution: {integrity: sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==}
dev: true
/minipass-collect@1.0.2:
resolution: {integrity: sha512-6T6lH0H8OG9kITm/Jm6tdooIbogG9e0tLgpY6mphXSm/A9u8Nq1ryBG+Qspiub9LjWlBPsPS3tWQ/Botq4FdxA==}