Skip to content

Instantly share code, notes, and snippets.

@paul-ylz
Created May 26, 2016 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul-ylz/dab3311338bd3b2d5e9e8602ec260f99 to your computer and use it in GitHub Desktop.
Save paul-ylz/dab3311338bd3b2d5e9e8602ec260f99 to your computer and use it in GitHub Desktop.
Houston APNS - persistent connection with error handling
# With thanks to Houston gem maintainers and
# http://joshsymonds.com/blog/2013/10/17/sidekiq-plus-houston-production-ready/
#
module ApnsConnHandler
class ApnConnection
def initialize
setup()
end
def setup
@conn = Houston::Connection.new(
Houston::APPLE_PRODUCTION_GATEWAY_URI, # uri
File.read(ENV['APNS_CERT']), # cert
nil # passphrase
)
@conn.open
end
def write(data)
begin
raise "Connection is closed" unless @conn.open?
@conn.write data
rescue => e
attempts ||= 0
attempts += 1
if attempts < 5
setup()
retry
else
raise e
end
end
end
end
APNS_POOL = ConnectionPool.new(size: 15, timeout: 10) do
ApnConnection.new
end
def apns_push(apn_pushes)
APNS_POOL.with do |connection|
apn_pushes.each do |apn_push|
notification = Houston::Notification.new apn_push.houston_options
connection.write notification.message
if notification.error
logger.error "Error: #{notification.error}"
else
logger.info "apn_push done for #{apn_push.token}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment