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
+1
View File
@@ -13,6 +13,7 @@
"dependencies": { "dependencies": {
"dayjs": "^1.11.10", "dayjs": "^1.11.10",
"json5": "^2.2.3", "json5": "^2.2.3",
"minimist": "^1.2.8",
"puppeteer": "20.1.0", "puppeteer": "20.1.0",
"puppeteer-extra": "3.3.6", "puppeteer-extra": "3.3.6",
"puppeteer-extra-plugin-stealth": "2.11.2", "puppeteer-extra-plugin-stealth": "2.11.2",
+2 -14
View File
@@ -23,6 +23,7 @@ import sqlite3 from 'sqlite3';
import * as cliHighlight from 'cli-highlight'; import * as cliHighlight from 'cli-highlight';
import { saveChatStartupRecord, saveJobInfoFromRecommendPage, saveMarkAsNotSuitRecord } from "./handlers"; import { saveChatStartupRecord, saveJobInfoFromRecommendPage, saveMarkAsNotSuitRecord } from "./handlers";
import { UpdateChatStartupLogTable1729182577167 } from "./migrations/1729182577167-UpdateChatStartupLogTable"; import { UpdateChatStartupLogTable1729182577167 } from "./migrations/1729182577167-UpdateChatStartupLogTable";
import minimist from 'minimist'
Boolean(cliHighlight); Boolean(cliHighlight);
@@ -67,25 +68,12 @@ export default class SqlitePlugin {
constructor(dbFilePath) { constructor(dbFilePath) {
this.initPromise = initDb(dbFilePath); this.initPromise = initDb(dbFilePath);
this.runRecordId = minimist(process.argv.slice(2))['run-record-id'] ?? 0
} }
userInfo = null userInfo = null
apply(hooks) { 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( hooks.userInfoResponse.tapPromise(
"SqlitePlugin", "SqlitePlugin",
async (userInfoResponse) => { async (userInfoResponse) => {
@@ -7,6 +7,9 @@ import { pipeWriteRegardlessError } from '../utils/pipe'
import * as JSONStream from 'JSONStream' import * as JSONStream from 'JSONStream'
import { initPowerSaveBlocker } from './power-saver-blocker' import { initPowerSaveBlocker } from './power-saver-blocker'
import gtag from '../../utils/gtag' 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 = (() => { const rerunInterval = (() => {
let v = Number(process.env.MAIN_BOSSGEEKGO_RERUN_INTERVAL) let v = Number(process.env.MAIN_BOSSGEEKGO_RERUN_INTERVAL)
@@ -16,14 +19,18 @@ const rerunInterval = (() => {
return v return v
})() })()
function runWithDaemon() { function runWithDaemon({ runRecordId }) {
const subProcessOfCore = childProcess.spawn(process.argv[0], process.argv.slice(1), { const subProcessOfCore = childProcess.spawn(
process.argv[0],
[...process.argv.slice(1), `--run-record-id=${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: 'geekAutoStartWithBossMain'
} }
}) }
)
subProcessOfCore!.stdio[3]!.pipe(JSONStream.parse()).on('data', async (raw) => { subProcessOfCore!.stdio[3]!.pipe(JSONStream.parse()).on('data', async (raw) => {
const data = 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.` `[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() runWithDaemon({ runRecordId })
}) })
} }
@@ -96,11 +103,19 @@ export function runAutoChatWithDaemon() {
const pipeForRead: fs.ReadStream = fs.createReadStream(null, { fd: 3 }) const pipeForRead: fs.ReadStream = fs.createReadStream(null, { fd: 3 })
const pipeForReadWithJsonParser = pipeForRead.pipe(JSONStream.parse()) 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') { 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) pipeForReadWithJsonParser.off('data', waitForCanRun)
clearSuicideTimer() clearSuicideTimer()
runWithDaemon() runWithDaemon({ runRecordId: result.id })
// if don't call close, when kill child process, child process will ANR. // if don't call close, when kill child process, child process will ANR.
pipeForRead.close() pipeForRead.close()
@@ -80,7 +80,6 @@ const runAutoChat = async () => {
} }
const hooks = { const hooks = {
daemonInitialized: new AsyncSeriesHook(),
puppeteerLaunched: new SyncHook(), puppeteerLaunched: new SyncHook(),
pageLoaded: new SyncHook(), pageLoaded: new SyncHook(),
cookieWillSet: new SyncHook(['cookies']), cookieWillSet: new SyncHook(['cookies']),
@@ -94,7 +93,6 @@ const runAutoChat = async () => {
errorEncounter: new SyncHook(['errorInfo']) errorEncounter: new SyncHook(['errorInfo'])
} }
initPlugins(hooks) initPlugins(hooks)
await hooks.daemonInitialized.promise()
gtag('run_auto_chat_with_boss_main_ready') gtag('run_auto_chat_with_boss_main_ready')
pipeWriteRegardlessError( pipeWriteRegardlessError(
+3 -1
View File
@@ -14,6 +14,9 @@ importers:
json5: json5:
specifier: ^2.2.3 specifier: ^2.2.3
version: 2.2.3 version: 2.2.3
minimist:
specifier: ^1.2.8
version: 1.2.8
puppeteer: puppeteer:
specifier: 20.1.0 specifier: 20.1.0
version: 20.1.0 version: 20.1.0
@@ -4437,7 +4440,6 @@ packages:
/minimist@1.2.8: /minimist@1.2.8:
resolution: {integrity: sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==} resolution: {integrity: sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==}
dev: true
/minipass-collect@1.0.2: /minipass-collect@1.0.2:
resolution: {integrity: sha512-6T6lH0H8OG9kITm/Jm6tdooIbogG9e0tLgpY6mphXSm/A9u8Nq1ryBG+Qspiub9LjWlBPsPS3tWQ/Botq4FdxA==} resolution: {integrity: sha512-6T6lH0H8OG9kITm/Jm6tdooIbogG9e0tLgpY6mphXSm/A9u8Nq1ryBG+Qspiub9LjWlBPsPS3tWQ/Botq4FdxA==}