Skip to content

Commit

Permalink
handle error event on log_connection_exception should be a class method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jirapong committed Apr 28, 2011
1 parent e364b6f commit b9a10f1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
24 changes: 13 additions & 11 deletions generators/apn_migrations_generator.rb
@@ -1,31 +1,33 @@
require 'rails_generator' require 'rails_generator'
require 'rails/generators/migration'
# Generates the migrations necessary for APN on Rails. # Generates the migrations necessary for APN on Rails.
# This should be run upon install and upgrade of the # This should be run upon install and upgrade of the
# APN on Rails gem. # APN on Rails gem.
# #
# $ ruby script/generate apn_migrations # $ ruby script/generate apn_migrations
class ApnMigrationsGenerator < Rails::Generator::Base class ApnMigrationsGenerator < Rails::Generator::Base

desc "Generates the migrations necessary for APN on Rails."

def manifest # :nodoc: def manifest # :nodoc:
record do |m| record do |m|
timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S") timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
db_migrate_path = File.join('db', 'migrate') db_migrate_path = File.join('db', 'migrate')

m.directory(db_migrate_path) m.directory(db_migrate_path)

Dir.glob(File.join(File.dirname(__FILE__), 'templates', 'apn_migrations', '*.rb')).sort.each_with_index do |f, i| Dir.glob(File.join(File.dirname(__FILE__), 'templates', 'apn_migrations', '*.rb')).sort.each_with_index do |f, i|
f = File.basename(f) f = File.basename(f)
f.match(/\d+\_(.+)/) f.match(/\d+\_(.+)/)
timestamp = timestamp.succ timestamp = timestamp.succ
if Dir.glob(File.join(db_migrate_path, "*_#{$1}")).empty? if Dir.glob(File.join(db_migrate_path, "*_#{$1}")).empty?
m.file(File.join('apn_migrations', f), m.file(File.join('apn_migrations', f),
File.join(db_migrate_path, "#{timestamp}_#{$1}"), File.join(db_migrate_path, "#{timestamp}_#{$1}"),
{:collision => :skip}) {:collision => :skip})
end end
end end

end # record end # record

end # manifest end # manifest

end # ApnMigrationsGenerator end # ApnMigrationsGenerator
59 changes: 30 additions & 29 deletions lib/apn_on_rails/app/models/apn/app.rb
@@ -1,33 +1,33 @@
class APN::App < APN::Base class APN::App < APN::Base

has_many :groups, :class_name => 'APN::Group', :dependent => :destroy has_many :groups, :class_name => 'APN::Group', :dependent => :destroy
has_many :devices, :class_name => 'APN::Device', :dependent => :destroy has_many :devices, :class_name => 'APN::Device', :dependent => :destroy
has_many :notifications, :through => :devices, :dependent => :destroy has_many :notifications, :through => :devices, :dependent => :destroy
has_many :unsent_notifications, :through => :devices has_many :unsent_notifications, :through => :devices
has_many :group_notifications, :through => :groups has_many :group_notifications, :through => :groups
has_many :unsent_group_notifications, :through => :groups has_many :unsent_group_notifications, :through => :groups

def cert def cert
(RAILS_ENV == 'production' ? apn_prod_cert : apn_dev_cert) (RAILS_ENV == 'production' ? apn_prod_cert : apn_dev_cert)
end end

# Opens a connection to the Apple APN server and attempts to batch deliver # Opens a connection to the Apple APN server and attempts to batch deliver
# an Array of group notifications. # an Array of group notifications.
# #
# #
# As each APN::GroupNotification is sent the <tt>sent_at</tt> column will be timestamped, # As each APN::GroupNotification is sent the <tt>sent_at</tt> column will be timestamped,
# so as to not be sent again. # so as to not be sent again.
# #
def send_notifications def send_notifications
if self.cert.nil? if self.cert.nil?
raise APN::Errors::MissingCertificateError.new raise APN::Errors::MissingCertificateError.new
return return
end end
APN::App.send_notifications_for_cert(self.cert, self.id) APN::App.send_notifications_for_cert(self.cert, self.id)
end end

def self.send_notifications def self.send_notifications
apps = APN::App.all apps = APN::App.all
apps.each do |app| apps.each do |app|
app.send_notifications app.send_notifications
end end
Expand All @@ -36,12 +36,12 @@ def self.send_notifications
send_notifications_for_cert(global_cert, nil) send_notifications_for_cert(global_cert, nil)
end end
end end

def self.send_notifications_for_cert(the_cert, app_id) def self.send_notifications_for_cert(the_cert, app_id)
# unless self.unsent_notifications.nil? || self.unsent_notifications.empty? # unless self.unsent_notifications.nil? || self.unsent_notifications.empty?
if (app_id == nil) if (app_id == nil)
conditions = "app_id is null" conditions = "app_id is null"
else else
conditions = ["app_id = ?", app_id] conditions = ["app_id = ?", app_id]
end end
begin begin
Expand All @@ -57,15 +57,15 @@ def self.send_notifications_for_cert(the_cert, app_id)
rescue Exception => e rescue Exception => e
log_connection_exception(e) log_connection_exception(e)
end end
# end # end
end end

def send_group_notifications def send_group_notifications
if self.cert.nil? if self.cert.nil?
raise APN::Errors::MissingCertificateError.new raise APN::Errors::MissingCertificateError.new
return return
end end
unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty? unless self.unsent_group_notifications.nil? || self.unsent_group_notifications.empty?
APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock| APN::Connection.open_for_delivery({:cert => self.cert}) do |conn, sock|
unsent_group_notifications.each do |gnoty| unsent_group_notifications.each do |gnoty|
gnoty.devices.find_each do |device| gnoty.devices.find_each do |device|
Expand All @@ -77,9 +77,9 @@ def send_group_notifications
end end
end end
end end

def send_group_notification(gnoty) def send_group_notification(gnoty)
if self.cert.nil? if self.cert.nil?
raise APN::Errors::MissingCertificateError.new raise APN::Errors::MissingCertificateError.new
return return
end end
Expand All @@ -93,22 +93,22 @@ def send_group_notification(gnoty)
end end
end end
end end

def self.send_group_notifications def self.send_group_notifications
apps = APN::App.all apps = APN::App.all
apps.each do |app| apps.each do |app|
app.send_group_notifications app.send_group_notifications
end end
end end

# Retrieves a list of APN::Device instnces from Apple using # Retrieves a list of APN::Device instnces from Apple using
# the <tt>devices</tt> method. It then checks to see if the # the <tt>devices</tt> method. It then checks to see if the
# <tt>last_registered_at</tt> date of each APN::Device is # <tt>last_registered_at</tt> date of each APN::Device is
# before the date that Apple says the device is no longer # before the date that Apple says the device is no longer
# accepting notifications then the device is deleted. Otherwise # accepting notifications then the device is deleted. Otherwise
# it is assumed that the application has been re-installed # it is assumed that the application has been re-installed
# and is available for notifications. # and is available for notifications.
# #
# This can be run from the following Rake task: # This can be run from the following Rake task:
# $ rake apn:feedback:process # $ rake apn:feedback:process
def process_devices def process_devices
Expand All @@ -118,7 +118,7 @@ def process_devices
end end
APN::App.process_devices_for_cert(self.cert) APN::App.process_devices_for_cert(self.cert)
end # process_devices end # process_devices

def self.process_devices def self.process_devices
apps = APN::App.all apps = APN::App.all
apps.each do |app| apps.each do |app|
Expand All @@ -129,23 +129,24 @@ def self.process_devices
APN::App.process_devices_for_cert(global_cert) APN::App.process_devices_for_cert(global_cert)
end end
end end

def self.process_devices_for_cert(the_cert) def self.process_devices_for_cert(the_cert)
puts "in APN::App.process_devices_for_cert" puts "in APN::App.process_devices_for_cert"
APN::Feedback.devices(the_cert).each do |device| APN::Feedback.devices(the_cert).each do |device|
if device.last_registered_at < device.feedback_at if device.last_registered_at < device.feedback_at
puts "device #{device.id} -> #{device.last_registered_at} < #{device.feedback_at}" puts "device #{device.id} -> #{device.last_registered_at} < #{device.feedback_at}"
device.destroy device.destroy
else else
puts "device #{device.id} -> #{device.last_registered_at} not < #{device.feedback_at}" puts "device #{device.id} -> #{device.last_registered_at} not < #{device.feedback_at}"
end end
end end
end end


protected protected
def log_connection_exception(ex) def log_connection_exception(ex)
puts ex.message STDERR.puts ex.message
raise ex
end end

end end

0 comments on commit b9a10f1

Please sign in to comment.