diff --git a/packages/geek-auto-start-chat-with-boss/index.mjs b/packages/geek-auto-start-chat-with-boss/index.mjs index b1bac53..c01395d 100644 --- a/packages/geek-auto-start-chat-with-boss/index.mjs +++ b/packages/geek-auto-start-chat-with-boss/index.mjs @@ -100,9 +100,6 @@ export async function initPuppeteer () { } } -const bossCookies = readStorageFile('boss-cookies.json') -const bossLocalStorage = readStorageFile('boss-local-storage.json') - const targetCompanyList = readConfigFile('target-company-list.json').filter(it => !!it.trim()); const combineRecommendJobFilterType = readConfigFile('boss.json').combineRecommendJobFilterType ?? CombineRecommendJobFilterType.ANY_COMBINE @@ -1476,7 +1473,9 @@ export async function mainLoop (hooks) { page = (await browser.pages())[0] hooks.pageGotten?.call(page) //set cookies - hooks.cookieWillSet?.call(bossCookies) + const bossCookies = readStorageFile('boss-cookies.json') + const bossLocalStorage = readStorageFile('boss-local-storage.json') + await hooks.cookieWillSet?.promise(bossCookies) for(let i = 0; i < bossCookies.length; i++){ await page.setCookie(bossCookies[i]); } diff --git a/packages/ui/src/main/features/boss-user-info-response-plugin.ts b/packages/ui/src/main/features/boss-user-info-response-plugin.ts deleted file mode 100644 index 1498571..0000000 --- a/packages/ui/src/main/features/boss-user-info-response-plugin.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { sendToDaemon } from "../flow/OPEN_SETTING_WINDOW/connect-to-daemon" -import minimist from 'minimist' - -const runRecordId = minimist(process.argv.slice(2))['run-record-id'] ?? null -export function pushUserInfoValidStatus (userInfoResponse) { - sendToDaemon({ - type: 'worker-to-gui-message', - data: { - type: 'prerequisite-step-by-step-checkstep-by-step-check', - step: { - id: 'login-status-check', - status: userInfoResponse.code === 0 ? 'fulfilled' : 'rejected' - }, - runRecordId - } - }) -} - -export class UserResponseInfoPlugin { - apply(hooks) { - hooks.userInfoResponse.tapPromise( - "UserResponseInfoPlugin", - (userInfoResponse) => { - pushUserInfoValidStatus(userInfoResponse) - return Promise.resolve() - } - ) - } -} \ No newline at end of file diff --git a/packages/ui/src/main/features/cookie-invalid-handle-plugin.ts b/packages/ui/src/main/features/cookie-invalid-handle-plugin.ts new file mode 100644 index 0000000..529292b --- /dev/null +++ b/packages/ui/src/main/features/cookie-invalid-handle-plugin.ts @@ -0,0 +1,93 @@ +import { sendToDaemon } from '../flow/OPEN_SETTING_WINDOW/connect-to-daemon' +import minimist from 'minimist' +import { loginWithCookieAssistant } from './login-with-cookie-assistant' +import { checkCookieListFormat } from '../../common/utils/cookie' +import { sleep } from '@geekgeekrun/utils/sleep.mjs' +import { readStorageFile } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' + +const runRecordId = minimist(process.argv.slice(2))['run-record-id'] ?? null +export class CookieInvalidHandlePlugin { + apply(hooks) { + hooks.cookieWillSet.tapPromise('CookieInvalidHandlePlugin', async (cookies) => { + let isValid = checkCookieListFormat(cookies) + while (!isValid) { + try { + // popup login dialog, then update login status + await loginWithCookieAssistant() + await sleep(2000) + const newCookies = readStorageFile('boss-cookies.json') + isValid = checkCookieListFormat(newCookies) + if (isValid) { + cookies.length = 0 + for (const cookie of newCookies) { + cookies.push(cookie) + } + } + } catch (e) { + if (e?.message === 'USER_CANCELLED_LOGIN') { + sendToDaemon({ + type: 'worker-to-gui-message', + data: { + type: 'prerequisite-step-by-step-checkstep-by-step-check', + step: { + id: 'basic-cookie-check', + status: 'rejected' + }, + runRecordId + } + }) + throw new Error('LOGIN_STATUS_INVALID') + } + } + } + sendToDaemon({ + type: 'worker-to-gui-message', + data: { + type: 'prerequisite-step-by-step-checkstep-by-step-check', + step: { + id: 'basic-cookie-check', + status: 'fulfilled' + }, + runRecordId + } + }) + }) + hooks.userInfoResponse.tapPromise('CookieInvalidHandlePlugin', async (userInfoResponse) => { + if (userInfoResponse.code === 0) { + sendToDaemon({ + type: 'worker-to-gui-message', + data: { + type: 'prerequisite-step-by-step-checkstep-by-step-check', + step: { + id: 'login-status-check', + status: 'fulfilled' + }, + runRecordId + } + }) + return + } + try { + // popup login dialog, then update login status + await loginWithCookieAssistant() + } catch (e) { + if (e?.message === 'USER_CANCELLED_LOGIN') { + sendToDaemon({ + type: 'worker-to-gui-message', + data: { + type: 'prerequisite-step-by-step-checkstep-by-step-check', + step: { + id: 'login-status-check', + status: 'rejected' + }, + runRecordId + } + }) + throw new Error('LOGIN_STATUS_INVALID') + } + } + // throw new Error('THROW_FOR_RETRY') + return Promise.reject(new Error('THROW_FOR_RETRY')) + }) + } +} diff --git a/packages/ui/src/main/features/login-with-cookie-assistant.ts b/packages/ui/src/main/features/login-with-cookie-assistant.ts index 03cb8b5..9cdefdc 100644 --- a/packages/ui/src/main/features/login-with-cookie-assistant.ts +++ b/packages/ui/src/main/features/login-with-cookie-assistant.ts @@ -14,7 +14,7 @@ export async function loginWithCookieAssistant({ windowOption } = {}) { if (processDone) { resolve(true) } else { - reject(new Error('User cancelled login')) + reject(new Error('USER_CANCELLED_LOGIN')) } }) }) diff --git a/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS_MAIN/index.ts b/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS_MAIN/index.ts index c821800..e6d4924 100644 --- a/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS_MAIN/index.ts +++ b/packages/ui/src/main/flow/GEEK_AUTO_START_CHAT_WITH_BOSS_MAIN/index.ts @@ -17,9 +17,15 @@ import GtagPlugin from '../../utils/gtag/GtagPlugin' import { connectToDaemon, sendToDaemon } from '../OPEN_SETTING_WINDOW/connect-to-daemon' // import { PeriodPushCurrentPageScreenshotPlugin } from '../../utils/screenshot' import { checkShouldExit } from '../../utils/worker' -import { UserResponseInfoPlugin } from '../../features/boss-user-info-response-plugin' +import { CookieInvalidHandlePlugin } from '../../features/cookie-invalid-handle-plugin' +import initPublicIpc from '../../utils/initPublicIpc' const { default: SqlitePlugin } = SqlitePluginModule +process.on('SIGTERM', () => { + console.log('收到SIGTERM信号,正在退出') + process.exit(0) +}) + const rerunInterval = (() => { let v = Number(process.env.MAIN_BOSSGEEKGO_RERUN_INTERVAL) if (isNaN(v)) { @@ -36,7 +42,7 @@ const initPlugins = (hooks) => { new SqlitePlugin(getPublicDbFilePath()).apply(hooks) new GtagPlugin().apply(hooks) // new PeriodPushCurrentPageScreenshotPlugin().apply(hooks) - new UserResponseInfoPlugin().apply(hooks) + new CookieInvalidHandlePlugin().apply(hooks) } const runRecordId = minimist(process.argv.slice(2))['run-record-id'] ?? null @@ -83,7 +89,7 @@ const runAutoChat = async () => { puppeteerLaunched: new SyncHook(['browser']), pageGotten: new SyncHook(['page']), pageLoaded: new SyncHook(), - cookieWillSet: new SyncHook(['cookies']), + cookieWillSet: new AsyncSeriesHook(['cookies']), userInfoResponse: new AsyncSeriesHook(['userInfo']), mainFlowWillLaunch: new AsyncSeriesHook(['args']), jobDetailIsGetFromRecommendList: new AsyncSeriesHook(['userInfo']), @@ -147,6 +153,11 @@ const runAutoChat = async () => { } export const waitForProcessHandShakeAndRunAutoChat = async () => { + await app.whenReady() + app.on('window-all-closed', (e) => { + e.preventDefault() + }) + initPublicIpc() await connectToDaemon() await sendToDaemon( { diff --git a/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/connect-to-daemon.ts b/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/connect-to-daemon.ts index 30e9102..95f93f6 100644 --- a/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/connect-to-daemon.ts +++ b/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/connect-to-daemon.ts @@ -2,11 +2,9 @@ import { randomUUID } from "node:crypto"; import { EventEmitter } from "node:events"; import { tmpdir } from "node:os"; import path from "node:path"; -import fs from "node:fs"; const net = require('net'); const split2 = require('split2'); -const { app } = require('electron'); let daemonClient = null; export const daemonEE = new EventEmitter() @@ -138,14 +136,6 @@ export function sendToDaemon(message, { // sendToDaemon({ type: 'start-worker', workerId, command, args, env }); // }); -app.on('window-all-closed', () => { - if (daemonClient) { - daemonClient.destroy(); - } -}); - -app.on('before-quit', () => { - if (daemonClient) { - daemonClient.destroy(); - } -}); +export function closeDaemonClient() { + daemonClient?.destroy() +} diff --git a/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/index.ts b/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/index.ts index ef9c70f..7a9c2f7 100644 --- a/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/index.ts +++ b/packages/ui/src/main/flow/OPEN_SETTING_WINDOW/index.ts @@ -6,7 +6,7 @@ import initIpc from './ipc' import gtag from '../../utils/gtag' import initPublicIpc from '../../utils/initPublicIpc' import { launchDaemon } from './launch-daemon' -import { connectToDaemon, sendToDaemon } from './connect-to-daemon' +import { connectToDaemon, sendToDaemon, closeDaemonClient } from './connect-to-daemon' import { sleep } from "@geekgeekrun/utils/sleep.mjs" export function openSettingWindow() { @@ -91,4 +91,6 @@ export function openSettingWindow() { } ) }) + app.on('window-all-closed', closeDaemonClient) + app.on('before-quit', closeDaemonClient) }