Module: EventMachineSpawn

Defined in:
lib/right_agent/eventmachine_spawn.rb

Overview

Wrap EventMachine to support automatically spawning of fiber before executing associated block so that if block yields its fiber it is not the root fiber

Defined Under Namespace

Classes: PeriodicTimer, Timer

Class Method Summary collapse

Class Method Details

.add_periodic_timer(*args, &block) ⇒ Object



62
63
64
# File 'lib/right_agent/eventmachine_spawn.rb', line 62

def self.add_periodic_timer(*args, &block)
  EM.add_periodic_timer(*args) { @fiber_pool ? @fiber_pool.spawn(&block) : yield }
end

.add_timer(*args, &block) ⇒ Object



58
59
60
# File 'lib/right_agent/eventmachine_spawn.rb', line 58

def self.add_timer(*args, &block)
  EM.add_timer(*args) { @fiber_pool ? @fiber_pool.spawn(&block) : yield }
end

.execute(&block) ⇒ Object



36
37
38
# File 'lib/right_agent/eventmachine_spawn.rb', line 36

def self.execute(&block)
  @fiber_pool ? @fiber_pool.spawn(&block) : yield
end

.fiber_poolObject



28
29
30
# File 'lib/right_agent/eventmachine_spawn.rb', line 28

def self.fiber_pool
  @fiber_pool
end

.fiber_pool=(value) ⇒ Object



32
33
34
# File 'lib/right_agent/eventmachine_spawn.rb', line 32

def self.fiber_pool=(value)
  @fiber_pool = value
end

.next_tick(*args, &block) ⇒ Object



44
45
46
# File 'lib/right_agent/eventmachine_spawn.rb', line 44

def self.next_tick(*args, &block)
  EM.next_tick(*args) { @fiber_pool ? @fiber_pool.spawn(&block) : yield }
end

.run(*args, &block) ⇒ Object



40
41
42
# File 'lib/right_agent/eventmachine_spawn.rb', line 40

def self.run(*args, &block)
  EM.run(*args) { @fiber_pool ? @fiber_pool.spawn(&block) : yield }
end

.wait(seconds) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/right_agent/eventmachine_spawn.rb', line 48

def self.wait(seconds)
  if @fiber_pool
    fiber = Fiber.current
    EM.add_timer(seconds) { fiber.resume }
    Fiber.yield
  else
    sleep(seconds)
  end
end