Build Status

Using Redis for pub/sub background tasks

Installation

> npm install nxus-worker-queue --save

Usage

For each task, you need to define a unique task name.

Register a worker handler

app.get('worker-queue').worker('myBackgroundTask', ({data}) => {
  this.app.log.debug("Hello", data.hi)
})

Request task processing

app.get('worker-queue').task('myBackgroundTask', {hi: world})

Receive notifications of completed tasks

Register two tasks, one for processing and one for notifications, and trigger the second from within the first handler.

app.get('worker-queue').worker('myBackgroundTask', ({data}) => {
  this.app.log.debug("Hello", data.hi)
  app.get('worker-queue').task('myBackgroundTask-complete', {result: true})
})
app.get('worker-queue').worker('myBackgroundTask-complete', ({data}) => {
  this.app.log.debug("Completed", data.result)
})

app.get('worker-queue').task('myBackgroundTask', {hi: world})

API


Worker Queue module for background tasks

index

Provide a task handler

worker(taskName: string, handler: function)
Parameters
taskName (string) Name of the task (channel) to listen for
handler (function) Handler for processing task requests
Example
app.get('worker-queue').worker('backgroundJob', (msg) -> {})

Request handling of a background task

task(taskName: string, message: object)
Parameters
taskName (string) Name of the task (channel) to publish to
message (object) Options for the task worker; must be JSON serializable
Example
app.get('worker-queue').task('backgroundJob', {hi: 'world'})

Cleans the current queue for the given taskName. Good idea to do this on occasion as Bull will keep all completed tasks in Redis.

clean(taskName: string)
Parameters
taskName (string) The name of the queue to clean. If not provided, all queues are cleaned.

Emptys the current queue for the given taskName.

empty(taskName: string)
Parameters
taskName (string) The name of the queue to empty. If not provided, all queues are emptied.