From 678ff1cee915a025355b03a4f2841b8d8e8e09e4 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Tue, 20 Jul 2010 18:47:44 -0700 Subject: [PATCH] fall back to blocking behavior for em_mysql2 AR adapter if EM isn't running --- .../connection_adapters/em_mysql2_adapter.rb | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/active_record/connection_adapters/em_mysql2_adapter.rb b/lib/active_record/connection_adapters/em_mysql2_adapter.rb index 0077e13..e26c9df 100644 --- a/lib/active_record/connection_adapters/em_mysql2_adapter.rb +++ b/lib/active_record/connection_adapters/em_mysql2_adapter.rb @@ -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