Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noblesamurai/node-run-on-worker
Run a node.js module in a separate process using cluster.
https://github.com/noblesamurai/node-run-on-worker
Last synced: about 1 month ago
JSON representation
Run a node.js module in a separate process using cluster.
- Host: GitHub
- URL: https://github.com/noblesamurai/node-run-on-worker
- Owner: noblesamurai
- License: other
- Created: 2019-03-18T23:58:24.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-07-13T02:59:53.000Z (over 3 years ago)
- Last Synced: 2024-12-03T09:05:28.088Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
## 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
| Param | Type | Description |
| --- | --- | --- |
| workerFile |string
| file 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.## Contributing
### Prerequisites
```
$ pip install pre-commit
```### Installation
```
$ pre-commit install --install-hooks
```## 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.