class Rpush::Daemon::Webpush::Delivery

Constants

OK
TEMPORARY_FAILURES

Public Class Methods

new(app, http, notification, batch) click to toggle source
# File lib/rpush/daemon/webpush/delivery.rb, line 26
def initialize(app, http, notification, batch)
  @app = app
  @http = http
  @notification = notification
  @batch = batch
end

Public Instance Methods

perform() click to toggle source
# File lib/rpush/daemon/webpush/delivery.rb, line 33
def perform
  response = send_request
  process_response response
rescue SocketError, SystemCallError => error
  mark_retryable(@notification, Time.now + 10.seconds, error)
  raise
rescue StandardError => error
  mark_failed(error)
  raise
ensure
  @batch.notification_processed
end

Private Instance Methods

deliver_after_header(response) click to toggle source
# File lib/rpush/daemon/webpush/delivery.rb, line 93
def deliver_after_header(response)
  Rpush::Daemon::RetryHeaderParser.parse(response.header['retry-after'])
end
fail_delivery(response) click to toggle source
# File lib/rpush/daemon/webpush/delivery.rb, line 87
def fail_delivery(response)
  fail_message = fail_message(response)
  log_error("#{@notification.id} failed: #{fail_message}")
  fail Rpush::DeliveryError.new(response.code.to_i, @notification.id, fail_message)
end
fail_message(response) click to toggle source
# File lib/rpush/daemon/webpush/delivery.rb, line 102
def fail_message(response)
  msg = Rpush::Daemon::HTTP_STATUS_CODES[response.code.to_i]
  if explanation = response.body.to_s[0..200].presence
    msg += ": #{explanation}"
  end
  msg
end
process_response(response) click to toggle source
# File lib/rpush/daemon/webpush/delivery.rb, line 66
def process_response(response)
  case response.code.to_i
  when *OK
    mark_delivered
  when *TEMPORARY_FAILURES
    retry_delivery(response)
  else
    fail_delivery(response)
  end
end
retry_delivery(response) click to toggle source
# File lib/rpush/daemon/webpush/delivery.rb, line 77
def retry_delivery(response)
  time = deliver_after_header(response)
  if time
    mark_retryable(@notification, time)
  else
    mark_retryable_exponential(@notification)
  end
  log_info("Webpush endpoint responded with a #{response.code} error. #{retry_message}")
end
retry_message() click to toggle source
# File lib/rpush/daemon/webpush/delivery.rb, line 97
def retry_message
  deliver_after = @notification.deliver_after.strftime('%Y-%m-%d %H:%M:%S')
  "Notification #{@notification.id} will be retried after #{deliver_after} (retry #{@notification.retries})."
end
send_request() click to toggle source
# File lib/rpush/daemon/webpush/delivery.rb, line 48
def send_request
  # The initializer is inherited from Webpush::Request and looks like
  # this:
  #
  # initialize(message: '', subscription:, vapid:, **options)
  #
  # where subscription is a hash of :endpoint and :keys, and vapid
  # holds the vapid public and private keys and the :subject (which is
  # an email address).
  Request.new(
    message: @notification.message,
    subscription: @notification.subscription,
    vapid: @app.vapid,
    ttl: @notification.time_to_live,
    urgency: @notification.urgency
  ).perform(@http)
end