diff --git a/ruby/lib/jam_ruby/lib/em_helper.rb b/ruby/lib/jam_ruby/lib/em_helper.rb index 3e290c47f..592f97e7b 100644 --- a/ruby/lib/jam_ruby/lib/em_helper.rb +++ b/ruby/lib/jam_ruby/lib/em_helper.rb @@ -12,7 +12,7 @@ require 'jam_ruby' module JamWebEventMachine @@log = Logging.logger[JamWebEventMachine] - + # starts amqp & eventmachine up first. # and then calls your block. # After the supplied block is done, @@ -36,8 +36,9 @@ module JamWebEventMachine end - def self.run_em(calling_thread = nil) + def self.run_em(calling_thread = nil, semaphore = nil, ran = {}) + semaphore.lock if semaphore EM.run do # this is global because we need to check elsewhere if we are currently connected to amqp before signalling success with some APIs, such as 'create session' $amqp_connection_manager = AmqpConnectionManager.new(true, 4, :host => APP_CONFIG.rabbitmq_host, :port => APP_CONFIG.rabbitmq_port) @@ -54,6 +55,8 @@ module JamWebEventMachine end end + ran[:ran] = true + semaphore.unlock if semaphore calling_thread.wakeup if calling_thread end end @@ -65,12 +68,18 @@ module JamWebEventMachine end def self.run - return if defined?(Rails::Console) + + ran = {} + semaphore = Mutex.new current = Thread.current Thread.new do - run_em(current) + run_em(current, semaphore, ran) end - Thread.stop + semaphore.synchronize { + unless ran[:ran] + semaphore.sleep(10) + end + } end def self.start