Class: Goru::Reactor

Inherits:
Object
  • Object
show all
Defined in:
lib/goru/reactor.rb

Overview

public

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue:, scheduler:) ⇒ Reactor

Returns a new instance of Reactor.



13
14
15
16
17
18
19
20
21
22
# File 'lib/goru/reactor.rb', line 13

def initialize(queue:, scheduler:)
  @queue = queue
  @scheduler = scheduler
  @routines = Set.new
  @timers = Timers::Group.new
  @stopped = false
  @status = nil
  @selector = NIO::Selector.new
  @commands = []
end

Instance Attribute Details

#statusObject (readonly)

public


26
27
28
# File 'lib/goru/reactor.rb', line 26

def status
  @status
end

Instance Method Details

#adopt_routine(routine) ⇒ Object

public


136
137
138
# File 'lib/goru/reactor.rb', line 136

def adopt_routine(routine)
  command(:adopt, routine)
end

#asleep_for(seconds) ⇒ Object

public


128
129
130
131
132
# File 'lib/goru/reactor.rb', line 128

def asleep_for(seconds)
  @timers.after(seconds) {
    yield
  }
end

#deregister(routine) ⇒ Object

public


154
155
156
# File 'lib/goru/reactor.rb', line 154

def deregister(routine)
  command(:deregister, routine)
end

#finished?Boolean

public

Returns:

  • (Boolean)


105
106
107
# File 'lib/goru/reactor.rb', line 105

def finished?
  @status == :idle || @status == :stopped
end

#register(routine) ⇒ Object

public


148
149
150
# File 'lib/goru/reactor.rb', line 148

def register(routine)
  command(:register, routine)
end

#routine_finished(routine) ⇒ Object

public


142
143
144
# File 'lib/goru/reactor.rb', line 142

def routine_finished(routine)
  command(:cleanup, routine)
end

#runObject

public


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/goru/reactor.rb', line 30

def run
  set_status(:running)

  until @stopped
    tick
  end
ensure
  @timers.cancel
  @selector.close
  set_status(:finished)
end

#stopObject

public


119
120
121
122
123
124
# File 'lib/goru/reactor.rb', line 119

def stop
  @stopped = true
  wakeup
rescue ClosedQueueError
  # nothing to do
end

#wakeupObject

public


111
112
113
114
115
# File 'lib/goru/reactor.rb', line 111

def wakeup
  @selector.wakeup
rescue IOError
  # nothing to do
end