2021-10-13 16:40:18 +02:00
|
|
|
import { workerData, parentPort } from 'worker_threads'
|
|
|
|
import initLog from '../log.js'
|
|
|
|
|
2021-10-13 19:45:59 +02:00
|
|
|
let runScript = ''
|
2021-10-13 16:40:18 +02:00
|
|
|
let myName = ''
|
|
|
|
|
|
|
|
let log = initLog(myName)
|
|
|
|
|
|
|
|
parentPort.on(
|
|
|
|
'message',
|
2021-10-13 19:45:59 +02:00
|
|
|
async message => {
|
2021-10-13 16:40:18 +02:00
|
|
|
switch (message.type) {
|
2021-10-13 19:45:59 +02:00
|
|
|
case 'initWorker':
|
2021-10-13 16:40:18 +02:00
|
|
|
myName = message.name
|
|
|
|
log = initLog(myName)
|
2021-10-13 19:45:59 +02:00
|
|
|
process.env.DEBUG && log(`Started new thread -> «${myName}»`)
|
2021-10-13 16:40:18 +02:00
|
|
|
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 => {
|
2021-10-14 19:20:09 +02:00
|
|
|
parentPort.postMessage({ result, id: message.id })
|
2021-10-13 19:45:59 +02:00
|
|
|
process.env.DEBUG && log(`Script done -> «${runScript}»`)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2021-10-13 16:40:18 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
2021-10-13 19:45:59 +02:00
|
|
|
log(`Invalid message -> ${message.type ?? 'No message type provided!'}`)
|
2021-10-13 16:40:18 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|