Class: Synapse::Command::CommandGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/command/gateway.rb

Overview

Simplified interface to the command bus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_bus) ⇒ undefined

Parameters:



14
15
16
17
# File 'lib/synapse/command/gateway.rb', line 14

def initialize(command_bus)
  @command_bus = command_bus
  @filters = Array.new
end

Instance Attribute Details

#filtersArray (readonly)

Returns:

  • (Array)


7
8
9
# File 'lib/synapse/command/gateway.rb', line 7

def filters
  @filters
end

#retry_schedulerRetryScheduler

Returns:



10
11
12
# File 'lib/synapse/command/gateway.rb', line 10

def retry_scheduler
  @retry_scheduler
end

Instance Method Details

#send(command) ⇒ undefined

Fire and forget method of sending a command to the command bus

If the given command is a bare object, it will be wrapped in a command message before being dispatched on the command bus.

Parameters:

  • command (Object)

Returns:

  • (undefined)


27
28
29
# File 'lib/synapse/command/gateway.rb', line 27

def send(command)
  send_with_callback command, CommandCallback.new
end

#send_and_wait(command, timeout = nil) ⇒ Object

Sends the given command and blocks indefinitely until the result of the execution is provided, the timeout is created or the thread is interrupted.

If the given command is a bare object, it will be wrapped in a command message before being dispatched on the command bus.

Parameters:

  • command (Object)
  • timeout (Float) (defaults to: nil)

Returns:

  • (Object)

    The return value from the command handler

Raises:



61
62
63
64
65
# File 'lib/synapse/command/gateway.rb', line 61

def send_and_wait(command, timeout = nil)
  callback = FutureCallback.new
  send_with_callback command, callback
  callback.result timeout
end

#send_with_callback(command, callback) ⇒ undefined

Sends the given command

If the given command is a bare object, it will be wrapped in a command message before being dispatched on the command bus.

Parameters:

Returns:

  • (undefined)


40
41
42
43
44
45
46
47
48
# File 'lib/synapse/command/gateway.rb', line 40

def send_with_callback(command, callback)
  command = process_with_filters(CommandMessage.as_message(command))

  if @retry_scheduler
    callback = RetryingCallback.new callback, command, @retry_scheduler, @command_bus
  end

  @command_bus.dispatch_with_callback(command, callback)
end