MLissCetrus
MLissCetrus

Reputation: 463

How can I access the return data from a worker-thread?

I have downloaded the following example that shows how to use worker-threads:

https://github.com/heroku-examples/node-workers-example.git

NOTE: This example requires Redis to be installed and running

What I am trying to figure out is how can I access the data that is returned from the worker thread?

I have added a 'testData' JSON object that has a start and end date and passed that JSON object to the queue.add

// Kick off a new job by adding it to the work queue
app.post('/job', async (req, res) => {
  // This would be where you could pass arguments to the job
  // Ex: workQueue.add({ url: 'https://www.heroku.com' })
  // Docs: https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queueadd

  var testData = {
    "dateStart": new Date(),
    "dateCompleted": null
  }

  console.log('testData start ' + testData.dateStart);

  let job = await workQueue.add( testData );
  res.json({ id: job.id });
});

Here is the output from the log statement in the above function:

07:52:47 web.1      |  testData start Mon Apr 13 2020 07:52:47 GMT-0700 (Pacific Daylight Time)

I have modified the workQueue.process function to add the ending date and spit out a few log statements:

  workQueue.process(maxJobsPerWorker, async (job) => {
    // This is an example job that just slowly reports on progress
    // while doing no work. Replace this with your own job logic.
    let progress = 0;

    console.log('Processing job', job.id,  job.data);

    // throw an error 5% of the time
    if (Math.random() < 0.05) {
      throw new Error("This job failed!")
    }

    while (progress < 100) {
      await sleep(50);
      progress += 1;
      job.progress(progress)
    }

    job.data.dateCompleted = new Date();
    console.log('ending job ' + JSON.stringify(job.data));

    // A job can return values that will be stored in Redis as JSON
    // This return value is unused in this demo application.
    return {r:job.data};
  });
}

Here is the log output from the above function:

07:52:47 worker.1   |  Processing job 76 {
07:52:47 worker.1   |    dateStart: '2020-04-13T14:52:47.785Z',
07:52:47 worker.1   |    dateCompleted: null }
07:52:52 worker.1   |  ending job {"dateStart":"2020-04-13T14:52:47.785Z","dateCompleted":"2020-04-13T14:52:52.831Z"}

Now comes the fun part, here is the code that catches when the worker thread is completed. I have just added logging statements

workQueue.on('global:completed', (jobId, result) => {

  console.log(`Job completed with result ${result}`);

  console.log('result ' + result );
  console.log('result.r ' + result.r );

  console.log('end ' + result.dateCompleted);
  console.log('beg ' + result.dateStart);
  //var diff = result.dateCompleted - result.dateStart;
  //console.log('duration ' + JSON.stringify( diff ));
});

Here is the output:

07:52:52 web.1      |  Job completed with result {"r":{"cnt":100,"dateStart":"2020-04-13T14:52:47.785Z","dateCompleted":"2020-04-13T14:52:52.831Z"}}
07:52:52 web.1      |  result {"r":{"dateStart":"2020-04-13T14:52:47.785Z","dateCompleted":"2020-04-13T14:52:52.831Z"}}
07:52:52 web.1      |  result.r undefined
07:52:52 web.1      |  end undefined
07:52:52 web.1      |  beg undefined

To me it looks like the data is being set in the worker-thread, and the console log statement "knows" about it... but what I don't understand is why is result.r undefined and why can't I access the dateStart and dateCompleted values?

UPDATE:

Added the following to my code:

  var res = JSON.parse(result);
  console.log('res ' + JSON.stringify(res) );
  console.log('res.r.dateStart ' + res.r.dateStart );

Here is the output:

08:52:34 web.1      |  res {"r":{"dateStart":"2020-04-13T15:52:29.272Z","dateCompleted":"2020-04-13T15:52:34.338Z"}}
08:52:34 web.1      |  res.r.dateStart 2020-04-13T15:55:11.965Z

Upvotes: 1

Views: 1826

Answers (1)

MLissCetrus
MLissCetrus

Reputation: 463

The result is being returned as a string. Per msbit, I needed to add the following:

var res = JSON.parse(result);

Then everything works as expected.

Upvotes: 1

Related Questions