# step-function-worker

> Easy AWS step function activity worker in node.js

Latest version **3.0.0** (published 2021-10-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install step-function-worker
pnpm add step-function-worker
yarn add step-function-worker
bun add step-function-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.0.0 |
| Published | 2021-10-19 |
| First published | 2017-07-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0 |
| Dependencies | 2 |
| Unpacked size | 52.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Pierre Colle |
| Maintainers | piercus |
| Keywords | step-function, worker, aws, stepfunction, activity |

## Links

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

## Dependencies (2)

- [aws-sdk](https://npm.io/package/aws-sdk.md) ^2.82.0
- [aws-arn-parser](https://npm.io/package/aws-arn-parser.md) ^1.0.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2021-10-19
- 2.1.0 — 2019-01-29
- 2.0.1 — 2018-12-08
- 2.0.0 — 2018-10-24
- 1.1.0 — 2018-06-29
- 1.0.0 — 2018-04-24
- 0.0.3 — 2017-12-14
- 0.0.2 — 2017-07-08
- 0.0.1 — 2017-07-08

## README

[![Build Status](https://app.travis-ci.com/piercus/step-function-worker.svg?branch=master)](https://app.travis-ci.com/piercus/step-function-worker)
[![codecov](https://codecov.io/gh/piercus/step-function-worker/branch/master/graph/badge.svg)](https://codecov.io/gh/piercus/step-function-worker)

# step-function-worker

Create a nodejs aws step-function worker/pooler easily :-)

## install

```
npm install step-function-worker
```

### Example usage

#### Basic example

```javascript
const fn = function(input, cb, heartbeat){
  // do something
  doSomething(input)

  // call heartbeat to avoid timeout
  heartbeat()

  // call callback in the end
  cb(null, {"foo" : "bar"}); // output must be compatible with JSON.stringify
};

const worker = new StepFunctionWorker({
  activityArn : '<activity-ARN>',
  workerName : 'workerName',
  fn : fn,
  taskConcurrency : 22, // default is null = Infinity
  poolConcurrency : 2 // default is 1
});
```

### Concurrency management

Since version **3.0**, `concurrency` has been replaced by `poolConcurrency` and `taskConcurrency`.

see more information in https://github.com/piercus/step-function-worker/issues/16#issuecomment-486971866

* `poolConcurrency` is the maximum number of parallel getActivity, http request (see [`sdk.getActivity`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/StepFunctions.html#getActivityTask-property)) (default: `1`) Increase this to have a more responsive worker, decrease this to consume less http connections.

* `taskConcurrency` (`null` means Infinite) represents the maximum number of parallel tasks done by the worker (default: equals to `poolConcurrency`).

Anyway, you should always have `poolConcurrency` <= `taskConcurrency`.

#### Set the Region

By default, this package is built on top of `aws-sdk` so you should set your AWS Region by changing `AWS_REGION` environment variable.

If you want to set it in JS code directly you can do it using `awsConfig` (see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html to see all available options) like 

```javascript
const worker = new StepFunctionWorker({
  activityArn : '<activity-ARN>',
  workerName : 'workerName',
  fn : fn,
  awsConfig: {
    region: '<your-region>'
  }
});
```

#### Close the worker

```javascript
// when finish close the worker with a callback
// this closing process may take up to 60 seconds per concurent worker, to close all connections smoothly without loosing any task
worker.close(function(){
  process.exit();
})
```

#### Get info on current worker

```javascript
// A worker as multiple poolers and multiple running tasks
// You can have infos about it by doing
const {poolers, tasks} = worker.report();

// poolers is an array of {
//   startTime: <Date>,
//   workerName: <String>,
//   status: <String>
// }
//
// tasks is an array of {
//  taskToken: <String>,
//  input: <Object>,
//  startTime: <Date>
// }
//
```

#### Custom logging with winston

You can customize logging by using a [winston](https://www.npmjs.com/package/winston) logger (or winston-like logger) as input

```javascript
const winston = require('winston');

const logger = winston.createLogger({
  level: 'debug',
  format: winston.format.json(),
  defaultMeta: { service: 'user-service' },
  transports: [
    //
    // - Write to all logs with level `info` and below to `combined.log` 
    // - Write all logs error (and below) to `error.log`.
    //
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    new winston.transports.File({ filename: 'combined.log' })
  ]
});

const worker = new StepFunctionWorker({
  activityArn : '<activity-ARN>',
  workerName : 'workerName',
  fn : fn,
  logger
});
```

Alternatively, you can just use a winston-like logger

```javascript
const logger = console;

const worker = new StepFunctionWorker({
  activityArn : '<activity-ARN>',
  workerName : 'workerName',
  fn : fn,
  logger
});
```

#### Events


```javascript
// when a task starts
worker.on('task', function(task){
  // task.taskToken
  // task.input
  console.log("task ", task.input)
});

// when a task fails
worker.on('failure', function(failure){
  // out.error
  // out.taskToken
  console.log("Failure :",failure.error)
});

// when a heartbeat signal is sent
worker.on('heartbeat', function(beat){
  // out.taskToken
  console.log("Heartbeat");
});

// when a task succeed
worker.on('success', function(out){
  // out.output
  // out.taskToken
  console.log("Success :",out.output)
});

// when an error happens
worker.on('error', function(err){
  console.log("error ", err)
});

// when the worker has no more task to process
worker.on('empty', function(){
  console.log("error ", err)
});

// when the worker reaches taskConcurrency tasks
worker.on('full', function(err){
  console.log("error ", err)
});
```

### Documentation

See JSDoc in the code.

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