2.0.0 • Published 4 years ago

run-on-worker v2.0.0

Weekly downloads
9
License
BSD
Repository
github
Last release
4 years ago

Run On Worker Build Status Maintainability Test Coverage

Run a node.js module in a separate process using cluster.

Installation

This module is installed via npm:

$ npm install run-on-worker

Usage

// worker.js
const { serializeError } = require('serialize-error');

process.once('uncaughtException', process.exit.bind(process, 1));
process.once('unhandledRejection', process.exit.bind(process, 1));
process.once('message', request => {
  try {
    const message = JSON.parse(request);
    const response = message.reduce((acc, n) => acc + n, 0);
    process.send(JSON.stringify({ response }));
  } catch (err) {
    process.send(JSON.stringify({ error: serializeError(err) }));
    // or if you want to use serialize-error
    // process.send(JSON.stringify({ error: { message: err.message, code: 42 } });
  }
  process.exit();
});

// main.js
async function sum (values) {
  const workerFile = path.resolve(__dirname, 'worker.js');
  return runOnWorker(workerFile, values);
}

try {
  const result = await sum([ 2, 3, 6 ]); // 11
} catch (err) {
}

API

runOnWorker(workerFile, message) ⇒ *

Run a process on a worker and return the results.

The worker will receive a JSON stringified message object and should return a JSON stringified response { response } or { error } object. If an onProgress function is provided, it will recieve info sent back from the worker via a stringified { progress } object.

If an error is returned it will be automatically converted into an Error using the deserialise function provided in the serialize-error module. The worker can also use the serialize-error module to serialise errors to be returned.

Kind: global function Returns: * - JSON.parsed response from the worker Throws:

  • Error
ParamTypeDescription
workerFilestringfile to run as the worker process
message*anything that can be JSON stringified to be went to the worker process.

Note: To regenerate this section from the jsdoc run npm run docs and paste the output above.

License

The BSD License

Copyright (c) 2019, Andrew Harris.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the Andrew Harris. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.