ddstats-server/libs/utils/multithread/genericWorker.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

import { workerData, parentPort } from 'worker_threads'
import initLog from '../log.js'
2021-10-13 19:45:59 +02:00
let runScript = ''
let myName = ''
let log = initLog(myName)
parentPort.on(
'message',
2021-10-13 19:45:59 +02:00
async message => {
switch (message.type) {
2021-10-13 19:45:59 +02:00
case 'initWorker':
myName = message.name
log = initLog(myName)
2021-10-13 19:45:59 +02:00
process.env.DEBUG && log(`Started new thread -> «${myName}»`)
break
2021-10-13 19:45:59 +02:00
case 'runScript':
runScript = message.script
process.env.DEBUG && log(`Running script -> «${runScript}»`)
import(runScript).then(
script => script.main(message.data).then(
result => {
parentPort.postMessage({ result, id: message.id })
2021-10-13 19:45:59 +02:00
process.env.DEBUG && log(`Script done -> «${runScript}»`)
}
)
)
break
default:
2021-10-13 19:45:59 +02:00
log(`Invalid message -> ${message.type ?? 'No message type provided!'}`)
break
}
}
)