class Souffle::PollingEvent

Eventmachine polling event helper.

Attributes

error_handler[RW]

The proc to run when the timeout has occurred.

event_loop[RW]

The event loop proc, should call complete on success.

interval[R]

The interval to run the periodic timer against the event_loop.

node[RW]

The node to run the polling event against.

pre_event[RW]

The proc to run prior to the periodic event loop.

state[RW]

The current state of the polling event.

timeout[R]

The timeout (in seconds) for the periodic timer.

Public Class Methods

new(node, &blk) click to toggle source

Create a new polling even instance.

@param [ Souffle::Node ] node The node to run the polling event against. @param [ Proc ] blk The block to evaluate in the instance context.

@example

node = Souffle::Node.new
node.name = "example_node"

EM.run do
  evt = PollingEvent.new(node) do
    interval 1
    timeout 5
    pre_event     { puts "at the beginning" }
    event_loop    { puts "inside of the event loop" }
    error_handler { puts "in error handler"; EM.stop }
  end
end
# File lib/souffle/polling_event.rb, line 45
def initialize(node, &blk)
  @state = Hash.new
  @node = node
  instance_eval(&blk) if block_given?
  initialize_defaults
  initialize_state
  start_event
end

Public Instance Methods

event_complete() click to toggle source

Helper for the event block to set notify the

# File lib/souffle/polling_event.rb, line 88
def event_complete
  @event_timer.cancel
  @timeout_timer.cancel
end
start_event() click to toggle source

Begin the polling event.

# File lib/souffle/polling_event.rb, line 78
def start_event
  pre_event
  @event_timer = EM.add_periodic_timer(interval) { event_loop }
  @timeout_timer = EM::Timer.new(timeout) do
    @event_timer.cancel
    error_handler
  end
end

Private Instance Methods

initialize_defaults() click to toggle source

Initialize default values for the event.

# File lib/souffle/polling_event.rb, line 96
def initialize_defaults
  @timeout       ||= 100
  @interval      ||= 2
  @pre_event     ||= Proc.new { |state| nil }
  @event_loop    ||= Proc.new { |state| nil }
  @error_handler ||= Proc.new { |state| nil }
end
initialize_state() click to toggle source

Initialize the default values for the state of the event.

# File lib/souffle/polling_event.rb, line 105
def initialize_state
  @state[:node] = @node
  @state[:interval] = interval
  @state[:timeout] = timeout
end