Adding questions using worker pools

This commit is contained in:
mrfry 2020-12-19 10:32:15 +01:00
parent d8695682f7
commit 4681ea3791
5 changed files with 50 additions and 17 deletions

View file

@ -21,7 +21,8 @@ const recDataFile = './stats/recdata'
const dataLockFile = './data/lockData'
import logger from '../utils/logger'
import { searchData, createQuestion } from '../utils/classes'
import { createQuestion } from '../utils/classes'
import { doALongTask } from './workerPool'
import idStats from '../utils/ids'
import utils from '../utils/utils'
import { SearchResult, addQuestion, getSubjNameWithoutYear } from './classes'
@ -166,7 +167,14 @@ function processIncomingRequestUsingDb(
logger.DebugLog(currentQuestion, 'actions', 3)
recievedQuestions.push(currentQuestion)
questionSearchPromises.push(
searchData(qdb, currentQuestion, recievedData.subj)
doALongTask({
type: 'work',
data: {
qdb: qdb.data,
question: currentQuestion,
subjName: recievedData.subj,
},
})
)
})

View file

@ -585,7 +585,6 @@ const workerTs = (file: string, wkOpts: any) => {
if (!isMainThread) {
const workerIndex = workerData.workerIndex
// TODO: check if thread independent
logger.Log(
`[THREAD #${workerIndex}]: Worker ${workerIndex} reporting for duty`
@ -595,7 +594,11 @@ if (!isMainThread) {
if (msg.type === 'work') {
const { qdb, subjName, question, questionData } = msg.data
const index = msg.index
console.log(`[THREAD #${workerIndex}]: staring work on ${index}`)
console.log(
`[THREAD #${workerIndex}]: staring work${
!isNaN(index) ? ` on job index #${index}` : ''
}`
)
let searchResult = null
try {
@ -607,12 +610,18 @@ if (!isMainThread) {
// ONDONE:
parentPort.postMessage({
msg: `From thread #${workerIndex}: job ${index} done`,
msg: `From thread #${workerIndex}: job ${
!isNaN(index) ? `#${index}` : ''
}done`,
workerIndex: workerIndex,
result: searchResult,
})
console.log(`[THREAD #${workerIndex}]: Work ${index} done!`)
console.log(
`[THREAD #${workerIndex}]: Work ${
!isNaN(index) ? `#${index}` : ''
}done!`
)
} else if (msg.type === 'update') {
if (msg.data.workerIndex !== workerIndex) {
// TODO

View file

@ -18,7 +18,7 @@ let workers: any = null
// console.log('[MSGFROMCLIENT]', res)
// })
export function doALongTask(i: Number, obj: any): Promise<any> {
export function doALongTask(obj: any): Promise<any> {
return new Promise((resolve) => {
pool
.acquire()
@ -47,11 +47,12 @@ export function initWorkerPool(): void {
workers = []
const factory = {
create: function() {
const worker = getAWorker(workers.length)
const currInd = workers.length
const worker = getAWorker(currInd)
workers.push(worker)
return {
worker: worker,
index: workers.length,
index: currInd,
}
},
destroy: function(client) {