@nxus/worker-queue

2.3.0

src/index.js

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', (msg) => {
  console.log("Hello", msg.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', (msg) => {
  console.log("Hello", msg.hi)
  app.get('worker-queue').task('myBackgroundTask-complete', {result: true})
})
app.get('worker-queue').worker('myBackgroundTask-complete', (msg) => {
  console.log("Completed", msg.result)
})

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

API


WorkerQueue

src/index.js

Worker Queue module for background tasks

Instance members

#task(taskName, message)

Request handling of a background task

task(taskName, message)

src/index.js

Request handling of a background task

Parameters

  • string taskName :

    Name of the task (channel) to publish to

  • object message :

    Options for the task worker

Examples

app.get('worker-queue').task('backgroundJob', {hi: 'world'})
#worker(taskName, handler)

Provide a task handler

worker(taskName, handler)

src/index.js

Provide a task handler

Parameters

  • string taskName :

    Name of the task (channel) to listen for

  • function handler :

    Handler for processing task requests

Examples

app.get('worker-queue').worker('backgroundJob', (msg) -> {})