Commit 294dfe15ca4002ff2c6c6f2e83e59e042ac973ba

Authored by jcredding
1 parent b5fdacd1

added connection fix initializer, helps long running resque workers not fail

Showing 1 changed file with 19 additions and 2 deletions   Show diff stats
config/initializers/connection_fix.rb
  1 +# from http://gist.github.com/238999
  2 +#
  3 +# If your workers are inactive for a long period of time, they'll lose
  4 +# their MySQL connection.
  5 +#
  6 +# This hack ensures we re-connect whenever a connection is
  7 +# lost. Because, really. why not?
  8 +#
  9 +# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
  10 +#
  11 +# From:
  12 +# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
  13 +
1 14 if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
  15 +
2 16 module ActiveRecord::ConnectionAdapters
  17 +
3 18 class Mysql2Adapter
4 19 alias_method :execute_without_retry, :execute
5 20  
6 21 def execute(*args)
7 22 execute_without_retry(*args)
8   - rescue Mysql2::Error => e
  23 + rescue ActiveRecord::StatementInvalid => e
9 24 if e.message =~ /server has gone away/i
10 25 warn "Server timed out, retrying"
11 26 reconnect!
... ... @@ -15,5 +30,7 @@ if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
15 30 end
16 31 end
17 32 end
  33 +
18 34 end
19   -end
20 35 \ No newline at end of file
  36 +
  37 +end
... ...