banjo-apn_sender 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MmViNGY0ZDRiZDE3NjZlMmJjODlkM2QyZDNjY2U3NWNmN2Q2NjE4YQ==
5
+ data.tar.gz: !binary |-
6
+ NWVhYzU5YzNjZjkzOTQyZmQ0NWRhYTk0ZTc1N2M1NmU0OWY1ZDI2Yg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MDZmMjJjODgyOTVjY2MyODIzNjk3ZjQ5NDg0NWNjZDAwZjlmN2JiOTdkZjA1
10
+ MjY5YmQ5MWRkOTMxZjQ3MDFhNjVhNzUzN2I1YzdkYWJhYmY2ZDBlOTMyODJl
11
+ OGVlNTBlMzFkYjJlNTE4NTU2NmQwY2FiZTgyOGE1NzQxMjcwNzI=
12
+ data.tar.gz: !binary |-
13
+ ZTlhZGRiZjQ1M2VkMTE0NzNmZTFkYzMwOTQ2ZDdmNmM4MjMzNDkwM2UyMWY5
14
+ MGQzODk1MDczMDNmMTliMjUyYzRjNGZhZDM4ZTc4M2ZmZGExOTM1MjYyYWVj
15
+ YmEyNWIwNWE2YjE0MTFlZTc2ZTFhZmIxNmMyMjExMGY1OGRiNDM=
data/apn_sender.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{banjo-apn_sender}
8
- s.version = "2.0.1"
8
+ s.version = "2.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Kali Donovan}, %q{KW Justin Leung}]
data/lib/apn/base.rb CHANGED
@@ -5,10 +5,13 @@ module APN
5
5
  # APN::Base takes care of all the boring certificate loading, socket creating, and logging
6
6
  # responsibilities so APN::Sender and APN::Feedback and focus on their respective specialties.
7
7
  module Base
8
- attr_accessor :opts, :logger
8
+ attr_accessor :opts, :logger, :fifos
9
+
10
+ FIFO_SIZE = 5
9
11
 
10
12
  def initialize(opts = {})
11
13
  @opts = opts
14
+ @fifos = Hash.new { [] }
12
15
 
13
16
  setup_logger
14
17
  setup_paths
@@ -21,12 +24,6 @@ module APN
21
24
  return @socket
22
25
  end
23
26
 
24
- protected
25
- # Default to Rails logger, if available
26
- def setup_logger
27
- @logger = defined?(::Rails.logger) ? ::Rails.logger : Logger.new(STDOUT)
28
- end
29
-
30
27
  # Log message to any logger provided by the user (e.g. the Rails logger).
31
28
  # Accepts +log_level+, +message+, since that seems to make the most sense,
32
29
  # and just +message+, to be compatible with Resque's log method and to enable
@@ -38,7 +35,7 @@ module APN
38
35
  level, message = 'info', level if message.nil? # Handle only one argument if called from Resque, which expects only message
39
36
 
40
37
  return false unless self.logger && self.logger.respond_to?(level)
41
- self.logger.send(level, "[#{apn_environment}] #{Time.now}: #{message}")
38
+ self.logger.send(level, "[APNConnection:#{object_id} #{apn_environment}] #{message}")
42
39
  end
43
40
 
44
41
  # Log the message first, to ensure it reports what went wrong if in daemon mode.
@@ -48,6 +45,12 @@ module APN
48
45
  raise msg
49
46
  end
50
47
 
48
+ protected
49
+ # Default to Rails logger, if available
50
+ def setup_logger
51
+ @logger = defined?(::Rails.logger) ? ::Rails.logger : Logger.new(STDOUT)
52
+ end
53
+
51
54
  def apn_enterprise?
52
55
  @apn_enterprise ||= @opts[:enterprise].present?
53
56
  end
@@ -89,6 +92,7 @@ module APN
89
92
  return true if @socket && @socket_tcp && !@socket.closed? && !@socket_tcp.closed?
90
93
  log_and_die("Trying to open half-open connection") if (@socket && !@socket.closed?) || (@socket_tcp && !@socket_tcp.closed?)
91
94
 
95
+ log(:info, "Setting up SSL connection to APN...")
92
96
  ctx = OpenSSL::SSL::SSLContext.new
93
97
  ctx.cert = OpenSSL::X509::Certificate.new(@apn_cert)
94
98
  ctx.key = OpenSSL::PKey::RSA.new(@apn_cert)
@@ -6,22 +6,30 @@ class APN::Connection
6
6
  @sandbox_senders = Hash.new
7
7
  @enterprise_semaphore = Mutex.new
8
8
 
9
- def send_to_apple(notification)
9
+
10
+ def push_fifo(env, token)
11
+ @fifos[env] <<= token
12
+ @fifos[env].shift if @fifos[env][FIFO_SIZE]
13
+ end
14
+
15
+ def send_to_apple(notification, token, env, tag)
10
16
  retries = 0
17
+ push_fifo(env, token)
11
18
 
12
19
  begin
13
20
  self.socket.write( notification.to_s )
14
21
  rescue => e
15
- Rails.logger.error("Try #{retries}: APNConnection to #{apn_host} error with #{e}")
22
+ log(:error, "Try #{retries}: #{e.class} to #{apn_host}: #{e.message}, recent_tokens: #{@fifos[env]}")
16
23
 
17
24
  # Try reestablishing the connection
18
25
  if (retries += 1) <= TIMES_TO_RETRY_SOCKET_ERROR
19
26
  teardown_connection
27
+ sleep 1
20
28
  setup_connection
21
29
  retry
22
30
  end
23
31
 
24
- Rails.logger.error("APNConnection gave up send_to_apple after #{retries} failures")
32
+ log(:error, "#{e.class} to #{apn_host}: #{e.message}, recent_tokens: #{@fifos[env]}")
25
33
  raise e
26
34
  end
27
35
  end
@@ -47,11 +55,14 @@ class APN::Connection
47
55
  end
48
56
  end
49
57
 
50
- Rails.logger.info "[APNConnection:#{sender.object_id} #{sandbox ? 'sandbox' : 'production'}#{enterprise ? ' enterprise' : ''}] token: #{token} message: #{options}"
58
+ env = sandbox ? 'sandbox' : enterprise ? 'enterprise' : 'production'
59
+ tag = "#{sandbox ? 'sandbox' : 'production'}#{enterprise ? ' enterprise' : ''}"
60
+ sender.log(:info, "token: #{token} message: #{options}")
61
+
51
62
  if enterprise
52
- @enterprise_semaphore.synchronize { sender.send_to_apple(msg) }
63
+ @enterprise_semaphore.synchronize { sender.send_to_apple(msg, token, env, tag) }
53
64
  else
54
- sender.send_to_apple(msg)
65
+ sender.send_to_apple(msg, token, env, tag)
55
66
  end
56
67
  end
57
68
 
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: banjo-apn_sender
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
5
- prerelease:
4
+ version: 2.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Kali Donovan
@@ -15,7 +14,6 @@ dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: apn_sender
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
18
  - - ! '>='
21
19
  - !ruby/object:Gem::Version
@@ -23,7 +21,6 @@ dependencies:
23
21
  type: :runtime
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
25
  - - ! '>='
29
26
  - !ruby/object:Gem::Version
@@ -31,7 +28,6 @@ dependencies:
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: yajl-ruby
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
32
  - - ! '>='
37
33
  - !ruby/object:Gem::Version
@@ -39,7 +35,6 @@ dependencies:
39
35
  type: :runtime
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
39
  - - ! '>='
45
40
  - !ruby/object:Gem::Version
@@ -68,25 +63,24 @@ files:
68
63
  - lib/apn/notification.rb
69
64
  homepage: http://github.com/BanjoInc/apn_sender
70
65
  licenses: []
66
+ metadata: {}
71
67
  post_install_message:
72
68
  rdoc_options: []
73
69
  require_paths:
74
70
  - lib
75
71
  required_ruby_version: !ruby/object:Gem::Requirement
76
- none: false
77
72
  requirements:
78
73
  - - ! '>='
79
74
  - !ruby/object:Gem::Version
80
75
  version: '0'
81
76
  required_rubygems_version: !ruby/object:Gem::Requirement
82
- none: false
83
77
  requirements:
84
78
  - - ! '>='
85
79
  - !ruby/object:Gem::Version
86
80
  version: '0'
87
81
  requirements: []
88
82
  rubyforge_project:
89
- rubygems_version: 1.8.23
83
+ rubygems_version: 2.2.2
90
84
  signing_key:
91
85
  specification_version: 3
92
86
  summary: APN connection pluggable to multithreaded background worker (like SideKiq)