Skip to content
Browse files

Embed API

  • Loading branch information...
1 parent 8101590 commit 29a5336bffb4b0c6e7bb6f75af5004e4e47d146f @ileitch committed
Showing with 84 additions and 11 deletions.
  1. +2 −7 bin/rapns
  2. +7 −0 lib/rapns.rb
  3. +5 −1 lib/rapns/configuration.rb
  4. +11 −2 lib/rapns/daemon.rb
  5. +15 −0 lib/rapns/embed.rb
  6. +15 −1 spec/unit/daemon_spec.rb
  7. +29 −0 spec/unit/embed_spec.rb
View
9 bin/rapns
@@ -11,9 +11,7 @@ if environment.nil? || environment =~ /^-/
exit 1
end
-# A mock configuration to be used before the Rails environment is loaded.
-class CommandlineConfig < Struct.new(*Rapns::CONFIG_ATTRS); end
-config = CommandlineConfig.new
+config = Rapns::ConfigurationWithoutDefaults.new
ARGV.options do |opts|
opts.banner = banner
@@ -34,8 +32,5 @@ load 'config/environment.rb'
load 'config/initializers/rapns.rb' if File.exist?('config/initializers/rapns.rb')
Rapns.config.update(config)
-
-require 'rapns/daemon'
-require 'rapns/patches'
-
+Rapns.require_for_daemon
Rapns::Daemon.start
View
7 lib/rapns.rb
@@ -8,6 +8,7 @@
require 'rapns/notification'
require 'rapns/app'
require 'rapns/configuration'
+require 'rapns/embed'
require 'rapns/apns/binary_notification_validator'
require 'rapns/apns/device_token_format_validator'
@@ -21,3 +22,9 @@
require 'rapns/gcm/notification'
require 'rapns/gcm/app'
+module Rapns
+ def self.require_for_daemon
+ require 'rapns/daemon'
+ require 'rapns/patches'
+ end
+end
View
6 lib/rapns/configuration.rb
@@ -7,9 +7,12 @@ def self.configure
yield config if block_given?
end
- CONFIG_ATTRS = [:foreground, :push_poll, :feedback_poll,
+ CONFIG_ATTRS = [:foreground, :push_poll, :feedback_poll, :embedded,
:airbrake_notify, :check_for_errors, :pid_file, :batch_size]
+ class ConfigurationWithoutDefaults < Struct.new(*CONFIG_ATTRS)
+ end
+
class Configuration < Struct.new(*CONFIG_ATTRS)
attr_accessor :apns_feedback_callback
@@ -22,6 +25,7 @@ def initialize
self.airbrake_notify = true
self.check_for_errors = true
self.batch_size = 5000
+ self.embedded = false
end
def update(other)
View
13 lib/rapns/daemon.rb
@@ -39,7 +39,7 @@ def self.start
:airbrake_notify => Rapns.config.airbrake_notify)
setup_signal_hooks
- unless Rapns.config.foreground
+ unless Rapns.config.foreground || Rapns.config.embedded
daemonize
reconnect_database
end
@@ -47,11 +47,20 @@ def self.start
write_pid_file
ensure_upgraded
AppRunner.sync
- Feeder.start(Rapns.config.push_poll)
+
+ if Rapns.config.embedded
+ Thread.new { start_feeder }
+ else
+ start_feeder
+ end
end
protected
+ def self.start_feeder
+ Feeder.start(Rapns.config.push_poll)
+ end
+
def self.ensure_upgraded
count = 0
View
15 lib/rapns/embed.rb
@@ -0,0 +1,15 @@
+module Rapns
+ def self.embed(options = {})
+ Rapns.require_for_daemon
+
+ config = Rapns::ConfigurationWithoutDefaults.new
+ options.each { |k, v| config.send("#{k}=", v) }
+ config.embedded = true
+ Rapns.config.update(config)
+ Rapns::Daemon.start
+ end
+
+ def self.shutdown
+ Rapns::Daemon.shutdown
+ end
+end
View
16 spec/unit/daemon_spec.rb
@@ -5,7 +5,8 @@ module Rails; end
let(:certificate) { stub }
let(:password) { stub }
- let(:config) { stub(:pid_file => nil, :push_poll => 2, :airbrake_notify => false, :foreground => true) }
+ let(:config) { stub(:pid_file => nil, :push_poll => 2, :airbrake_notify => false,
+ :foreground => true, :embedded => false) }
let(:logger) { stub(:info => nil, :error => nil, :warn => nil) }
before do
@@ -31,6 +32,12 @@ module Rails; end
Rapns::Daemon.start
end
+ it "does not fork into a daemon if the embedded option is true" do
+ config.stub(:embedded => true)
+ Rapns::Daemon.should_not_receive(:daemonize)
+ Rapns::Daemon.start
+ end
+
it "writes the process ID to the PID file" do
Rapns::Daemon.should_receive(:write_pid_file)
Rapns::Daemon.start
@@ -48,6 +55,13 @@ module Rails; end
Rapns::Daemon.start
end
+ it "starts the feeder in a new thread if embedded" do
+ config.stub(:embedded => true)
+ Thread.should_receive(:new).and_yield
+ Rapns::Daemon::Feeder.should_receive(:start).with(2)
+ Rapns::Daemon.start
+ end
+
it "syncs apps" do
Rapns::Daemon::AppRunner.should_receive(:sync)
Rapns::Daemon.start
View
29 spec/unit/embed_spec.rb
@@ -0,0 +1,29 @@
+require 'unit_spec_helper'
+
+describe Rapns, 'embed' do
+ before do
+ Rapns::Daemon.stub(:start)
+ end
+
+ it 'sets the embedded config option to true' do
+ Rapns.embed
+ Rapns.config.embedded.should be_true
+ end
+
+ it 'starts the daemon' do
+ Rapns::Daemon.should_receive(:start)
+ Rapns.embed
+ end
+
+ it 'overrides the default config options with those given as a hash' do
+ Rapns.config.push_poll = 4
+ expect { Rapns.embed(:push_poll => 2) }.to change(Rapns.config, :push_poll).to(2)
+ end
+end
+
+describe Rapns, 'shutdown' do
+ it 'shuts down the daemon' do
+ Rapns::Daemon.should_receive(:shutdown)
+ Rapns.shutdown
+ end
+end

0 comments on commit 29a5336

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