Class: Goru::Reactor

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

Overview

public

Constant Summary collapse

STATUS_RUNNING =
public
:running
STATUS_FINISHED =
public
:finished
STATUS_IDLE =
public
:idle
STATUS_STOPPED =
public
:stopped

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


42
43
44
# File 'lib/goru/reactor.rb', line 42

def status
  @status
end

Instance Method Details

#adopt_routine(routine) ⇒ Object

public


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

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

#asleep_for(seconds) ⇒ Object

public


145
146
147
148
149
# File 'lib/goru/reactor.rb', line 145

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

#deregister(routine) ⇒ Object

public


171
172
173
# File 'lib/goru/reactor.rb', line 171

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

#finished?Boolean

public

Returns:

  • (Boolean)


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

def finished?
  @status == STATUS_IDLE || @status == STATUS_STOPPED
end

#register(routine) ⇒ Object

public


165
166
167
# File 'lib/goru/reactor.rb', line 165

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

#routine_finished(routine) ⇒ Object

public


159
160
161
# File 'lib/goru/reactor.rb', line 159

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

#runObject

public


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/goru/reactor.rb', line 46

def run
  set_status(STATUS_RUNNING)

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

#stopObject

public


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

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

#wakeupObject

public


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

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