<commit_before># trailpack-tasker

[![NPM version][npm-image]][npm-url]
[![Build status][ci-image]][ci-url]
[![Dependency Status][daviddm-image]][daviddm-url]
[![Code Climate][codeclimate-image]][codeclimate-url]

Easily set up background workers with [RabbitMQ](https://www.rabbitmq.com/) and [Trails](http://trailsjs.io).

## Install

```sh
$ npm install --save trailpack-tasker
```

## Configure

```js
// config/main.js
module.exports = {
  packs: [
    // ... other trailpacks
    require('trailpack-tasker')
  ]
}
```

```js
// config/tasker.js
module.exports = {

  /**
   * Define worker profiles. Each worker of a given type listens for the
   * "tasks" defined in its profile below. The task names represent a Task
   * defined in api.tasks
   */
  profiles: {
    memoryBound: {
      tasks: [ 'hiMemoryTask1' ]
    },
    cpuBound: {
      tasks: [ 'VideoEncoder', 'hiCpuTask2' ]
    }
  },

  /**
   * Set RabbitMQ connection info
   */
  rabbitmq: {
    exchange: process.env.TASKER_EXCHANGE,
    host: 'localhost',
    port: 5672,
    username: 'guest',
    password: '<PASSWORD>',
    ssl: true
  },

  /**
   * Set worker to subscribe to tasks in the matching profile (tasker.profiles).
   * If false, the application will not subscribe to any tasks.
   */
  worker: process.env.WORKER || false
}
```

## Usage

Define tasks in `api.tasks`. Each task is run in a separate process.

```js
// api/tasks/VideoEncoder.js

const Task = require('trailpack-tasker').Task
module.exports = class VideoEncoder extends Task {

  /**
   * "payload" is the message from RabbitMQ, and contains all the information
   * the worker needs to do its job. By default, sets this.payload and this.app.
   *
   * @param payload.videoFormat
   * @param payload.videoBuffer
   */
  constructor (app, payload) {
    super(app, payload)
  }

  /**
   * Do work here. When the work is finished (the Promise is resolved), send
   * "ack" to the worker queue. You must override this method.<commit_msg>[doc] move task dir under services<commit_after># trailpack-tasker

[![NPM version][npm-image]][npm-url]
[![Build status][ci-image]][ci-url]
[![Dependency Status][daviddm-image]][daviddm-url]
[![Code Climate][codeclimate-image]][codeclimate-url]

Easily set up background workers with [RabbitMQ](https://www.rabbitmq.com/) and [Trails](http://trailsjs.io).

## Install

```sh
$ npm install --save trailpack-tasker
```

## Configure

```js
// config/main.js
module.exports = {
  packs: [
    // ... other trailpacks
    require('trailpack-tasker')
  ]
}
```

```js
// config/tasker.js
module.exports = {

  /**
   * Define worker profiles. Each worker of a given type listens for the
   * "tasks" defined in its profile below. The task names represent a Task
   * defined in api.services.tasks
   */
  profiles: {
    memoryBound: {
      tasks: [ 'hiMemoryTask1' ]
    },
    cpuBound: {
      tasks: [ 'VideoEncoder', 'hiCpuTask2' ]
    }
  },

  /**
   * Set RabbitMQ connection info
   */
  rabbitmq: {
    exchange: process.env.TASKER_EXCHANGE,
    host: 'localhost',
    port: 5672,
    username: 'guest',
    password: '<PASSWORD>',
    ssl: true
  },

  /**
   * Set worker to subscribe to tasks in the matching profile (tasker.profiles).
   * If false, the application will not subscribe to any tasks.
   */
  worker: process.env.WORKER || false
}
```

## Usage

Define tasks in `api.services.tasks`. Each task is run in a separate process.

```js
// api/services/tasks/VideoEncoder.js

const Task = require('trailpack-tasker').Task
module.exports = class VideoEncoder extends Task {

  /**
   * "payload" is the message from RabbitMQ, and contains all the information
   * the worker needs to do its job. By default, sets this.payload and this.app.
   *
   * @param payload.videoFormat
   * @param payload.videoBuffer
   */
  constructor (app, payload) {
    super(app, payload)
  }

  /**
   * Do work here. When the work is finished (the Promise is resolved), send
   * "ack" to the worker queue. You must override this method.
{'fraction_non_alphanumeric': 0.1387785501489573, 'fraction_numerical': 0.0029791459781529296, 'mean_word_length': 3.1154239019407557, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '690818', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1293, 'n_words': 386}