如何在Hapijs中实现子进程派生?

时间:2019-03-04 12:37:20

标签: javascript node.js performance hapijs

有人可以帮助我如何在hapijs中实现子进程派生吗?我收到此错误

错误:方法未返回值,承诺或引发错误

当我运行下面提到的程序时。

控制器

const path = require('path');
import { fork } from 'child_process';
public async longComputationProcess(request: IRequest, h: Hapi.ResponseToolkit) {
    this.server.log('INFO', 'Report generation starts.');
    try {
      let payload: any = request.payload;
      const requesterId = request.auth.credentials.id;
      const parentPath = path.join(__dirname, '../../services');
      const forkedProcess = fork(`./child-process-service.js`, [payload, requesterId], { cwd: parentPath });
      forkedProcess.send(3e9);
      forkedProcess.on('message', async (sum) => {
        console.log('CP - Total Sum: ', sum);
                                return h.response(sum);
      });
      forkedProcess.on('close', (code) => {
        forkedProcess.kill();
      });
    } catch (err) {
      this.server.log('ERROR', err);
      return Boom.badImplementation(
        'Internal server error. Please contact admin.'
      );
    }
  }
}

child-process-service.js

process.on('message', async (limit) => {
  const sum = await longComputation(limit);
  process.send(sum);
});
const longComputation = (limit) => {
  let sum = 0;
  for (let i = 0; i < limit; i += 1) {
    sum += i;
  }
  return sum;
};

谢谢

0 个答案:

没有答案