Class: Resque::Plugins::Aps::Application

Inherits:
Object
  • Object
show all
Extended by:
Helper
Includes:
Helper
Defined in:
lib/resque/plugins/aps/application.rb

Constant Summary collapse

@@CAFile =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

logger

Constructor Details

#initialize(attributes) ⇒ Application

Returns a new instance of Application.



150
151
152
153
154
# File 'lib/resque/plugins/aps/application.rb', line 150

def initialize(attributes)
  attributes.each do |k, v|
    respond_to?(:"#{k}=") ? send(:"#{k}=", v) : raise(Resque::Plugins::Aps::UnknownAttributeError, "unknown attribute: #{k}")
  end
end

Instance Attribute Details

#cert_fileObject

Returns the value of attribute cert_file.



11
12
13
# File 'lib/resque/plugins/aps/application.rb', line 11

def cert_file
  @cert_file
end

#cert_passwdObject

Returns the value of attribute cert_passwd.



11
12
13
# File 'lib/resque/plugins/aps/application.rb', line 11

def cert_passwd
  @cert_passwd
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/resque/plugins/aps/application.rb', line 11

def name
  @name
end

Class Method Details

.application_exception(exception, name) ⇒ Object



144
145
146
147
148
# File 'lib/resque/plugins/aps/application.rb', line 144

def self.application_exception(exception, name)
  exc = Exception.new("#{exception} (#{name})")
  exc.set_backtrace(exception.backtrace)
  return exc
end

.close_sockets(socket, ssl_socket) ⇒ Object

Close the sockets



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/resque/plugins/aps/application.rb', line 126

def self.close_sockets(socket, ssl_socket)
  begin
    if ssl_socket
      ssl_socket.close
    end
  rescue
    Resque.logger.error("#{$!}: #{$!.backtrace.join("\n")}") if Resque.logger
  end

  begin
    if socket
      socket.close
    end
  rescue
    Resque.logger.error("#{$!}: #{$!.backtrace.join("\n")}") if Resque.logger
  end
end

.create_sockets(cert, passphrase, host, port) ⇒ Object

Create the TCP and SSL sockets for sending the notification



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/resque/plugins/aps/application.rb', line 101

def self.create_sockets(cert, passphrase, host, port)
  ctx = OpenSSL::SSL::SSLContext.new
  ctx.key = OpenSSL::PKey::RSA.new(cert, passphrase)
  ctx.cert = OpenSSL::X509::Certificate.new(cert)

  if @@CAFile && File.exists?(@@CAFile)
    ctx.ca_file = @@CAFile
  end
  if defined?(ROOT_CA) && ROOT_CA && File.directory?(ROOT_CA)
    ctx.ca_path = ROOT_CA
  end
  ctx.verify_callback = proc do |preverify_ok, ssl_context|
    Resque::Plugins::Aps::Application.verify_ssl_certificate(preverify_ok, ssl_context)
  end

  s = TCPSocket.new(host, port)
  ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
  ssl.sync = true

  return s, ssl
end

.perform(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/resque/plugins/aps/application.rb', line 20

def self.perform(*args)
  app_name = args[0]
  @aps_retry = args[1] || false
  begin
    Resque.enqueue_aps_application(app_name) if @aps_retry
    count, duration, ex = Resque::Plugins::Aps::Application.perform_no_fail(app_name)
    logger.info("Sent #{count} #{app_name} notifications in batch over #{duration} sec.") if logger
    Resque.dequeue_aps_application(app_name) if @aps_retry
    raise ex if ex
  rescue
    Resque.dequeue_aps_application(app_name) if @aps_retry
    raise $!
  end
end

.perform_clear(app_name) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/resque/plugins/aps/application.rb', line 35

def self.perform_clear(app_name)
  while true
    count, duration, ex = Resque::Plugins::Aps::Application.perform_no_fail(app_name, false, true)
    p "Sent #{count} #{app_name} notifications in batch over #{duration} sec. Returned #{ex.inspect}"
    break if ex.nil?
  end
end

.perform_no_fail(app_name, requeue = true, read_response = false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/resque/plugins/aps/application.rb', line 43

def self.perform_no_fail(app_name, requeue = true, read_response = false)
  count = 0
  start = Time.now
  excep = nil
  duration = Benchmark.realtime do
    Resque.aps_application(app_name).socket do |socket, app|
      n_old = nil
      while true
        n = Resque.dequeue_aps(app_name)
        if n.nil?
          if @aps_retry && app.aps_nil_notification_retry?(count, start)
            next
          else
            break
          end
        end

        app.before_aps_write n
        begin
          n.batch_id = count + 1
          n.expiry   = Time.now.utc.to_i + 3600
          socket.write(n.formatted)
          app.after_aps_write n
          count += 1
          if read_response
            resp = socket.read
            if resp && resp != ""
              # logger.error "Failure response: #{resp.inspect}" if logger
              logger.error "Failure response: #{resp.bytes.to_a.map{|i| i.to_s(16)}.join}" if logger
              break
            end
          end
        rescue
          # logger.error Application.application_exception($!, app_name) if logger
          app.failed_aps_write n, $!, n_old
          logger.error "#{$!}: Sent #{count} notifications before failure." if logger
          Resque.enqueue_aps(app_name, n) if requeue
          excep = $!
          break
        end
        n_old = n
      end
    end
  end
  return count, duration, excep
end

.verify_ssl_certificate(preverify_ok, ssl_context) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/resque/plugins/aps/application.rb', line 90

def self.verify_ssl_certificate(preverify_ok, ssl_context)
  if preverify_ok != true || ssl_context.error != 0
    err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})"
    raise OpenSSL::SSL::SSLError.new(err_msg)
  end
  true
end

Instance Method Details

#after_aps_read(feedback) ⇒ Object



216
217
218
# File 'lib/resque/plugins/aps/application.rb', line 216

def after_aps_read(feedback)
  logger.debug("ResqueAps[after_read]: #{feedback.to_s}") if logger
end

#after_aps_write(notification) ⇒ Object



197
198
199
# File 'lib/resque/plugins/aps/application.rb', line 197

def after_aps_write(notification)
  logger.debug("ResqueAps[after_write]: #{notification}") if logger
end

#aps_nil_notification_retry?(sent_count, start_time) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/resque/plugins/aps/application.rb', line 208

def aps_nil_notification_retry?(sent_count, start_time)
  false
end

#aps_read_error(exception) ⇒ Object



220
221
222
# File 'lib/resque/plugins/aps/application.rb', line 220

def aps_read_error(exception)
  logger.error("ResqueAps[read_error]: #{exception} (#{name}): #{exception.backtrace.join("\n")}") if logger
end

#aps_read_failedObject



224
225
226
# File 'lib/resque/plugins/aps/application.rb', line 224

def aps_read_failed
  logger.error("ResqueAps[read_failed]: Bad data on the socket (#{name})") if logger
end

#before_aps_readObject



212
213
214
# File 'lib/resque/plugins/aps/application.rb', line 212

def before_aps_read
  logger.debug("ResqueAps[before_read]:") if logger
end

#before_aps_write(notification) ⇒ Object



193
194
195
# File 'lib/resque/plugins/aps/application.rb', line 193

def before_aps_write(notification)
  logger.debug("ResqueAps[before_write]: #{notification}") if logger
end

#failed_aps_write(notification, exception, previous_notification = nil) ⇒ Object



201
202
203
# File 'lib/resque/plugins/aps/application.rb', line 201

def failed_aps_write(notification, exception, previous_notification = nil)
  logger.error("ResqueAps[write_failed]: #{exception} (#{notification}): #{exception.backtrace.join("\n")}") if logger
end

#inspectObject



16
17
18
# File 'lib/resque/plugins/aps/application.rb', line 16

def inspect
  "#<#{self.class.name} #{name.inspect}, #{cert_passwd.inspect}, #{cert_file.inspect}>"
end

#notify_aps_admin(exception) ⇒ Object



205
206
# File 'lib/resque/plugins/aps/application.rb', line 205

def notify_aps_admin(exception)
end

#socket(cert = nil, certp = nil, host = nil, port = nil, &block) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/resque/plugins/aps/application.rb', line 156

def socket(cert = nil, certp = nil, host = nil, port = nil, &block)
  logger.debug("resque-aps: ssl_socket(#{name})") if logger
  exc = nil

  begin
    socket, ssl_socket = Application.create_sockets(cert || File.read(cert_file),
                                                    certp || cert_passwd,
                                                    host || Resque.aps_gateway_host,
                                                    port || Resque.aps_gateway_port)
  rescue
    raise Application.application_exception($!, name)
  end

  begin
    ssl_socket.connect
    yield ssl_socket, self if block_given?
  rescue
    exc = Application.application_exception($!, name)
    if $!.message =~ /^SSL_connect .* certificate (expired|revoked)/ || $!.message =~ /^SSL Verification failed/
      notify_aps_admin exc
    end
    raise exc
  ensure
    Application.close_sockets(socket, ssl_socket)
  end

  exc
end

#to_hashObject



185
186
187
# File 'lib/resque/plugins/aps/application.rb', line 185

def to_hash
  {'name' => name, 'cert_file' => cert_file, 'cert_passwd' => cert_passwd}
end

#to_jsonObject



189
190
191
# File 'lib/resque/plugins/aps/application.rb', line 189

def to_json
  to_hash.to_json
end