Skip to content

Commit

Permalink
Add global error handling support.
Browse files Browse the repository at this point in the history
  • Loading branch information
radsaq committed Apr 6, 2011
1 parent ea98b9a commit 5773322
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/em_mysql2_connection_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
class EmMysql2ConnectionPool

class Query
def initialize(sql, opts, deferrable)
@sql, @opts, @deferrable = sql, opts, deferrable
def initialize(sql, opts, deferrable, on_error = nil)
@sql, @opts, @deferrable, @on_error = sql, opts, deferrable, on_error
end

def sql(connection)
Expand All @@ -14,10 +14,13 @@ def sql(connection)

def execute(connection, &block)
@busy = true
q = connection.query sql(connection), @opts
query_text = sql(connection)
q = connection.query query_text, @opts
q.callback{ |result| succeed result, connection.affected_rows, &block }
q.errback{ |error| fail error, &block }
q.errback{ |error| fail error, query_text, &block }
return q
rescue Exception => e
do_error(e, query_text)
end

def succeed(result, affected_rows, &block)
Expand All @@ -26,18 +29,25 @@ def succeed(result, affected_rows, &block)
@busy and block.call
@busy = false
end
def fail(error, &block)
def fail(error, sql, &block)
@deferrable.fail error
ensure
do_error(error, sql)
@busy and block.call
@busy = false
end

def do_error(e, sql)
if @on_error.respond_to?(:call)
@on_error.call(e, sql)
end
end
end

def initialize(conf)
@pool_size = conf[:size] || 10
@query_queue = EM::Queue.new
@on_error = conf[:on_error]
start_queue conf
end

Expand All @@ -62,7 +72,7 @@ def start_queue(conf)
def query(sql, opts={})
deferrable = EM::DefaultDeferrable.new
deferrable.callback{ |result,affected_rows| yield result, affected_rows } if block_given?
@query_queue.push Query.new(sql, opts, deferrable)
@query_queue.push Query.new(sql, opts, deferrable, @on_error)
deferrable
end
end

4 comments on commit 5773322

@niko
Copy link

@niko niko commented on 5773322 Apr 8, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started implementing a global errback.

Also I'm thinking about calling a predefined errback that just puts the error if no global and no local errback is defined. That should make finding errors easier, especially for ppl not used to the whole callback/errback thing. What do you think?

@radsaq
Copy link
Owner Author

@radsaq radsaq commented on 5773322 Apr 8, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit leery about things that puts, but if it only does that without any user-defined error handler at all, that seems reasonable.

@niko
Copy link

@niko niko commented on 5773322 Apr 8, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right with your opinion to puts… I just thought: Not handling errors at all is mistake and users doing this could use the help of some additional puts if/when errors actually occur. Perhaps I'll just add it and see if people complain. :)

@radsaq
Copy link
Owner Author

@radsaq radsaq commented on 5773322 Apr 8, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me.

Please sign in to comment.