Problems converting process incoming request to use new searchData worker

This commit is contained in:
mrfry
2020-10-14 10:22:21 +02:00
parent c846d2591e
commit 63b08244dc
4 changed files with 233 additions and 228 deletions
+80 -85
View File
@@ -24,10 +24,9 @@ module.exports = {
const dataFile = './publicDirs/qminingPublic/data.json'
const recDataFile = './stats/recdata'
const processDataWorkerFile = './src/utils/processData.js'
const { Worker } = require('worker_threads')
const logger = require('../utils/logger.js')
const { searchData } = require('../utils/classes.js')
const idStats = require('../utils/ids.js')
const utils = require('../utils/utils.js')
const { addQuestion, getSubjNameWithoutYear } = require('./classes.js')
@@ -74,99 +73,95 @@ function ProcessIncomingRequest(recievedData, qdb, infos, dryRun, user) {
logger.DebugLog(data, 'actions', 3)
let allQLength = data.quiz.length
const worker = new Worker(processDataWorkerFile, {
workerData: {
data: data,
qdb: qdb,
},
})
logger.DebugLog('Question:', 'actions', 2)
logger.DebugLog(question, 'actions', 2)
let currentQuestion = createQuestion(
question.Q,
question.A,
question.data
)
logger.DebugLog(
'Searching for question in subj ' + data.subj,
'actions',
3
)
logger.DebugLog(currentQuestion, 'actions', 3)
worker.on('error', (err) => {
logger.Log('Process Data Worker error!', logger.GetColor('redbg'))
console.error(err)
reject(err)
})
searchData(qdb, currentQuestion, data.subj)
.then((allQuestions) => {
logger.DebugLog('Message from processData', 'actions', 1)
logger.DebugLog(allQuestions, 'actions', 3)
worker.on('exit', (code) => {
logger.DebugLog('Process Data exit, code: ' + code, 'actions', 1)
if (code !== 0) {
logger.Log(
'Process Data Worker error! Exit code is not 0',
logger.GetColor('redbg')
)
reject(new Error('Process Data Worker exit code is not 0!'))
}
})
try {
let color = logger.GetColor('green')
let msg = ''
if (allQuestions.length > 0) {
color = logger.GetColor('blue')
msg += `New questions: ${allQuestions.length} ( All: ${allQLength} )`
allQuestions.forEach((currentQuestion) => {
const sName = getSubjNameWithoutYear(data.subj)
logger.DebugLog(
'Adding question with subjName: ' + sName + ' :',
'actions',
3
)
logger.DebugLog(currentQuestion, 'actions', 3)
addQuestion(qdb, sName, currentQuestion)
})
worker.on('message', (workerMsg) => {
logger.DebugLog('Message from processData', 'actions', 1)
logger.DebugLog(workerMsg, 'actions', 3)
const allQuestions = workerMsg
try {
let color = logger.GetColor('green')
let msg = ''
if (allQuestions.length > 0) {
color = logger.GetColor('blue')
msg += `New questions: ${allQuestions.length} ( All: ${allQLength} )`
allQuestions.forEach((currentQuestion) => {
const sName = getSubjNameWithoutYear(data.subj)
currWrites++
logger.DebugLog(
'Adding question with subjName: ' + sName + ' :',
'currWrites for data.json: ' + currWrites,
'actions',
3
1
)
logger.DebugLog(currentQuestion, 'actions', 3)
addQuestion(qdb, sName, currentQuestion)
})
currWrites++
logger.DebugLog(
'currWrites for data.json: ' + currWrites,
'actions',
1
)
if (currWrites >= writeAfter && !dryRun) {
currWrites = 0
logger.DebugLog('Writing data.json', 'actions', 1)
utils.WriteFile(JSON.stringify(qdb), dataFile)
logger.Log('\tData file written', color)
} else if (dryRun) {
logger.Log('\tDry run')
if (currWrites >= writeAfter && !dryRun) {
currWrites = 0
logger.DebugLog('Writing data.json', 'actions', 1)
utils.WriteFile(JSON.stringify(qdb), dataFile)
logger.Log('\tData file written', color)
} else if (dryRun) {
logger.Log('\tDry run')
}
} else {
msg += `No new data ( ${allQLength} )`
}
} else {
msg += `No new data ( ${allQLength} )`
}
let subjRow = '\t' + data.subj
if (data.id) {
subjRow += ' ( CID: ' + logger.logHashed(data.id) + ')'
idStats.LogId(user.id, data.subj, allQuestions.length, allQLength)
}
logger.Log(subjRow)
if (data.version !== undefined) {
msg += '. Version: ' + data.version
}
let subjRow = '\t' + data.subj
if (data.id) {
subjRow += ' ( CID: ' + logger.logHashed(data.id) + ')'
idStats.LogId(user.id, data.subj, allQuestions.length, allQLength)
}
logger.Log(subjRow)
if (data.version !== undefined) {
msg += '. Version: ' + data.version
}
logger.Log('\t' + msg, color)
logger.DebugLog('New Questions:', 'actions', 2)
logger.DebugLog(allQuestions, 'actions', 2)
logger.Log('\t' + msg, color)
logger.DebugLog('New Questions:', 'actions', 2)
logger.DebugLog(allQuestions, 'actions', 2)
logger.DebugLog('ProcessIncomingRequest done', 'actions', 1)
resolve({
newQuestions: allQLength.length,
newDb: qdb,
})
} catch (error) {
console.log(error)
logger.Log(
'Error while processing processData worker result!',
logger.GetColor('redbg')
)
reject(new Error('Error while processing processData worker result!'))
}
})
logger.DebugLog('ProcessIncomingRequest done', 'actions', 1)
resolve({
newQuestions: allQLength.length,
newDb: qdb,
})
} catch (error) {
console.log(error)
logger.Log(
'Error while processing processData worker result!',
logger.GetColor('redbg')
)
reject(
new Error('Error while processing processData worker result!')
)
}
})
.catch((err) => {
logger.Log('Process Data Worker error!', logger.GetColor('redbg'))
console.error(err)
reject(err)
})
} catch (err) {
console.log(err)
logger.Log('Couldnt parse JSON data', logger.GetColor('redbg'))