Class: Server

Server

Ponos server class. Given a queue adapter the worker server will connect to RabbitMQ, subscribe to the given queues, and begin spawning workers for incoming jobs. The only required option is `opts.queues` which should be a non-empty flat list of strings. The server uses this list to subscribe to only queues you have provided.

Constructor

new Server(opts)

Parameters:
Name Type Description
opts Object Options for the server.
Properties
Name Type Attributes Default Description
errorCat ErrorCat <optional>
An error cat instance to use for the server.
events Object.<String, function()> <optional>
Mapping of event (fanout) exchanges which to subscribe and handlers.
log bunyan <optional>
A bunyan logger to use for the server.
name String <optional>
ponos A name to namespace the created exchange queues.
rabbitmq Object <optional>
RabbitMQ connection options.
Properties
Name Type Attributes Default Description
channel Object <optional>
RabbitMQ channel options.
Properties
Name Type Attributes Description
prefetch Object <optional>
Set prefetch for each consumer in a channel.
hostname String <optional>
localhost Hostname for RabbitMQ. Can be set with environment variable RABBITMQ_HOSTNAME.
port Number <optional>
5672 Port for RabbitMQ. Can be set with environment variable RABBITMQ_PORT.
username String <optional>
Username for RabbitMQ. Can be set with environment variable RABBITMQ_USERNAME.
password String <optional>
Username for Password. Can be set with environment variable RABBITMQ_PASSWORD.
tasks Object.<String, function()> <optional>
Mapping of queues to subscribe directly with handlers.
Author:
  • Bryan Kendall
  • Ryan Sandor Richards
Source:

Methods

consume() → {Promise}

Start consuming from the subscribed queues. This is called by `.start`. This can be called after the server has been started to start consuming from additional queues.
Source:
Returns:
Promise resolved when consuming has started.
Type
Promise

setAllEvents(map) → {Server}

Takes a map of event exchanges and handlers and subscribes to them all.
Parameters:
Name Type Description
map Object.<String, function()> A map of exchanges and task handlers.
Properties
Name Type Description
key String Exchange name.
value Object Object with handler and additional options for the worker (must have a `.task` handler function)
value function Handler function to take a job.
Source:
Returns:
The server.
Type
Server

setAllTasks(map) → {Server}

Takes a map of queues and task handlers and sets them all.
Parameters:
Name Type Description
map Object.<String, function()> A map of queue names and task handlers.
Properties
Name Type Description
key String Queue name.
value Object Object with a handler and additional options for the worker (must have a `.task` handler function)
value function Handler function to take a job.
Source:
Returns:
The server.
Type
Server

setEvent(exchangeName, task, optsopt) → {Server}

Assigns a task to an exchange.
Parameters:
Name Type Attributes Description
exchangeName String Exchange name.
task function Function to take a job and return a promise.
opts Object <optional>
Options for the worker that performs the task.
Source:
Returns:
The server.
Type
Server

setTask(queueName, task, optsopt) → {Server}

Assigns a task to a queue.
Parameters:
Name Type Attributes Description
queueName String Queue name.
task function Function to take a job and return a promise.
opts Object <optional>
Options for the worker that performs the task.
Source:
Returns:
The server.
Type
Server

start() → {Promise}

Starts the worker server, connects to RabbitMQ, subscribes and consumes from all the provided queues and exchanges (tasks and events).
Source:
Returns:
Promise that resolves once the server is listening.
Type
Promise

stop() → {Promise}

Stops the worker server, unsubscribing and disconnecting from RabbitMQ.
Source:
Returns:
A promise that resolves when the server is stopped.
Type
Promise