Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

suggested chang to fix issue #20 (https://github.com/PRX/apn_on_rails/issues#issue/20) #25

Open
wants to merge 2 commits into from

3 participants

@andreasmueller

As suggested, the code now uses only one query (rather than a query per device) to get all notifications. I have tested the code only locally, so please review carefully ..

andreasmueller added some commits
@andreasmueller andreasmueller modified send_notifications_for_cert routine to decrease db load
Rather than making a query for each device, we can get all unsent
notifications for a given app with one SQL query.

The change also required a slight update of the app_spec.
e6e9b8d
@andreasmueller andreasmueller specify :select in find to avoid :read_only
The APN::Notification.find call returned read-only ActiveRecords, which
can be prevented by specifying :select.

-> see http://stackoverflow.com/questions/639171/what-is-causing-this-activerecordreadonlyrecord-error
9462d19
@stuartkhall

Please merge this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 27, 2011
  1. @andreasmueller

    modified send_notifications_for_cert routine to decrease db load

    andreasmueller authored
    Rather than making a query for each device, we can get all unsent
    notifications for a given app with one SQL query.
    
    The change also required a slight update of the app_spec.
  2. @andreasmueller

    specify :select in find to avoid :read_only

    andreasmueller authored
    The APN::Notification.find call returned read-only ActiveRecords, which
    can be prevented by specifying :select.
    
    -> see http://stackoverflow.com/questions/639171/what-is-causing-this-activerecordreadonlyrecord-error
This page is out of date. Refresh to see the latest.
View
17 lib/apn_on_rails/app/models/apn/app.rb
@@ -40,18 +40,17 @@ def self.send_notifications
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"
+ conditions = "app_id is null and sent_at is null"
else
- conditions = ["app_id = ?", app_id]
+ conditions = ["app_id = ? and sent_at is null", 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
+ notifications = APN::Notification.find(:all, :select => "apn_notifications.*", :conditions => conditions, :joins => " INNER JOIN apn_devices ON apn_devices.id = apn_notifications.device_id")
+ notifications.each do |noty|
+ conn.write(noty.message_for_sending)
+ noty.sent_at = Time.now
+ noty.save
end
end
rescue Exception => e
@@ -148,4 +147,4 @@ def log_connection_exception(ex)
puts ex.message
end
-end
+end
View
10 spec/apn_on_rails/app/models/apn/app_spec.rb
@@ -20,10 +20,7 @@
APN::App.should_receive(:all).once.and_return([app])
app.should_receive(:cert).twice.and_return(app.apn_dev_cert)
- APN::Device.should_receive(:find_each).twice.and_yield(device)
-
- device.should_receive(:unsent_notifications).and_return(notifications,[])
-
+ APN::Notification.should_receive(:find).and_return(notifications, [])
ssl_mock = mock('ssl_mock')
ssl_mock.should_receive(:write).with('message-0')
@@ -52,8 +49,7 @@
notify.should_receive(:save)
end
- APN::Device.should_receive(:find_each).and_yield(device)
- device.should_receive(:unsent_notifications).and_return(notifications)
+ APN::Notification.should_receive(:find).and_return(notifications)
ssl_mock = mock('ssl_mock')
ssl_mock.should_receive(:write).with('message-0')
@@ -227,4 +223,4 @@
end
end
-end
+end
Something went wrong with that request. Please try again.