Class: Souffle::PollingEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/souffle/polling_event.rb

Overview

Eventmachine polling event helper.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, &blk) ⇒ PollingEvent

Create a new polling even instance.

Examples:

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

Parameters:

  • node (Souffle::Node)

    The node to run the polling event against.

  • blk (Proc)

    The block to evaluate in the instance context.



45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#error_handlerObject

The proc to run when the timeout has occurred.



24
25
26
# File 'lib/souffle/polling_event.rb', line 24

def error_handler
  @error_handler
end

#event_loopObject

The event loop proc, should call complete on success.



21
22
23
# File 'lib/souffle/polling_event.rb', line 21

def event_loop
  @event_loop
end

#intervalObject (readonly)

The interval to run the periodic timer against the event_loop.



12
13
14
# File 'lib/souffle/polling_event.rb', line 12

def interval
  @interval
end

#nodeObject

The node to run the polling event against.



6
7
8
# File 'lib/souffle/polling_event.rb', line 6

def node
  @node
end

#pre_eventObject

The proc to run prior to the periodic event loop.



18
19
20
# File 'lib/souffle/polling_event.rb', line 18

def pre_event
  @pre_event
end

#stateObject

The current state of the polling event.



9
10
11
# File 'lib/souffle/polling_event.rb', line 9

def state
  @state
end

#timeoutObject (readonly)

The timeout (in seconds) for the periodic timer.



15
16
17
# File 'lib/souffle/polling_event.rb', line 15

def timeout
  @timeout
end

Instance Method Details

#event_completeObject

Helper for the event block to set notify the



88
89
90
91
# File 'lib/souffle/polling_event.rb', line 88

def event_complete
  @event_timer.cancel
  @timeout_timer.cancel
end

#start_eventObject

Begin the polling event.



78
79
80
81
82
83
84
85
# 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