class APN::Daemon

Attributes

airbrake[RW]
apple[RW]
cert[RW]
connected[RW]
host[RW]
logger[RW]
queue[RW]
redis[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/apn/daemon.rb, line 14
def initialize(options = {})
  options[:redis_host]  ||= 'localhost'
  options[:redis_port]  ||= '6379'
  options[:host]        ||= 'gateway.sandbox.push.apple.com'
  options[:port]        ||=  2195
  options[:queue]       ||= 'apn_queue'
  options[:password]    ||= ''
  raise 'No cert provided!' unless options[:cert]

  redis_options = { :host => options[:redis_host], :port => options[:redis_port] }
  redis_options[:password] = options[:redis_password] if options.has_key?(:redis_password)

  @redis = Redis.new(redis_options)
  @queue = options[:queue]
  @cert = options[:cert]
  @password = options[:password]
  @host = options[:host]
  @port = options[:port]
  @dir = options[:dir]

  APN.configure do |config|
    config.log_file = options[:logfile]
  end

  APN.log(:info, "Listening on queue: #{self.queue}")
end

Public Instance Methods

client() click to toggle source
# 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!() click to toggle source
# File lib/apn/daemon.rb, line 41
def run!
  @last_notification = nil

  loop do
    begin
      message = @redis.blpop(self.queue, 1)
      if message
        @notification = APN::Notification.new(JSON.parse(message.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() click to toggle source
# 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