CommandService Class
Provides a mechanism to send commands to all members of the cluster or a specific member.
Constructor
CommandService
-
broker
Parameters:
-
broker
CommandBroker
Item Index
Methods
Properties
- AWAITING_RESPONSE static
- DEFAULT_TIMEOUT static
- ERROR_INDEX static
- INITIALIZED static
- INSTANCE static
- NOT_FOUND static
Methods
init
-
cb
Initializes the service and the broker implementation. The broker is determined by the configuration value of "command.broker". This value can be "redis" for the out of the box implementation for Redis or an absolute path to another implementation.
Parameters:
-
cb
FunctionA callback that takes two parameters: cb(Error, TRUE/FALSE)
isRegistered
-
type
-
handler
Determines if the provided handler is registered for the given type.
Parameters:
-
type
Object -
handler
Object
Returns:
The index in the storage array where the handle to the function is kept. CommandService.NOT_FOUND if the handle is not registered for the type. CommandService.ERROR_INDEX when the parameters are invalid.
notifyOfCommand
-
command
Responsible for delegating out the received command to the registered handlers. The command parameter must be an object, must have a type property that is a string, and must have a registered handler for the specified type.
Parameters:
-
command
ObjectThe command to delegate
onCommandReceived
-
commandService
The global handler for incoming commands. It registers itself with the broker and then when messages are received it verifies that the message is meant for this member of the cluster (or all members) then proceeds to handoff to the function that will delegate out to the handlers.
Parameters:
-
commandService
CommandService
Returns:
(string, object)
registerForType
-
type
-
handler
Registers a handler for incoming commands of the specified type.
Parameters:
-
type
StringThe name/type of the command to handle
-
handler
FunctionA function that takes two parameters: handler(channel, command). where channel is a string and command is an object.
Returns:
TRUE if the the handler was registered, FALSE if not.
sendCommand
-
type
-
[options]
-
[cb]
Sends a command to the cluster
Parameters:
-
type
StringThe command name/type
-
[options]
Object optionalThe options that will be serialized and sent to the other processes in the cluster
-
[to]
String optionalThe cluster process that should handle the message
-
-
[cb]
Function optionalA callback that provides two parameters: cb(Error, Command ID)
sendCommandGetResponse
-
type
-
options
-
onResponse
Sends a command to a single process in the cluster expecting a response.
Parameters:
-
type
String -
options
Object-
to
String -
[timeout]
Integer optional
-
-
onResponse
Function
sendCommandToAllGetResponses
-
type
-
[options]
-
cb
Sends a command to all processes iin the cluster and waits for a response from all before calling back.
Parameters:
-
type
StringThe name/type of the command being sent
-
[options]
Object optionalThe options for the command. The options object becomes the command object. Custom properties to be part of the command can be added. However, certain properties do have special meaning such as "id", "to", "from", "timeout", "includeme". These special properties may be overriden by this function or one it calls in order for the commands to function properly.
-
[id]
String optional -
[ignoreme]
Boolean optional -
[timeout=2000]
Integer optionalTimeout in milliseconds for each process to respond.
-
[progress]
Function optionalA function called right before each command is sent. The function should take two parameters. The first is the index of the task being executed and the second is the total number of tasks.
-
-
cb
FunctionA callback that provides two parameters: cb(Error, Array)
sendInResponseTo
-
command
-
responseCommand
-
[cb]
Provides a mechanism to respond to a the entity that sent the command.
Parameters:
-
command
ObjectThe command that was sent to ths process
-
responseCommand
ObjectThe command to send back to the entity that sent the first command.
-
[cb]
Function optionalA callback that provides two parameters: cb(Error, Command ID)
shutdown
-
cb
Shuts down the command service and the broker if initialized
Parameters:
-
cb
FunctionA callback that takes two parameters: cb(Error, TRUE/FALSE)
unregisterForType
-
type
-
handler
Unregisters a handler for the specified type.
Parameters:
-
type
StringThe name/type of the command that the handler is registered for
-
handler
FunctionThe handler function to unregister
Returns:
TRUE if the handler was unregistered, FALSE if not.
Properties
AWAITING_RESPONSE
Object
private
static
A hash of the brokers that are available out of the box
DEFAULT_TIMEOUT
Integer
private
static
The default timeout in milliseconds (2 seconds)
ERROR_INDEX
Number
static
Indicates that an error occurrred while attempting to check if a handler was registered
INITIALIZED
Boolean
private
static
The singleton instance
NOT_FOUND
Number
static
Indicates that the handler was not found