Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Rapns Daemon wakeup: Refactor name of config variable... #165

Merged
merged 2 commits into from

2 participants

@mattconnolly

Rapns Daemon wakeup: Refactor name of config variable, and allow specification of separate bind/connect addresses.

Followup from #164

@ileitch ileitch merged commit e36cb5f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 16, 2013
  1. @mattconnolly
  2. @mattconnolly

    Rapns Daemon wakeup: Refactor name of config variable, and allow spec…

    mattconnolly authored
    …ification of separate bind/connect addresses
This page is out of date. Refresh to see the latest.
View
2  config/database.yml
@@ -15,7 +15,7 @@ jdbcpostgresql:
password: ""
mysql2:
- adapter: mysql
+ adapter: mysql2
database: rapns_test
host: localhost
username: rapns_test
View
1  lib/rapns.rb
@@ -21,6 +21,7 @@ def self.attr_accessible_available?
require 'rapns/push'
require 'rapns/apns_feedback'
require 'rapns/upgraded'
+require 'rapns/notifier'
require 'rapns/apns/binary_notification_validator'
require 'rapns/apns/device_token_format_validator'
View
2  lib/rapns/configuration.rb
@@ -9,7 +9,7 @@ def self.configure
CONFIG_ATTRS = [:foreground, :push_poll, :feedback_poll, :embedded,
:airbrake_notify, :check_for_errors, :pid_file, :batch_size,
- :push, :store, :logger, :batch_storage_updates, :udp_wake_host, :udp_wake_port]
+ :push, :store, :logger, :batch_storage_updates, :wakeup]
class ConfigurationWithoutDefaults < Struct.new(*CONFIG_ATTRS)
end
View
4 lib/rapns/daemon/feeder.rb
@@ -53,8 +53,8 @@ def self.enqueue_notifications
def self.interruptible_sleeper
unless @interruptible_sleeper
@interruptible_sleeper = InterruptibleSleep.new
- if Rapns.config.udp_wake_host && Rapns.config.udp_wake_port
- @interruptible_sleeper.enable_wake_on_udp Rapns.config.udp_wake_host, Rapns.config.udp_wake_port
+ if Rapns.config.wakeup
+ @interruptible_sleeper.enable_wake_on_udp Rapns.config.wakeup[:bind], Rapns.config.wakeup[:port]
end
end
@interruptible_sleeper
View
17 lib/rapns/notifier.rb
@@ -32,4 +32,21 @@ def close
end
end
+
+ # Call this from a client application after saving a Notification to the database to wakeup the Rapns
+ # Daemon to deliver the notification immediately.
+ def self.wakeup
+ notifier.notify
+ end
+
+ # Default notifier instance. This uses the :connect, :port values in Rapns.config.wakeup to connect to the
+ # wakeup socket in the Rapns Daemon. It will fall back to :host, :port if :connect is not specified.
+ def self.notifier
+ unless @notifier
+ if Rapns.config.wakeup
+ @notifier = Notifier.new(Rapns.config.wakeup[:connect] || Rapns.config.wakeup[:host], Rapns.config.wakeup[:port])
+ end
+ end
+ @notifier
+ end
end
View
22 spec/unit/daemon/feeder_spec.rb
@@ -5,8 +5,7 @@
:push_poll => 0,
:embedded => false,
:push => false,
- :udp_wake_host => nil,
- :udp_wake_port => nil) }
+ :wakeup => nil) }
let!(:app) { Rapns::Apns::App.create!(:name => 'my_app', :environment => 'development', :certificate => TEST_CERT) }
let(:notification) { Rapns::Apns::Notification.create!(:device_token => "a" * 64, :app => app) }
let(:logger) { double }
@@ -78,4 +77,23 @@ def start
Rapns::Daemon::Feeder.stub(:loop).and_yield
Rapns::Daemon::Feeder.start
end
+
+ it "creates the wakeup socket" do
+ bind = '127.0.0.1'
+ port = 12345
+ config.stub(:wakeup => { :bind => bind, :port => port})
+ sleeper = double(:enable_wake_on_udp => [bind, port])
+ sleeper.should_receive(:enable_wake_on_udp).with(bind, port)
+ Rapns::Daemon::InterruptibleSleep.stub(:new => sleeper)
+ # kludgy, but the instance variable is already set in previous tests, and this test
+ # requires it to be *not* initialised from a previous test.
+ Rapns::Daemon::Feeder.class_eval <<-EOF
+ def self.reset_interruptible_sleeper
+ @interruptible_sleeper = nil
+ end
+ EOF
+ Rapns::Daemon::Feeder.reset_interruptible_sleeper
+ Rapns::Daemon::Feeder.interruptible_sleeper
+ end
+
end
View
20 spec/unit/notifier_spec.rb
@@ -26,4 +26,24 @@
end
end
end
+
+ describe "default notifier" do
+ it "creates using :connect first" do
+ Rapns.config.stub :wakeup => { :connect => '127.0.0.1', :port => 1234 }
+ Rapns::Notifier.should_receive(:new).with('127.0.0.1', 1234)
+ Rapns.notifier
+ end
+
+ it "creates using :host next" do
+ Rapns.config.stub :wakeup => { :host => '127.0.0.1', :port => 1234 }
+ Rapns::Notifier.should_receive(:new).with('127.0.0.1', 1234)
+ Rapns.notifier
+ end
+
+ it "returns nil when wakeup is not specified" do
+ Rapns.config.stub :wakeup => nil
+ Rapns::Notifier.should_not_receive(:new)
+ expect(Rapns.notifier).to be_nil
+ end
+ end
end
Something went wrong with that request. Please try again.