Skip to content
Browse files

reduced batch size due to broken pipe and other connection errors

  • Loading branch information...
1 parent 5327611 commit 742e59c58710fd477d3c1a34e87b80cc1a77fc32 @ramigg committed
Showing with 57 additions and 44 deletions.
  1. +1 −0 .gitignore
  2. +55 −43 lib/apn_on_rails/app/models/apn/app.rb
  3. +1 −1 lib/apn_on_rails/libs/connection.rb
View
1 .gitignore
@@ -15,3 +15,4 @@ profile
spec/active_record/test.db
.bundle
**/.svn
+.idea
View
98 lib/apn_on_rails/app/models/apn/app.rb
@@ -1,16 +1,16 @@
class APN::App < APN::Base
-
+
has_many :groups, :class_name => 'APN::Group', :dependent => :destroy
has_many :devices, :class_name => 'APN::Device', :dependent => :destroy
has_many :notifications, :through => :devices, :dependent => :destroy
has_many :unsent_notifications, :through => :devices
has_many :group_notifications, :through => :groups
has_many :unsent_group_notifications, :through => :groups
-
+
def cert
(RAILS_ENV == 'production' ? apn_prod_cert : apn_dev_cert)
end
-
+
# Opens a connection to the Apple APN server and attempts to batch deliver
# an Array of group notifications.
#
@@ -25,9 +25,9 @@ def send_notifications
end
APN::App.send_notifications_for_cert(self.cert, self.id)
end
-
+
def self.send_notifications
- apps = APN::App.all
+ apps = APN::App.all
apps.each do |app|
app.send_notifications
end
@@ -36,50 +36,60 @@ def self.send_notifications
send_notifications_for_cert(global_cert, nil)
end
end
-
+
def self.send_notifications_for_cert(the_cert, app_id)
# unless self.unsent_notifications.nil? || self.unsent_notifications.empty?
- if (app_id == nil)
- conditions = "app_id is null"
- else
- conditions = ["app_id = ?", app_id]
- end
- begin
- APN::Connection.open_for_delivery({:cert => the_cert}) do |conn, sock|
- APN::Device.find_each(:conditions => conditions) do |dev|
- dev.unsent_notifications.each do |noty|
- conn.write(noty.message_for_sending)
- noty.sent_at = Time.now
- noty.save
- end
+ if (app_id == nil)
+ conditions = "app_id is null"
+ else
+ conditions = ["app_id = ?", app_id]
+ end
+ begin
+ APN::Connection.open_for_delivery({:cert => the_cert}) do |conn, sock|
+ APN::Device.find_each(:conditions => conditions) do |dev|
+ dev.unsent_notifications.each do |noty|
+ conn.write(noty.message_for_sending)
+ noty.sent_at = Time.now
+ noty.save
end
end
- rescue Exception => e
- log_connection_exception(e)
end
+ rescue Exception => e
+ log_connection_exception(e)
+ end
# end
end
-
+
def send_group_notifications
- if self.cert.nil?
+ if self.cert.nil?
raise APN::Errors::MissingCertificateError.new
return
end
- unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty?
- APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock|
- unsent_group_notifications.each do |gnoty|
- gnoty.devices.find_each do |device|
- conn.write(gnoty.message_for_sending(device))
+ unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty?
+ unsent_group_notifications.each do |gnoty|
+ failed = 0
+ devices_to_send = gnoty.devices.count
+ gnoty.devices.find_in_batches(:batch_size => 100) do |devices|
+ APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock|
+ devices.each do |device|
+ begin
+ conn.write(gnoty.message_for_sending(device))
+ rescue Exception => e
+ puts e.message
+ failed += 1
+ end
+ end
end
- gnoty.sent_at = Time.now
- gnoty.save
end
+ puts "Sent to: #{devices_to_send - failed}/#{devices_to_send} "
+ gnoty.sent_at = Time.now
+ gnoty.save
end
end
end
-
+
def send_group_notification(gnoty)
- if self.cert.nil?
+ if self.cert.nil?
raise APN::Errors::MissingCertificateError.new
return
end
@@ -93,14 +103,14 @@ def send_group_notification(gnoty)
end
end
end
-
+
def self.send_group_notifications
apps = APN::App.all
apps.each do |app|
app.send_group_notifications
end
- end
-
+ end
+
# Retrieves a list of APN::Device instnces from Apple using
# the <tt>devices</tt> method. It then checks to see if the
# <tt>last_registered_at</tt> date of each APN::Device is
@@ -117,8 +127,10 @@ def process_devices
return
end
APN::App.process_devices_for_cert(self.cert)
- end # process_devices
-
+ end
+
+ # process_devices
+
def self.process_devices
apps = APN::App.all
apps.each do |app|
@@ -129,23 +141,23 @@ def self.process_devices
APN::App.process_devices_for_cert(global_cert)
end
end
-
+
def self.process_devices_for_cert(the_cert)
puts "in APN::App.process_devices_for_cert"
APN::Feedback.devices(the_cert).each do |device|
if device.last_registered_at < device.feedback_at
puts "device #{device.id} -> #{device.last_registered_at} < #{device.feedback_at}"
device.destroy
- else
+ else
puts "device #{device.id} -> #{device.last_registered_at} not < #{device.feedback_at}"
end
- end
+ end
end
-
-
+
+
protected
def log_connection_exception(ex)
puts ex.message
end
-
+
end
View
2 lib/apn_on_rails/libs/connection.rb
@@ -59,7 +59,7 @@ def open(options = {}, &block) # :nodoc:
ssl.connect
yield ssl, sock if block_given?
-
+ ensure
ssl.close
sock.close
end

0 comments on commit 742e59c

Please sign in to comment.
Something went wrong with that request. Please try again.