Threads store their own qdb. It gets updated when new questions are added to reduce message sizes between threads

This commit is contained in:
mrfry 2020-12-19 11:31:12 +01:00
parent 906ab8ee62
commit 68dcbff846
5 changed files with 56 additions and 141 deletions

View file

@ -1,11 +1,6 @@
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads'
import { isMainThread, parentPort, workerData } from 'worker_threads'
import logger from './logger'
import {
Question,
QuestionDb,
QuestionData,
Subject,
} from '../types/basicTypes'
import { Question, QuestionData, Subject } from '../types/basicTypes'
interface SearchResultQuestion extends Question {
match: number
@ -16,8 +11,6 @@ export interface SearchResult {
dbName: string
}
const searchDataWorkerFile = './src/utils/classes.ts'
const assert = (val) => {
if (!val) {
throw new Error('Assertion failed')
@ -216,7 +209,7 @@ function createQuestion(
return {
Q: simplifyQuestion(question),
A: answer ? simplifyAnswer(answer) : null,
data,
data: data,
}
}
@ -414,83 +407,13 @@ function prepareQuestion(
preparedQuestion = question
} else {
// FIXME data was checkedif its null, it should be never null. check if its really never null
const parsedData = typeof data === 'object' ? data : JSON.parse(data)
const parsedData = typeof data === 'object' ? data : JSON.parse(data) // TODO: put in try?
preparedQuestion = createQuestion(question, null, parsedData)
}
return simplifyQuestion(preparedQuestion)
}
function searchData(
qdb: QuestionDb,
question: Question | string,
subjName: string,
questionData?: QuestionData | string
): Promise<SearchResult> {
// FIXME subjName was checkedif its null, it should be never null. check if its really never null
return new Promise((resolve, reject) => {
assert(question)
logger.DebugLog('Searching for question', 'qdb search', 1)
const preparedQuestion = prepareQuestion(question, questionData)
logger.DebugLog('Question:', 'qdb search', 2)
logger.DebugLog(preparedQuestion, 'qdb search', 2)
logger.DebugLog(`Subject name: ${subjName}`, 'qdb search', 2)
const worker = workerTs(searchDataWorkerFile, {
workerData: {
data: qdb.data,
subjName,
question: preparedQuestion,
questionData,
},
})
worker.on('error', (err) => {
logger.Log('Search Data Worker error!', logger.GetColor('redbg'))
console.error(err)
reject(err)
})
worker.on('exit', (code) => {
logger.DebugLog('Search Data exit, code: ' + code, 'actions', 1)
if (code !== 0) {
logger.Log(
'Search Data Worker error! Exit code is not 0',
logger.GetColor('redbg')
)
reject(new Error('Search Data Worker error! Exit code is not 0'))
}
})
worker.on('message', (result) => {
// TODO: remove (?)
if (typeof result === 'string') {
try {
console.log(JSON.parse(result))
} catch (err) {
console.log(result)
}
}
logger.DebugLog(`Worker message arrived`, 'worker', 2)
logger.DebugLog(result, 'worker', 3)
logger.DebugLog(`Question result length: ${result.length}`, 'ask', 1)
logger.DebugLog(result, 'ask', 2)
logger.DebugLog(
`QDB search result length: ${result.length}`,
'qdb search',
1
)
resolve({
result: result,
dbName: qdb.name,
})
})
})
}
function dataToString(data: Array<Subject>): string {
const result = []
data.forEach((subj) => {
@ -509,10 +432,7 @@ function doSearch(
): any {
let result = []
const questionToSearch =
typeof question === 'string'
? createQuestion(question, null, questionData || { type: 'simple' })
: question
const questionToSearch = prepareQuestion(question, questionData)
assert(questionToSearch.data)
@ -565,26 +485,9 @@ function doSearch(
return result
}
const workerTs = (file: string, wkOpts: any) => {
wkOpts.eval = true
if (!wkOpts.workerData) {
wkOpts.workerData = {}
}
wkOpts.workerData.__filename = file
return new Worker(
`
const wk = require('worker_threads');
require('ts-node').register();
let file = wk.workerData.__filename;
delete wk.workerData.__filename;
require(file);
`,
wkOpts
)
}
if (!isMainThread) {
const workerIndex = workerData.workerIndex
const { workerIndex } = workerData
let qdbs: Array<any> = workerData.initData
logger.Log(
`[THREAD #${workerIndex}]: Worker ${workerIndex} reporting for duty`
@ -592,7 +495,7 @@ if (!isMainThread) {
parentPort.on('message', (msg) => {
if (msg.type === 'work') {
const { qdb, subjName, question, questionData } = msg.data
const { searchIn, subjName, question, questionData } = msg.data
const index = msg.index
console.log(
`[THREAD #${workerIndex}]: staring work${
@ -601,8 +504,12 @@ if (!isMainThread) {
)
let searchResult = null
const currQdb = qdbs.find((qdb) => {
return searchIn[0] === qdb.index
})
try {
searchResult = doSearch(qdb, subjName, question, questionData)
searchResult = doSearch(currQdb.data, subjName, question, questionData)
} catch (err) {
logger.Log('Error in worker thread!', logger.GetColor('redbg'))
console.error(err)
@ -623,12 +530,8 @@ if (!isMainThread) {
}done!`
)
} else if (msg.type === 'update') {
if (msg.data.workerIndex !== workerIndex) {
// TODO
// qdbs = msg.qdb
console.log(`[THREAD #${workerIndex}]: update`, msg.data)
console.log(`[THREAD #${workerIndex}]: From ... to ${msg.data.result}`)
}
qdbs = msg.qdbs
console.log(`[THREAD #${workerIndex}]: update`)
}
})
} else {
@ -642,6 +545,5 @@ export {
getSubjNameWithoutYear,
createQuestion,
addQuestion,
searchData,
dataToString,
}