appsody / stacks

Appsody application stacks. This repo will be archived soon.
https://appsody.dev
Apache License 2.0
90 stars 120 forks source link

nodejs: worker threads are not working #568

Open andrewhughes101 opened 4 years ago

andrewhughes101 commented 4 years ago

Describe the bug When using worker threads in the nodejs stack, the following error is thrown when using appsody run:

Error: Module did not self-register

The error is not produced using a appsody build and then a docker run.

To Reproduce Steps to reproduce the behavior:

  1. appsody init nodejs
  2. enter the following code into your app.js
const { Worker, isMainThread, parentPort } = require('worker_threads');
if (isMainThread) {
  const worker = new Worker(__filename);
  worker.on('message', (msg) => { console.log(msg); });
} else {
  parentPort.postMessage('Hello world!');
}
  1. appsody run

Expected behavior The worker thread should send a message to the main thread which should then output Hello World!. This expected behaviour can be reproduced by running node app.js in node 12 or later

Environment Details (please complete the following information):

Screenshots If applicable, add screenshots to help explain your problem.

Screenshot 2019-12-13 at 09 55 51
sam-github commented 4 years ago

@andrewhughes101 This is because appmetrics-dash does not support being required into a worker_thread.

Using your app, reproduce without appsody by doing in your initialized app:

npm install appmetrics-dash
npm start --node-options --require=appmetrics-dash/attach

Fix is to do either, or all of:

  1. appmetrics-dash.attach() returns immediately if ! require('worker_threads').isMainThread.
  2. nodejs/node stops applying the --require=... to worker_threads
  3. appsody/stacks stops auto-injecting the dashboard.