* a fix for race condition that can hang eventmachine startup

This commit is contained in:
Seth Call 2014-04-30 03:03:37 +00:00
parent c0834c92c7
commit 6463512420
1 changed files with 14 additions and 5 deletions

View File

@ -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