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 if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) 14 if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
  15 +
2 module ActiveRecord::ConnectionAdapters 16 module ActiveRecord::ConnectionAdapters
  17 +
3 class Mysql2Adapter 18 class Mysql2Adapter
4 alias_method :execute_without_retry, :execute 19 alias_method :execute_without_retry, :execute
5 20
6 def execute(*args) 21 def execute(*args)
7 execute_without_retry(*args) 22 execute_without_retry(*args)
8 - rescue Mysql2::Error => e 23 + rescue ActiveRecord::StatementInvalid => e
9 if e.message =~ /server has gone away/i 24 if e.message =~ /server has gone away/i
10 warn "Server timed out, retrying" 25 warn "Server timed out, retrying"
11 reconnect! 26 reconnect!
@@ -15,5 +30,7 @@ if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) @@ -15,5 +30,7 @@ if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
15 end 30 end
16 end 31 end
17 end 32 end
  33 +
18 end 34 end
19 -end  
20 \ No newline at end of file 35 \ No newline at end of file
  36 +
  37 +end