Class: Broker

Broker

new Broker(id, optionopt)

Broker class.

Parameters:
Name Type Attributes Description
id string

A unique broker ID.

option object <optional>

Options.

Properties
Name Type Attributes Description
ns string <optional>

Namespace used for redis keys. Default is 'dw'.

clustername string <optional>

Name of cluster this broker will belong to. Defaults to 'main'.

redis object <optional>

Redis configuration.

Properties
Name Type Attributes Description
pub object <optional>

Redis client instance for standard operation.

sub object <optional>

Redis client instance for subscription.

port number <optional>

Redis server's port number (defaults to 6379).

host string <optional>

Redis server's IP address (defaults to '127.0.0.1').

rpcTimeout number <optional>

RPC transaction timeout. Defaults to 3000 msec.

ttl number <optional>

Recovery timeout. Defaults to 0 (unlimited). This value
does not affect active workers. It is only referenced during the recovery process.
A worker, even with recoverable flag set to true, won't be recovered if it's beyond its TTL.

batchReadSize number <optional>

The number of messages to read from message queue at a time. Defaults to 1000.

brokerCache object <optional>

Broker cache (LRU) configuration.

Properties
Name Type Attributes Description
max number <optional>

Max number of workers LRU cache can remember
to resolve broker ID (by worker ID).

maxAge number <optional>

Max number of milliseconds in which an entry
in the cache will be invalidated.

retries object <optional>

Retry behavior configuration.

Properties
Name Type Attributes Description
initialInterval number <optional>

Initial interval in msec. Defaults to 40.

maxInterval number <optional>

Max interval in msec. Defaults to 800.

duration number <optional>

Max duration in msec, in which the retries will stop.

healthCheckInterval number <optional>

Health-check interval in seconds. Defaults to 1.

Source:

Members

(static, constant) State :number

An enumeration of Broker states

Type:
  • number
Properties:
Name Type Description
INACTIVE number

Broker is inactive

ACTIVATING number

Broker is activating

ACTIVE number

Broker is active

DESTROYING number

Broker is being destroyed

DESTROYED number

Broker has been destroyed

Source:

Methods

createWorker(workerName, optionopt, cbopt) → {Promise}

Create a worker.

Parameters:
Name Type Attributes Description
workerName string

Name of the worker to be created.

option object <optional>

Options.

Properties
Name Type Attributes Description
id string <optional>

Worker ID to be assigned. If not specified and the worker
is dynamic (option.static is set to false), then dworker will assign a unique ID for
the new worker.

static string <optional>

If "static", then this method will create
a static worker (only one instance in the cluster).

attributes object <optional>

A bag of parameters passed to the new worker
created on another process (broker). Defaults to false.
which can be referenced by Worker#attributes..

Properties
Name Type Attributes Description
recoverable boolean <optional>

Mark the new worker as
recoverable. When a process (broker) dies, workers marked as recoverable
may be reconstructed if condition allows.

cb Broker~createWorkerCallback <optional>

Callback.

Source:
Returns:

Returns a promise if cb is not provided.
See Broker~createWorkerCallback.

Type
Promise

destroy(optionopt, cbopt) → {Promise}

Destroy the broker.

Parameters:
Name Type Attributes Description
option object <optional>

Options.

Properties
Name Type Attributes Description
noRecover boolean <optional>

Tell the broker not to recover any
workers (regardless of 'recoverable' option) this broker current have.

cb Broker~destroyWorkerCallback <optional>

Callback.

Source:
Returns:

Returns a promise if cb is not provided.
See Broker~destroyWorkerCallback.

Type
Promise

findWorker(workerId, cbopt) → {Promise}

Find worker.

Parameters:
Name Type Attributes Description
workerId string

ID of a worker to find. If the worker is a dynamic worker,
then the ID should look like: "MyWorker#12" as in the form of #.
If the worker is static, workerId is identical to the worker name. (no "#.

cb Broker~findWorkerCallback <optional>

Callback.

Source:
Returns:

Returns a promise if cb is not provided.
See Broker~findWorkerCallback.

Type
Promise

quit()

Quit redis clients.
This method is provided so that we can make sure to quit redis connections,
or to do so for testing. However, the redis clients' lifetime may be
managed by other place. Therefore, use this method with a care.

Source:

registerWorker(nameopt, ctor, optionopt) → {void}

Register a worker class.

Parameters:
Name Type Attributes Description
name string <optional>

Name of the worker class. If omitted, the name
will be ctor.classname or ctor.name (the function name of the ctor).

ctor function

Constructor of the worker to be registered.

option object <optional>

Option.

Properties
Name Type Attributes Description
clustername string <optional>

Name of cluster the worker belongs to.
If this option is omitted, it defaults to 'main'.

Source:
Returns:
Type
void

restart(cbopt) → {Promise}

Restart dworker. (Adminstrative purpose only)
All active workers will be removed with no recovery.

Parameters:
Name Type Attributes Description
cb Broker~restartCallback <optional>

Callback function.

Source:
Returns:

Returns a promise if cb is not provided.
See Broker~restartCallback.

Type
Promise

restartAll()

Trigger restart of all active brokers under the same namespace.
This method does not wait for completion of actual restart. This
method just signals restart command to all active brokers.

Source:

setWorkerRegistry(registry) → {void}

Set a custom worker class registry.

Parameters:
Name Type Description
registry object

Custom registry to be used. The object
must support the following methods:

  • add(name, constructor)
  • get(name, constructor)
Source:
Returns:
Type
void

start(cbopt) → {Promise}

Start this broker.

Parameters:
Name Type Attributes Description
cb Broker~startCallback <optional>

Callback function.

Source:
Returns:

Returns a promise if cb is not provided.
See Broker~startCallback.

Type
Promise

Type Definitions

createWorkerCallback(err, agent)

Callback function for Broker#createWorker.

Parameters:
Name Type Description
err Error

Error object.

agent Agent

An instance for remote Worker (called 'Agent') via which you can
access remote worker that has just been instantiated.

Source:

destroyWorkerCallback(err)

Callback function for Broker#destroyWorker.

Parameters:
Name Type Description
err Error

Error object.

Source:

findWorkerCallback(err, agent)

Callback function for Broker#findWorker.

Parameters:
Name Type Description
err Error

Error object.

agent Agent

An instance for remote Worker (called 'Agent') via which you can
access remote worker that has just been instantiated. If the worker does not exist,
the returned agent will be null.

Source:

restartCallback(err, data)

Callback function for Broker.prototype.restart.

Parameters:
Name Type Description
err Error

Error object.

data object

Request data.

Source:

startCallback(err, data)

Callback function for Broker.prototype.start.

Parameters:
Name Type Description
err Error

Error object.

data object

Request data.

Source: