Class: Combi::Reactor

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

Class Method Summary collapse

Class Method Details

.join_threadObject



37
38
39
# File 'lib/combi/reactor.rb', line 37

def self.join_thread
  @@reactor_thread.join if @@reactor_thread
end

.log(message) ⇒ Object



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

def self.log(message)
  return unless @debug_mode ||= ENV['DEBUG'] == 'true'
  puts "#{object_id} #{self.class.name} #{message}"
end

.start(&block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/combi/reactor.rb', line 5

def self.start(&block)
  EM::error_handler do |error|
    STDERR << "ERROR IN EM\n"
    STDERR << "\t#{error.inspect}"
    STDERR << "\t#{error.backtrace.join("\t\n")}" << "\n"
  end
  log "-EM.start- the reactor is running: #{EM::reactor_running?}"
  raise "EM did not shut down" if EM::reactor_running?
  @@reactor_thread = Thread.new do
    begin
      log "------- starting EM reactor"
      EM::run do
        log "------- reactor started"
        block.call unless block.nil?
      end
    ensure
      puts "------- reactor stopped"
    end
  end
  30.times do
    sleep 0.1 unless EM::reactor_running?
  end
end

.stopObject



29
30
31
32
33
34
35
# File 'lib/combi/reactor.rb', line 29

def self.stop
  log "-EM.stop- the reactor is running: #{EM::reactor_running?}"
  EM::stop_event_loop if EM::reactor_running?
  50.times do
    sleep 0.3 if EM::reactor_running?
  end
end