# run-on-worker

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

Latest version **2.0.0** (published 2021-01-15) · BSD license · 0 weekly downloads

## Install

```sh
npm install run-on-worker
pnpm add run-on-worker
yarn add run-on-worker
bun add run-on-worker
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2021-01-15 |
| First published | 2019-03-19 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Node | ^10 \|\| ^12 \|\| ^14 |
| Dependencies | 1 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Andrew Harris. |
| Maintainers | aeh, timlesallen |

## Links

- npm: https://www.npmjs.com/package/run-on-worker
- Repository: https://github.com/noblesamurai/node-run-on-worker
- Issues: https://github.com/noblesamurai/node-run-on-worker/issues
- npm.io page: https://npm.io/package/run-on-worker

## Dependencies (1)

- [serialize-error](https://npm.io/package/serialize-error.md) ^8.0.0

## Recent versions

- 2.0.0 (latest) — 2021-01-15
- 2.0.0-beta.0 (beta) — 2021-01-13
- 1.1.0-progress-events.1 (progress-events) — 2019-06-11
- 1.1.0 — 2019-06-11
- 1.1.0-progress-events.0 — 2019-06-05
- 1.0.0 — 2019-03-19

## README

# Run On Worker [![Build Status](https://api.travis-ci.com/noblesamurai/node-run-on-worker.svg?branch=master)](http://travis-ci.com/noblesamurai/node-run-on-worker) [![Maintainability](https://api.codeclimate.com/v1/badges/8eedb3b307882391272e/maintainability)](https://codeclimate.com/github/noblesamurai/node-run-on-worker/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/8eedb3b307882391272e/test_coverage)](https://codeclimate.com/github/noblesamurai/node-run-on-worker/test_coverage)

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

## Installation

This module is installed via npm:

``` bash
$ npm install run-on-worker
```

## Usage

```js
// 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

<a name="runOnWorker"></a>

## runOnWorker(workerFile, message) ⇒ <code>\*</code>
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**: <code>\*</code> - JSON.parsed response from the worker
**Throws**:

- Error


| Param | Type | Description |
| --- | --- | --- |
| workerFile | <code>string</code> | file to run as the worker process |
| message | <code>\*</code> | 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.

---
_Source: https://npm.io/package/run-on-worker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
