Record stdout and stderr from the worker. Add a more detailed example

This commit is contained in:
2020-11-02 12:03:44 +00:00
parent 56ab028762
commit 4e1377241f
3 changed files with 88 additions and 11 deletions

View File

@@ -1,3 +1,4 @@
const dotenv = require('dotenv')
const EventEmitter = require('events')
const threads = require('worker_threads')
const path = require('path')
@@ -6,6 +7,8 @@ const fs = require('fs')
const Exceptions = require('./exceptions.js')
const CalcTask = require('./CalcTask.js')
dotenv.config()
// Basic utility functions to help the queue manager
const CalcQueueUtils = {
getCalcWorker: function(calcFn) {
@@ -28,8 +31,21 @@ const CalcQueueUtils = {
const task = this.queue.shift()
task._worker = new threads.Worker(task.workerPath())
task._worker = new threads.Worker(
task.workerPath(),
{
stderr: true,
stdout: true,
workerData: { 'id': task._id }
}
)
task._worker.stdout.on('data', (chunk) => {
task._stdout = task._stdout.concat(chunk)
})
task._worker.stderr.on('data', (chunk) => {
task._stderr = task._stderr.concat(chunk)
})
task._worker.on('message', (msg) => {
task._messages.push(msg)
})
@@ -97,8 +113,19 @@ CalcQueue.prototype = {
'id': q.id(),
'calcFn': q.calcFn(),
'result': q.result(),
'stdout': q.stdout().trim().split('\n'),
'stderr': q.stderr().trim().split('\n'),
'messages': q.messages(),
'error': (q.error() == null ? null : q.error().message)
'error': (
q.error() == null
? null
: q.error().name + ': ' + q.error().message
),
'errorStack': (
process.env.NODE_ENV !== 'development' || q.error() == null
? null
: q.error().stack.split('\n')
)
}
})
},