mirror of
https://github.com/geekgeekrun/geekgeekrun.git
synced 2026-09-09 01:17:06 +08:00
test use db in worker_thread
This commit is contained in:
@@ -17,7 +17,7 @@ import sqlite3 from 'sqlite3';
|
|||||||
import * as cliHighlight from 'cli-highlight';
|
import * as cliHighlight from 'cli-highlight';
|
||||||
Boolean(cliHighlight);
|
Boolean(cliHighlight);
|
||||||
|
|
||||||
function initDb(dbFilePath) {
|
export function initDb(dbFilePath) {
|
||||||
const { DataSource } = requireTypeorm()
|
const { DataSource } = requireTypeorm()
|
||||||
const appDataSource = new DataSource({
|
const appDataSource = new DataSource({
|
||||||
type: "sqlite",
|
type: "sqlite",
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import { getAnyAvailablePuppeteerExecutable } from '../../../flow/CHECK_AND_DOWN
|
|||||||
import { sleep } from '@geekgeekrun/utils/sleep.mjs'
|
import { sleep } from '@geekgeekrun/utils/sleep.mjs'
|
||||||
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../../common/enums/auto-start-chat'
|
import { AUTO_CHAT_ERROR_EXIT_CODE } from '../../../../common/enums/auto-start-chat'
|
||||||
import { mainWindow } from '../../../window/mainWindow'
|
import { mainWindow } from '../../../window/mainWindow'
|
||||||
|
import { getAutoStartChatRecord, initDbWorker } from '../utils/db/index'
|
||||||
|
|
||||||
export default function initIpc () {
|
export default function initIpc () {
|
||||||
ipcMain.on('open-external-link', (_, link) => {
|
ipcMain.on('open-external-link', (_, link) => {
|
||||||
@@ -257,4 +258,11 @@ export default function initIpc () {
|
|||||||
const cookies = readStorageFile('boss-cookies.json')
|
const cookies = readStorageFile('boss-cookies.json')
|
||||||
return checkCookieListFormat(cookies)
|
return checkCookieListFormat(cookies)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ipcMain.handle('connect-db', async () => {
|
||||||
|
const worker = await initDbWorker()
|
||||||
|
console.log(worker)
|
||||||
|
const a = await getAutoStartChatRecord()
|
||||||
|
console.log(a)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import createDbWorker from './worker/index?nodeWorker&url'
|
||||||
|
import { type Worker } from 'node:worker_threads'
|
||||||
|
import { randomUUID } from 'node:crypto'
|
||||||
|
|
||||||
|
let worker: Worker | null = null
|
||||||
|
let workerExitCode: number | null = null
|
||||||
|
export const initDbWorker = () => {
|
||||||
|
if (!worker || typeof workerExitCode === 'number') {
|
||||||
|
worker = createDbWorker()
|
||||||
|
workerExitCode = null
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
worker.once('exit', (exitCode) => {
|
||||||
|
workerExitCode = exitCode
|
||||||
|
worker = null
|
||||||
|
})
|
||||||
|
worker.on('message', function handler(data) {
|
||||||
|
if (data.type === 'DB_INIT_SUCCESS') {
|
||||||
|
resolve(worker)
|
||||||
|
// attach more event
|
||||||
|
worker?.off('message', handler)
|
||||||
|
} else if (data.type === 'DB_INIT_FAIL') {
|
||||||
|
reject(undefined)
|
||||||
|
worker?.terminate()
|
||||||
|
worker?.off('message', handler)
|
||||||
|
worker = null
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return worker
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const createWorkerPromise = async (data) => {
|
||||||
|
await initDbWorker()
|
||||||
|
const uuid = randomUUID()
|
||||||
|
worker.postMessage({
|
||||||
|
_uuid: uuid,
|
||||||
|
...data
|
||||||
|
})
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
worker.on('message', function handler(data) {
|
||||||
|
const { _uuid, ...payload } = data ?? {}
|
||||||
|
if (_uuid === uuid) {
|
||||||
|
resolve(payload)
|
||||||
|
worker!.off('message', handler)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getAutoStartChatRecord = async () => {
|
||||||
|
const res = await createWorkerPromise({
|
||||||
|
type: 'getAutoStartChatRecord'
|
||||||
|
})
|
||||||
|
return res
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { parentPort } from 'node:worker_threads'
|
||||||
|
import { initDb } from '@geekgeekrun/sqlite-plugin'
|
||||||
|
import typeorm from 'typeorm'
|
||||||
|
import { type DataSource } from 'typeorm'
|
||||||
|
import { getPublicDbFilePath } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs'
|
||||||
|
|
||||||
|
const dbInitPromise = initDb(getPublicDbFilePath())
|
||||||
|
let dataSource = null
|
||||||
|
|
||||||
|
dbInitPromise.then(
|
||||||
|
(_dataSource) => {
|
||||||
|
dataSource = _dataSource
|
||||||
|
attachMessageHandler()
|
||||||
|
parentPort?.postMessage({
|
||||||
|
type: 'DB_INIT_SUCCESS'
|
||||||
|
})
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
parentPort?.postMessage({
|
||||||
|
type: 'DB_INIT_FAIL',
|
||||||
|
error
|
||||||
|
})
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const payloadHandler = {
|
||||||
|
getAutoStartChatRecord(payload, callback) {
|
||||||
|
callback({
|
||||||
|
x: 'zzzz',
|
||||||
|
...payload
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function attachMessageHandler() {
|
||||||
|
if (!dataSource) {
|
||||||
|
await dbInitPromise
|
||||||
|
}
|
||||||
|
parentPort?.on('message', (event) => {
|
||||||
|
const { _uuid, ...restObj } = event
|
||||||
|
const { type } = event
|
||||||
|
|
||||||
|
const callback = (result) => {
|
||||||
|
parentPort?.postMessage({
|
||||||
|
_uuid,
|
||||||
|
data: result
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
payloadHandler[type](restObj, callback)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -52,4 +52,10 @@ onMounted(async () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
electron.ipcRenderer.invoke('connect-db').then(() => {
|
||||||
|
debugger
|
||||||
|
}, () => {
|
||||||
|
debugger
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user