Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Patch PostgreSQLAdapter in Rails 3.1.0 and 3.1.1 to fix a reconnectio…

…n issue.
  • Loading branch information...
commit d2385fe02b68c37a49793d9fd48ca4c40991d07b 1 parent c87e8a3
@ileitch ileitch authored
View
30 lib/rapns/daemon.rb
@@ -2,17 +2,19 @@
require 'socket'
require 'pathname'
-require "rapns/daemon/configuration"
-require "rapns/daemon/certificate"
-require "rapns/daemon/delivery_error"
-require "rapns/daemon/pool"
-require "rapns/daemon/connection_pool"
-require "rapns/daemon/connection"
-require "rapns/daemon/delivery_queue"
-require "rapns/daemon/delivery_handler"
-require "rapns/daemon/delivery_handler_pool"
-require "rapns/daemon/feeder"
-require "rapns/daemon/logger"
+require 'rapns/daemon/configuration'
+require 'rapns/daemon/certificate'
+require 'rapns/daemon/delivery_error'
+require 'rapns/daemon/pool'
+require 'rapns/daemon/connection_pool'
+require 'rapns/daemon/connection'
+require 'rapns/daemon/delivery_queue'
+require 'rapns/daemon/delivery_handler'
+require 'rapns/daemon/delivery_handler_pool'
+require 'rapns/daemon/feeder'
+require 'rapns/daemon/logger'
+
+require 'rapns/daemon/patches'
module Rapns
module Daemon
@@ -26,7 +28,7 @@ def self.start(environment, foreground)
@foreground = foreground
setup_signal_hooks
- self.configuration = Configuration.new(environment, File.join(Rails.root, "config", "rapns", "rapns.yml"))
+ self.configuration = Configuration.new(environment, File.join(Rails.root, 'config', 'rapns', 'rapns.yml'))
configuration.load
self.logger = Logger.new(:foreground => foreground, :airbrake_notify => configuration.airbrake_notify)
@@ -57,7 +59,7 @@ def self.start(environment, foreground)
def self.setup_signal_hooks
@shutting_down = false
- ["SIGINT", "SIGTERM"].each do |signal|
+ ['SIGINT', 'SIGTERM'].each do |signal|
Signal.trap(signal) do
handle_shutdown_signal
end
@@ -93,7 +95,7 @@ def self.daemonize
def self.write_pid_file
if !configuration.pid_file.blank?
- File.open(configuration.pid_file, "w") do |f|
+ File.open(configuration.pid_file, 'w') do |f|
f.puts $$
end
end
View
4 lib/rapns/daemon/feeder.rb
@@ -23,9 +23,7 @@ def self.enqueue_notifications
Rapns::Daemon.delivery_queue.wait_until_empty
rescue ActiveRecord::StatementInvalid, *ADAPTER_ERRORS => e
Rapns::Daemon.logger.error(e)
- Rapns::Daemon.logger.warn('Lost connection to database, reconnecting...')
reconnect
- Rapns::Daemon.logger.warn('Database reconnected.')
rescue StandardError => e
Rapns::Daemon.logger.error(e)
end
@@ -38,6 +36,7 @@ def self.stop
end
def self.reconnect
+ Rapns::Daemon.logger.warn('Lost connection to database, reconnecting...')
attempts = 0
loop do
begin
@@ -51,6 +50,7 @@ def self.reconnect
sleep 2 # Avoid thrashing.
end
end
+ Rapns::Daemon.logger.warn('Database reconnected.')
end
end
end
View
6 lib/rapns/daemon/patches.rb
@@ -0,0 +1,6 @@
+if defined?(Rails) && ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
+ if Rails::VERSION::STRING == '3.1.0' || Rails::VERSION::STRING == '3.1.1'
+ STDERR.puts '[WARNING] Patched PostgreSQLAdapter to fix reconnection bug: https://github.com/rails/rails/issues/3160.'
+ require "rapns/daemon/patches/rails/#{Rails::VERSION::STRING}/postgresql_adapter.rb"
+ end
+end
View
12 lib/rapns/daemon/patches/rails/3.1.0/postgresql_adapter.rb
@@ -0,0 +1,12 @@
+module ActiveRecord
+ module ConnectionAdapters
+ class PostgreSQLAdapter < AbstractAdapter
+ def clear_cache!
+ @statements.each_value do |value|
+ @connection.query "DEALLOCATE #{value}" if active?
+ end
+ @statements.clear
+ end
+ end
+ end
+end
View
17 lib/rapns/daemon/patches/rails/3.1.1/postgresql_adapter.rb
@@ -0,0 +1,17 @@
+module ActiveRecord
+ module ConnectionAdapters
+ class PostgreSQLAdapter < AbstractAdapter
+ class StatementPool < ConnectionAdapters::StatementPool
+ def dealloc(key)
+ @connection.query "DEALLOCATE #{key}" if connection_active?
+ end
+
+ def connection_active?
+ @connection.status == PGconn::CONNECTION_OK
+ rescue PGError
+ false
+ end
+ end
+ end
+ end
+end
View
2  spec/spec_helper.rb
@@ -20,7 +20,7 @@
CreateRapnsNotifications.down rescue ActiveRecord::StatementInvalid
CreateRapnsNotifications.up
-module Rails; end
+
Bundler.require(:default)
require 'shoulda'
Please sign in to comment.
Something went wrong with that request. Please try again.