Interface: Worker

Worker

Worker class constructor.
Application must subclass this to implement abstract members.
Do not instantiate directly from application. Worker instances are created by Broker.
The subclass must pass all the arguments to this base Worker class. (See example)

Properties:
Name Type Description
id string

Worker ID (read-only)

state number

Current worker state (read-only)

attributes object

Attributes given by creator of this worker. (read-only)

load number

Current load imposed by this worker. (read/write)

Source:

Example

var util = require('util');
var Worker = require('dworker').Worker;

function MyWorker() {
    Worker.apply(this, arguments);
    // Your code here
}

util.inherits(MyWorker, Worker);

// Override abstract method
MyWorker.prototype.onCreate = function (info) {
    // Your implementation
};
// Override abstract method
MyWorker.prototype.onDestroy = function (info) {
    // Your implementation
};
// Override abstract method
MyWorker.prototype.onAsk = function (method, data) {
    // Your implementation
};
// Override abstract method
MyWorker.prototype.onTell = function (method, data) {
    // Your implementation
};

Members

(static) agent :Agent

Optional custom Agent class property. Set to undefined by default (uses base Agent class).

Type:
Source:

(static, constant) CreateCause :number

Enumeration of Worker creation causes

Type:
  • number
Properties:
Name Type Description
NEW number

Worker was created for the first time.

RECOVERY number

Worker was created as a result of recovery process.

Source:

(static, constant) DEFAULT_LOAD :number

Default Worker load

Type:
  • number
Source:

(static, constant) DestroyCause :number

Enumeration of Worker destroy causes

Type:
  • number
Properties:
Name Type Description
SELF number

Worker is destroyed by itself.

SYSTEM number

Worker is destroyed by the system. (server shutdown, etc)

Source:

(static, constant) State :number

An enumeration of Worker states

Type:
  • number
Properties:
Name Type Description
INACTIVE number

Worker is inactive

ACTIVATING number

Worker is being activated

ACTIVE number

Worker is active

DESTROYING number

Worker is being destroyed

DESTROYED number

Worker has been destroyed

Source:

Methods

destroy(optionopt) → {void}

Destroy this worker.
Call of this method will trigger onDestroy callback with
info.cause being DestroyCause#SELF.

Parameters:
Name Type Attributes Description
option object <optional>

Options.

Properties
Name Type Attributes Description
noRecover boolean <optional>

No recover option. This is set to true by default.
When this option is set to false, and this worker is created with recoverable option
set to true, this worker will be recreated right away somewhere in the same cluster.
This option is provide only for development purpose. (e.g. to test restoration of
context data on DB, etc.) Do not set this option to false in production for
obvious reasons.

Source:
Returns:
Type
void

(abstract) onAsk(method, data, cbopt) → {Promise}

Called when ask operation is received.
Application must override this method to handle ask operation.

Parameters:
Name Type Attributes Description
method string

Method name.

data object

Data sent by requesting entity.

cb Worker~onAskCallback <optional>

Callback from this method.

Source:
Returns:

Returns a Promise if cb is not supplied. See Worker~onAskCallback

Type
Promise

(abstract) onCreate(info, cbopt) → {Promise}

Called when this worker is created.

Parameters:
Name Type Attributes Description
info object

Information of this callback.

Properties
Name Type Description
cause number

Cause of the creation.

cb Worker~onCreateCallback <optional>

Callback from this method.

Source:
See:
Returns:

Returns a Promise if cb is not supplied. See Worker~onCreateCallback
Error return, or rejection of promise, will be ignored by
the caller.

Type
Promise

onDestroy(info, cbopt) → {Promise}

Called when this worker is about to be destroyed.

Parameters:
Name Type Attributes Description
info object

Information of this callback.

Properties
Name Type Description
cause number

Cause of the destruction.

cb Worker~onDestroyCallback <optional>

Callback from this method.

Source:
See:
Returns:

Returns a Promise if cb is not supplied. See Worker~onDestroyCallback
Error return, or rejection of promise, will be ignored by
the caller.

Type
Promise

(abstract) onTell(method, data)

Called when tell operation is received.
Application must override this method to handle tell operation.

Parameters:
Name Type Description
method string

Method name.

data object

Data sent by requesting entity.

Source:

Type Definitions

onAskCallback(err, data)

Callback function for Worker#onAsk.

Parameters:
Name Type Description
err Error

Error object.

data object

Request data.

Source:

onCreateCallback(err, data)

Callback function for Worker#onCreate.

Parameters:
Name Type Description
err Error

Error object.

data object

Request data.

Source:

onDestroyCallback(err, data)

Callback function for Worker#onDestroy.

Parameters:
Name Type Description
err Error

Error object.

data object

Request data.

Source: