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) |
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:
(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. |
(static, constant) DEFAULT_LOAD :number
Default Worker load
Type:
- number
(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) |
(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 |
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
|
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. |
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
|
|||||||
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
|
|||||||
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. |
Type Definitions
onAskCallback(err, data)
Callback function for Worker#onAsk.
Parameters:
| Name | Type | Description |
|---|---|---|
err |
Error | Error object. |
data |
object | Request data. |
onCreateCallback(err, data)
Callback function for Worker#onCreate.
Parameters:
| Name | Type | Description |
|---|---|---|
err |
Error | Error object. |
data |
object | Request data. |
onDestroyCallback(err, data)
Callback function for Worker#onDestroy.
Parameters:
| Name | Type | Description |
|---|---|---|
err |
Error | Error object. |
data |
object | Request data. |