From dac2f02c9b6cfb7362d21373a1ed65ec5fcfb2bd Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Fri, 26 Nov 2010 22:42:34 -0500 Subject: [PATCH] if no reactor use synch query --- lib/mysql2/em.rb | 12 ++++++++---- lib/mysql2/em_fiber.rb | 23 ++++++++++++++--------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/mysql2/em.rb b/lib/mysql2/em.rb index 98da1e6..bfdde70 100644 --- a/lib/mysql2/em.rb +++ b/lib/mysql2/em.rb @@ -23,10 +23,14 @@ 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 - deferable + if ::EM.reactor_running? + super(sql, opts.merge(:async => true)) + deferable = ::EM::DefaultDeferrable.new + ::EM.watch(self.socket, Watcher, self, deferable).notify_readable = true + deferable + else + super(sql, opts) + end end end end diff --git a/lib/mysql2/em_fiber.rb b/lib/mysql2/em_fiber.rb index 72b84f0..7adb42f 100644 --- a/lib/mysql2/em_fiber.rb +++ b/lib/mysql2/em_fiber.rb @@ -1,23 +1,28 @@ # encoding: utf-8 require 'mysql2/em' -require 'fiber' unless defined? Fiber +require 'fiber' module Mysql2 module EM module Fiber class Client < ::Mysql2::EM::Client def query(sql, opts={}) - deferable = super(sql, opts) + if ::EM.reactor_running? + deferable = super(sql, opts) - fiber = ::Fiber.current - deferable.callback do |result| - fiber.resume(result) + fiber = ::Fiber.current + deferable.callback do |result| + fiber.resume(result) + end + deferable.errback do |err| + p 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