API Docs for: 0.8.0
Show:

CommandService Class

Provides a mechanism to send commands to all members of the cluster or a specific member.

Constructor

CommandService

(
  • broker
)

Parameters:

  • broker CommandBroker

Methods

getInstance

() CommandService static

Retrieves the singleton instance of CommandService.

Returns:

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 Function

    A callback that takes two parameters: cb(Error, TRUE/FALSE)

isRegistered

(
  • type
  • handler
)
Number

Determines if the provided handler is registered for the given type.

Parameters:

  • type Object
  • handler Object

Returns:

Number:

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 Object

    The command to delegate

onCommandReceived

(
  • commandService
)
Function static

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:

Returns:

Function:

(string, object)

registerForType

(
  • type
  • handler
)
Boolean

Registers a handler for incoming commands of the specified type.

Parameters:

  • type String

    The name/type of the command to handle

  • handler Function

    A function that takes two parameters: handler(channel, command). where channel is a string and command is an object.

Returns:

Boolean:

TRUE if the the handler was registered, FALSE if not.

sendCommand

(
  • type
  • [options]
  • [cb]
)

Sends a command to the cluster

Parameters:

  • type String

    The command name/type

  • [options] Object optional

    The options that will be serialized and sent to the other processes in the cluster

    • [to] String optional

      The cluster process that should handle the message

  • [cb] Function optional

    A 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 String

    The name/type of the command being sent

  • [options] Object optional

    The 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 optional

      Timeout in milliseconds for each process to respond.

    • [progress] Function optional

      A 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 Function

    A 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 Object

    The command that was sent to ths process

  • responseCommand Object

    The command to send back to the entity that sent the first command.

  • [cb] Function optional

    A callback that provides two parameters: cb(Error, Command ID)

shutdown

(
  • cb
)

Shuts down the command service and the broker if initialized

Parameters:

  • cb Function

    A callback that takes two parameters: cb(Error, TRUE/FALSE)

unregisterForType

(
  • type
  • handler
)
Boolean

Unregisters a handler for the specified type.

Parameters:

  • type String

    The name/type of the command that the handler is registered for

  • handler Function

    The handler function to unregister

Returns:

Boolean:

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

INSTANCE

CommandService private static

The singleton instance

NOT_FOUND

Number static

Indicates that the handler was not found