mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Workers can now search in multiple specified db-s
This commit is contained in:
parent
68dcbff846
commit
19b8fdc5ab
5 changed files with 57 additions and 52 deletions
|
@ -22,7 +22,7 @@ const dataLockFile = './data/lockData'
|
|||
|
||||
import logger from '../utils/logger'
|
||||
import { createQuestion } from '../utils/classes'
|
||||
import { doALongTask } from './workerPool'
|
||||
import { doALongTask } from './workerPool'
|
||||
import idStats from '../utils/ids'
|
||||
import utils from '../utils/utils'
|
||||
import { SearchResult, addQuestion, getSubjNameWithoutYear } from './classes'
|
||||
|
@ -45,7 +45,7 @@ export interface RecievedData {
|
|||
scriptVersion: string
|
||||
}
|
||||
|
||||
interface Result {
|
||||
export interface Result {
|
||||
qdbName: string
|
||||
newQuestions: number
|
||||
}
|
||||
|
@ -128,6 +128,7 @@ export function processIncomingRequest(
|
|||
return new Promise((resolve) => resolve([]))
|
||||
}
|
||||
|
||||
// FIXME: this many promises and stuff might be unnecesarry
|
||||
const promises: Array<Promise<Result>> = questionDbs.reduce((acc, qdb) => {
|
||||
if (qdb.shouldSave(recievedData)) {
|
||||
acc.push(processIncomingRequestUsingDb(recievedData, qdb, dryRun, user))
|
||||
|
@ -202,8 +203,6 @@ function processIncomingRequestUsingDb(
|
|||
)
|
||||
logger.DebugLog(currentQuestion, 'actions', 3)
|
||||
addQuestion(qdb.data, sName, currentQuestion)
|
||||
// TODO: check if it really adds it, not only just some clone (questionDbs in api.ts
|
||||
// modifies too)
|
||||
})
|
||||
|
||||
currWrites++
|
||||
|
|
|
@ -495,33 +495,48 @@ if (!isMainThread) {
|
|||
|
||||
parentPort.on('message', (msg) => {
|
||||
if (msg.type === 'work') {
|
||||
const { searchIn, subjName, question, questionData } = msg.data
|
||||
const { subjName, question, questionData } = msg.data
|
||||
const index = msg.index
|
||||
const searchIn = msg.data.searchIn
|
||||
|
||||
console.log(
|
||||
`[THREAD #${workerIndex}]: staring work${
|
||||
!isNaN(index) ? ` on job index #${index}` : ''
|
||||
}`
|
||||
)
|
||||
|
||||
let searchResult = null
|
||||
const currQdb = qdbs.find((qdb) => {
|
||||
return searchIn[0] === qdb.index
|
||||
})
|
||||
let searchResult = []
|
||||
|
||||
try {
|
||||
searchResult = doSearch(currQdb.data, subjName, question, questionData)
|
||||
qdbs.forEach((qdb) => {
|
||||
if (searchIn === 'all' || searchIn.includes(qdb.index)) {
|
||||
const res = doSearch(qdb.data, subjName, question, questionData)
|
||||
searchResult = [...searchResult, ...res]
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
logger.Log('Error in worker thread!', logger.GetColor('redbg'))
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
// sorting
|
||||
const sortedResult = searchResult.sort((q1, q2) => {
|
||||
if (q1.match < q2.match) {
|
||||
return 1
|
||||
} else if (q1.match > q2.match) {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
})
|
||||
|
||||
// ONDONE:
|
||||
parentPort.postMessage({
|
||||
msg: `From thread #${workerIndex}: job ${
|
||||
!isNaN(index) ? `#${index}` : ''
|
||||
}done`,
|
||||
workerIndex: workerIndex,
|
||||
result: searchResult,
|
||||
result: sortedResult,
|
||||
})
|
||||
|
||||
console.log(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue