# chpr-worker

> AMQP worker library

Latest version **3.2.3** (published 2019-04-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install chpr-worker
pnpm add chpr-worker
yarn add chpr-worker
bun add chpr-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 | 3.2.3 |
| Published | 2019-04-12 |
| First published | 2017-03-31 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.10.0 |
| Dependencies | 3 |
| Unpacked size | 25.4 KB |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Transcovo |
| Maintainers | cptechadmin |
| Keywords | worker, AMQP, lib |

## Links

- npm: https://www.npmjs.com/package/chpr-worker
- Repository: https://github.com/transcovo/chpr-worker
- Homepage: https://github.com/transcovo/chpr-worker#readme
- Issues: https://github.com/transcovo/chpr-worker/issues
- npm.io page: https://npm.io/package/chpr-worker

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) 4.17.11
- [stakhanov](https://npm.io/package/stakhanov.md) 0.7.3
- [chpr-logger](https://npm.io/package/chpr-logger.md) 2.9.0

## Alternatives

- [@cantoo/pdf-lib](https://npm.io/package/@cantoo/pdf-lib.md) — 297.9K weekly downloads
- [datatables.net-buttons](https://npm.io/package/datatables.net-buttons.md) — 200.1K weekly downloads
- [@ckeditor/ckeditor5-export-pdf](https://npm.io/package/@ckeditor/ckeditor5-export-pdf.md) — 167.0K weekly downloads
- [scanbot-web-sdk](https://npm.io/package/scanbot-web-sdk.md) — 15.0K weekly downloads
- [@syncfusion/ej2-angular-pdfviewer](https://npm.io/package/@syncfusion/ej2-angular-pdfviewer.md) — 8.8K weekly downloads

## Recent versions

- 3.2.3 (latest) — 2019-04-12
- 3.2.2 — 2018-11-26
- 3.2.1 — 2018-11-16
- 3.1.0 — 2018-11-05
- 3.0.0 — 2018-10-31
- 2.2.0 — 2018-09-28
- 2.1.2 — 2018-05-18
- 2.1.1 — 2017-12-18
- 2.1.0 — 2017-12-13
- 2.0.0 — 2017-12-13
- 1.2.0 — 2017-06-08
- 1.1.1 — 2017-06-07
- 1.1.0 — 2017-06-05
- 1.0.2 — 2017-04-05
- 1.0.1 — 2017-04-03
- … 1 more at https://npm.io/package/chpr-worker/versions

## README

# chpr-worker
[![CircleCI](https://circleci.com/gh/transcovo/chpr-worker.svg?style=shield)](https://circleci.com/gh/transcovo/chpr-worker)
[![codecov](https://codecov.io/gh/transcovo/chpr-worker/branch/master/graph/badge.svg)](https://codecov.io/gh/transcovo/chpr-worker)

chpr-worker allows you to easily create a worker that take tasks from an AMQP queue. It handles common concerns related to implementing a worker with AMQP:
- Number of tasks handled at the same time
- Timeout after which the task is considered as failed
- Handle disconnections from the AMQP server
- Validating the schema of the message specifying the task

**Note:** this package is a wrapper for the **[stakhanov](https://www.npmjs.com/package/stakhanov)** open-source package. It only adds our logger via 
dependency injection. For up-to-date documentation, please consult the stakhanov project's [repository](https://github.com/ChauffeurPrive/stakhanov).

**BREAKING CHANGE with version >= 2.x**

It is not possible anymore to create a single worker by calling `createWorker`:
```javascript
    const worker = workerlib.createWorker({...});
```

You should use `createWorkers` for all worker creations.

**WARNING**

The `queueName` configuration must be unique for each worker, otherwise messages won't necessarily 
be routed to the good consumer.

When listening, the lib will create a queue of the form `queueName.routingKey` for each routing 
key/handler. That is why the queueName config must really be unique, typically of the form 
`application.workername`. This will generate a unique queue name per routing key, of the form 
`application.workername.routingkey`.

Example:

```javascript
    function* handle(msg) { ... };
    function* validate(msg) { ... };

    const worker = workerlib.createWorkers([{
      handle: handle,
      validate: validate,
      routingKey: 'test.something_happened'
    }], {
      workerName: 'my worker',
      amqpUrl: 'amqp://guest:guest@localhost:5672',
      exchangeName: 'bus',
      queueName: 'test.test_watcher'
    }, {
      channelPrefetch: 50,
      taskTimeout: 30000,
      processExitTimeout: 3000,
      channelCloseTimeout: 500
    });
```
### Basic use

To listen on channel:
```javascript
    yield worker.listen();
```
To shutdown worker:
```javascript
    worker.close();
```
Remark: for testing purpose, as the close function will execute a process.exit, you can
add the following parameter to the close function:
```javascript
    worker.close(false);
```

By default, the `close` method will be called on SIGTERM/SIGINT signals and operate a graceful shutdown.
If you want to override this behaviour (not recommended for production workers),
you should specify `closeOnSignals: false` as an option.

### Events

To organize your own tests, the worker instance allows you to wait for specific event
with the `wait` method.

Example:

    yield worker.wait(worker.TASK_COMPLETED);

Spec:

    wait(event, timeout)

The wait method returns a Promise which will be completed when the expected event
occurs for the first time, or rejected after a timeout.

`event`: name of the event, required. Must be one of the available events:

- `worker.TASK_COMPLETED`: emitted when a task has been completed.
- `worker.TASK_RETRIED`: emitted when a task is going to be retried because the
 handler has failed.
- `worker.TASK_FAILED`: emitted when a task has failed because the handler
 has failed on a task which was already being retried.
- `worker.WORKER_CLOSED`: emitted when the worker has been closed.

`timeout`: timeout in milliseconds after which the promise will be rejected. Defaults
to 1000 ms.

### Dev Requirements

Install Node 6.

For nvm users, just move to the project directory and run :

    nvm i

If you already have installed Node 6 before, just type:

    nvm use

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