Commit 3a2d7a6604372ce1ab98982709c9d851376879da

Authored by Dmitriy Zaporozhets
2 parents 04bf40a9 294dfe15

Merge pull request #917 from teaminsight/queue_fix

Connection fix for resque worker
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