rapns 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rapns/daemon.rb CHANGED
@@ -4,6 +4,7 @@ require "rapns/daemon/delivery_error"
4
4
  require "rapns/daemon/pool"
5
5
  require "rapns/daemon/connection_pool"
6
6
  require "rapns/daemon/connection"
7
+ require "rapns/daemon/delivery_queue"
7
8
  require "rapns/daemon/delivery_handler"
8
9
  require "rapns/daemon/delivery_handler_pool"
9
10
  require "rapns/daemon/feeder"
@@ -29,7 +30,7 @@ module Rapns
29
30
  self.certificate = Certificate.new(configuration.certificate)
30
31
  certificate.load
31
32
 
32
- self.delivery_queue = Queue.new
33
+ self.delivery_queue = DeliveryQueue.new
33
34
 
34
35
  self.delivery_handler_pool = DeliveryHandlerPool.new(configuration.connections)
35
36
  delivery_handler_pool.populate
@@ -19,8 +19,8 @@ module Rapns
19
19
  protected
20
20
 
21
21
  def handle_next_notification
22
+ notification = Rapns::Daemon.delivery_queue.pop
22
23
  begin
23
- notification = Rapns::Daemon.delivery_queue.pop
24
24
  return if notification == STOP
25
25
 
26
26
  Rapns::Daemon.connection_pool.claim_connection do |connection|
@@ -46,6 +46,8 @@ module Rapns
46
46
  end
47
47
  rescue StandardError => e
48
48
  Rapns::Daemon.logger.error(e)
49
+ ensure
50
+ Rapns::Daemon.delivery_queue.signal
49
51
  end
50
52
  end
51
53
  end
@@ -0,0 +1,44 @@
1
+ module Rapns
2
+ module Daemon
3
+ class DeliveryQueue
4
+ def initialize
5
+ @queue = Queue.new
6
+ @waiting_threads = []
7
+ @mutex = Mutex.new
8
+ @counter = 0
9
+ end
10
+
11
+ def push(obj)
12
+ @mutex.synchronize { @counter += 1 }
13
+ @queue.push(obj)
14
+ end
15
+
16
+ def pop
17
+ @queue.pop
18
+ end
19
+
20
+ def signal
21
+ @mutex.synchronize do
22
+ begin
23
+ @counter -= 1
24
+ if @counter <= 0
25
+ t = @waiting_threads.shift
26
+ t.wakeup if t
27
+ end
28
+ rescue ThreadError
29
+ retry
30
+ end
31
+ end
32
+ end
33
+
34
+ def wait
35
+ Thread.exclusive do
36
+ if @counter > 0
37
+ @waiting_threads << Thread.current
38
+ Thread.stop
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -16,6 +16,8 @@ module Rapns
16
16
  Rapns::Notification.ready_for_delivery.each do |notification|
17
17
  Rapns::Daemon.delivery_queue.push(notification)
18
18
  end
19
+
20
+ Rapns::Daemon.delivery_queue.wait
19
21
  rescue StandardError => e
20
22
  Rapns::Daemon.logger.error(e)
21
23
  end
data/lib/rapns/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rapns
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe Rapns::Daemon::DeliveryHandler do
4
4
  before do
5
5
  @notification = Rapns::Notification.create!(:device_token => "a" * 64)
6
- Rapns::Daemon.stub(:delivery_queue).and_return(Queue.new)
6
+ Rapns::Daemon.stub(:delivery_queue).and_return(Rapns::Daemon::DeliveryQueue.new)
7
7
  Rapns::Daemon.delivery_queue.push(@notification)
8
8
  @connection = mock("Connection", :connect => nil, :write => nil)
9
9
  Rapns::Daemon::Connection.stub(:new).and_return(@connection)
@@ -6,7 +6,7 @@ describe Rapns::Daemon::Feeder do
6
6
  @notification = Rapns::Notification.create!(:device_token => "a" * 64)
7
7
  @logger = mock("Logger", :info => nil, :error => nil)
8
8
  Rapns::Daemon.stub(:logger).and_return(@logger)
9
- @queue = mock(:push => nil)
9
+ @queue = mock(:push => nil, :wait => nil)
10
10
  Rapns::Daemon.stub(:delivery_queue).and_return(@queue)
11
11
  Rapns::Daemon.stub(:configuration => mock("Configuration", :poll => 2))
12
12
  end
@@ -52,6 +52,11 @@ describe Rapns::Daemon::Feeder do
52
52
  Rapns::Daemon::Feeder.enqueue_notifications
53
53
  end
54
54
 
55
+ it "should wait for the delivery queue to be emptied" do
56
+ Rapns::Daemon.delivery_queue.should_receive(:wait)
57
+ Rapns::Daemon::Feeder.enqueue_notifications
58
+ end
59
+
55
60
  it "should log errors" do
56
61
  e = StandardError.new("bork")
57
62
  Rapns::Notification.stub(:ready_for_delivery).and_raise(e)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-30 00:00:00.000000000Z
12
+ date: 2011-08-31 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Easy to use library for Apple's Push Notification Service with Rails
15
15
  3
@@ -33,6 +33,7 @@ files:
33
33
  - lib/rapns/daemon/delivery_error.rb
34
34
  - lib/rapns/daemon/delivery_handler.rb
35
35
  - lib/rapns/daemon/delivery_handler_pool.rb
36
+ - lib/rapns/daemon/delivery_queue.rb
36
37
  - lib/rapns/daemon/feeder.rb
37
38
  - lib/rapns/daemon/logger.rb
38
39
  - lib/rapns/daemon/pool.rb