fall back to blocking behavior for em_mysql2 AR adapter if EM isn't running

This commit is contained in:
Brian Lopez 2010-07-20 18:47:44 -07:00
parent 3ec92096ca
commit 678ff1cee9
1 changed files with 14 additions and 10 deletions

View File

@ -42,17 +42,21 @@ module Mysql2
end
def query(sql, opts={})
super(sql, opts.merge(:async => true))
deferable = ::EM::DefaultDeferrable.new
::EM.watch(self.socket, Watcher, self, deferable).notify_readable = true
fiber = Fiber.current
deferable.callback do |result|
fiber.resume(result)
if EM.reactor_running?
super(sql, opts.merge(:async => true))
deferable = ::EM::DefaultDeferrable.new
::EM.watch(self.socket, Watcher, self, deferable).notify_readable = true
fiber = Fiber.current
deferable.callback do |result|
fiber.resume(result)
end
deferable.errback do |err|
fiber.resume(err)
end
Fiber.yield
else
super(sql, opts)
end
deferable.errback do |err|
fiber.resume(err)
end
Fiber.yield
end
end
end