Class: APN::Daemon
- Inherits:
-
Object
- Object
- APN::Daemon
- Defined in:
- lib/apn/daemon.rb
Instance Attribute Summary collapse
-
#airbrake ⇒ Object
Returns the value of attribute airbrake.
-
#apple ⇒ Object
Returns the value of attribute apple.
-
#cert ⇒ Object
Returns the value of attribute cert.
-
#connected ⇒ Object
Returns the value of attribute connected.
-
#host ⇒ Object
Returns the value of attribute host.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#redis ⇒ Object
Returns the value of attribute redis.
Instance Method Summary collapse
- #client ⇒ Object
-
#initialize(options = {}) ⇒ Daemon
constructor
A new instance of Daemon.
- #run! ⇒ Object
- #send_notification ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Daemon
Returns a new instance of Daemon.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/apn/daemon.rb', line 14 def initialize( = {}) [:redis_host] ||= 'localhost' [:redis_port] ||= '6379' [:host] ||= 'gateway.sandbox.push.apple.com' [:port] ||= 2195 [:queue] ||= 'apn_queue' [:password] ||= '' raise 'No cert provided!' unless [:cert] = { :host => [:redis_host], :port => [:redis_port] } [:password] = [:redis_password] if .has_key?(:redis_password) @redis = Redis.new() @queue = [:queue] @cert = [:cert] @password = [:password] @host = [:host] @port = [:port] @dir = [:dir] APN.configure do |config| config.log_file = [:logfile] end APN.log(:info, "Listening on queue: #{self.queue}") end |
Instance Attribute Details
#airbrake ⇒ Object
Returns the value of attribute airbrake.
12 13 14 |
# File 'lib/apn/daemon.rb', line 12 def airbrake @airbrake end |
#apple ⇒ Object
Returns the value of attribute apple.
12 13 14 |
# File 'lib/apn/daemon.rb', line 12 def apple @apple end |
#cert ⇒ Object
Returns the value of attribute cert.
12 13 14 |
# File 'lib/apn/daemon.rb', line 12 def cert @cert end |
#connected ⇒ Object
Returns the value of attribute connected.
12 13 14 |
# File 'lib/apn/daemon.rb', line 12 def connected @connected end |
#host ⇒ Object
Returns the value of attribute host.
12 13 14 |
# File 'lib/apn/daemon.rb', line 12 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
12 13 14 |
# File 'lib/apn/daemon.rb', line 12 def logger @logger end |
#queue ⇒ Object
Returns the value of attribute queue.
12 13 14 |
# File 'lib/apn/daemon.rb', line 12 def queue @queue end |
#redis ⇒ Object
Returns the value of attribute redis.
12 13 14 |
# File 'lib/apn/daemon.rb', line 12 def redis @redis end |
Instance Method Details
#client ⇒ Object
80 81 82 |
# File 'lib/apn/daemon.rb', line 80 def client @client ||= APN::Client.new(host: @host, port: @port, cert: @cert, password: @password, dir: @dir, queue: @queue) end |
#run! ⇒ Object
41 42 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 |
# File 'lib/apn/daemon.rb', line 41 def run! @last_notification = nil loop do begin = @redis.blpop(self.queue, 1) if @notification = APN::Notification.new(JSON.parse(.last,:symbolize_names => true)) send_notification end rescue Exception => e if e.class == Interrupt || e.class == SystemExit APN.log(:info, 'Shutting down...') exit(0) end APN.log(:error, "Encountered error: #{e}, backtrace #{e.backtrace}") APN.log(:info, 'Trying to reconnect...') client.connect! APN.log(:info, 'Reconnected') client.push(@notification) end end end |
#send_notification ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/apn/daemon.rb', line 69 def send_notification if @last_notification.nil? || @last_notification < Time.now - 3600 APN.log(:info, 'Forced reconnection...') client.connect! end client.push(@notification) @last_notification = Time.now end |