diff --git a/lib/rapns/daemon.rb b/lib/rapns/daemon.rb index f789259..05a1d43 100644 --- a/lib/rapns/daemon.rb +++ b/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 diff --git a/lib/rapns/daemon/feeder.rb b/lib/rapns/daemon/feeder.rb index f92bbf3..eeb5aae 100644 --- a/lib/rapns/daemon/feeder.rb +++ b/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 diff --git a/lib/rapns/daemon/patches.rb b/lib/rapns/daemon/patches.rb new file mode 100644 index 0000000..ae0ed92 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/rapns/daemon/patches/rails/3.1.0/postgresql_adapter.rb b/lib/rapns/daemon/patches/rails/3.1.0/postgresql_adapter.rb new file mode 100644 index 0000000..fc9939a --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/rapns/daemon/patches/rails/3.1.1/postgresql_adapter.rb b/lib/rapns/daemon/patches/rails/3.1.1/postgresql_adapter.rb new file mode 100644 index 0000000..73971ca --- /dev/null +++ b/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 \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ea8ee73..390bf5f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,7 +20,7 @@ CreateRapnsNotifications.down rescue ActiveRecord::StatementInvalid CreateRapnsNotifications.up -module Rails; end + Bundler.require(:default) require 'shoulda'