class Pushr::Daemon::FcmSupport::ConnectionFcm
Constants
- IDLE_PERIOD
Attributes
authenticator[R]
configuration[R]
name[R]
response[R]
url[R]
Public Class Methods
new(configuration, i)
click to toggle source
# File lib/pushr/daemon/fcm_support/connection_fcm.rb, line 10 def initialize(configuration, i) @configuration = configuration @name = "#{@configuration.app}: ConnectionFcm #{i}" @authenticator = Pushr::Daemon::FcmSupport::Authenticator.new(configuration, i) @url = "https://fcm.googleapis.com/v1/projects/#{configuration.project_id}/messages:send" end
Public Instance Methods
connect()
click to toggle source
# File lib/pushr/daemon/fcm_support/connection_fcm.rb, line 17 def connect @last_use = Time.now uri = URI.parse(@url) @connection = open_http(uri.host, uri.port) @connection.start Pushr::Daemon.logger.info("[#{@name}] Connected to #{@url}") end
write(data)
click to toggle source
# File lib/pushr/daemon/fcm_support/connection_fcm.rb, line 25 def write(data) retry_count = 0 begin response = notification_request(data.to_message) handler = Pushr::Daemon::FcmSupport::ResponseHandler.new(response, data, retry_count) handler.handle rescue => e retry_count += 1 if retry_count < 10 retry else raise e end end end
Private Instance Methods
idle_period_exceeded?()
click to toggle source
# File lib/pushr/daemon/fcm_support/connection_fcm.rb, line 81 def idle_period_exceeded? # Timeout on the http connection is 5 minutes, reconnect after 5 minutes @last_use + IDLE_PERIOD < Time.now end
notification_request(data)
click to toggle source
# File lib/pushr/daemon/fcm_support/connection_fcm.rb, line 49 def notification_request(data) headers = { 'Authorization' => "Bearer #{@authenticator.fetch_access_token}", 'Content-type' => 'application/json' } uri = URI.parse(@url) post(uri, data, headers) end
open_http(host, port)
click to toggle source
# File lib/pushr/daemon/fcm_support/connection_fcm.rb, line 43 def open_http(host, port) http = Net::HTTP.new(host, port) http.use_ssl = true http end
post(uri, data, headers)
click to toggle source
# File lib/pushr/daemon/fcm_support/connection_fcm.rb, line 56 def post(uri, data, headers) reconnect_idle if idle_period_exceeded? retry_count = 0 begin response = @connection.post(uri.path, data, headers) @last_use = Time.now rescue EOFError, Errno::ECONNRESET, Timeout::Error => e retry_count += 1 Pushr::Daemon.logger.error("[#{@name}] Lost connection to #{@url} (#{e.class.name}), reconnecting ##{retry_count}...") if retry_count <= 3 reconnect sleep 1 retry else raise ConnectionError, "#{@name} tried #{retry_count - 1} times to reconnect but failed (#{e.class.name})." end end response end
reconnect()
click to toggle source
# File lib/pushr/daemon/fcm_support/connection_fcm.rb, line 91 def reconnect @connection.finish @last_use = Time.now @connection.start end
reconnect_idle()
click to toggle source
# File lib/pushr/daemon/fcm_support/connection_fcm.rb, line 86 def reconnect_idle Pushr::Daemon.logger.info("[#{@name}] Idle period exceeded, reconnecting...") reconnect end