Class: Rapnd::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/rapnd/daemon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
# File 'lib/rapnd/daemon.rb', line 14

def initialize(options = {})
  options[:redis_host]  ||= 'localhost'
  options[:redis_port]  ||= '6379'
  options[:host]        ||= 'gateway.sandbox.push.apple.com'
  options[:queue]       ||= 'rapnd_queue'
  options[:password]    ||= ''
  raise 'No cert provided!' unless options[:cert]
  
  Airbrake.configure { |config| config.api_key = options[:airbrake]; @airbrake = true; } if options[:airbrake]
  
  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]
  @host = options[:host]
  @logger = Logger.new("#{options[:dir]}/log/#{options[:queue]}.log")
  @logger.info "Listening on queue: #{self.queue}"
end

Instance Attribute Details

#airbrakeObject

Returns the value of attribute airbrake.



12
13
14
# File 'lib/rapnd/daemon.rb', line 12

def airbrake
  @airbrake
end

#appleObject

Returns the value of attribute apple.



12
13
14
# File 'lib/rapnd/daemon.rb', line 12

def apple
  @apple
end

#certObject

Returns the value of attribute cert.



12
13
14
# File 'lib/rapnd/daemon.rb', line 12

def cert
  @cert
end

#connectedObject

Returns the value of attribute connected.



12
13
14
# File 'lib/rapnd/daemon.rb', line 12

def connected
  @connected
end

#hostObject

Returns the value of attribute host.



12
13
14
# File 'lib/rapnd/daemon.rb', line 12

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/rapnd/daemon.rb', line 12

def logger
  @logger
end

#queueObject

Returns the value of attribute queue.



12
13
14
# File 'lib/rapnd/daemon.rb', line 12

def queue
  @queue
end

#redisObject

Returns the value of attribute redis.



12
13
14
# File 'lib/rapnd/daemon.rb', line 12

def redis
  @redis
end

Instance Method Details

#connect!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rapnd/daemon.rb', line 35

def connect!
  @logger.info 'Connecting...'
  @context      = OpenSSL::SSL::SSLContext.new
  @context.cert = OpenSSL::X509::Certificate.new(File.read(@cert))
  @context.key  = OpenSSL::PKey::RSA.new(File.read(@cert), @password)

  @sock         = TCPSocket.new(@host, 2195)
  self.apple    = OpenSSL::SSL::SSLSocket.new(@sock, @context)
  self.apple.sync = true
  self.apple.connect
  
  self.connected = true
  @logger.info 'Connected!'
  
  return @sock, @ssl
end

#run!Object



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
# File 'lib/rapnd/daemon.rb', line 52

def run!
  loop do
    begin
      message = @redis.blpop(self.queue, 1)
      if message
        notification = Rapnd::Notification.new(Marshal.load(message.last))
        self.connect! unless self.connected
        @logger.info "Sending #{notification.device_token}: #{notification.json_payload}"
        self.apple.write(notification.to_bytes)
      end
    rescue Exception => e
      if e.class == Interrupt || e.class == SystemExit
        @logger.info "Shutting down..."
        exit(0)
      end
      self.connect!
      if notification
        @logger.info "Trying again for: #{notification.json_payload}"
        self.apple.write(notification.to_bytes)
      end
      Airbrake.notify(e, {:environment_name => self.queue }) if @airbrake
      @logger.error "Encountered error: #{e}"
    end
  end
end