class Rpush::Daemon::Batch

Attributes

delivered[R]
failed[R]
notifications[R]
num_processed[R]
retryable[R]

Public Class Methods

new(notifications) click to toggle source
# File lib/rpush/daemon/batch.rb, line 9
def initialize(notifications)
  @notifications = notifications
  @num_processed = 0
  @delivered = []
  @failed = {}
  @retryable = {}
  @mutex = Mutex.new
end

Public Instance Methods

all_processed() click to toggle source
# File lib/rpush/daemon/batch.rb, line 95
def all_processed
  @mutex.synchronize do
    @num_processed = @notifications.size
    complete
  end
end
complete?() click to toggle source
# File lib/rpush/daemon/batch.rb, line 18
def complete?
  @complete == true
end
each_delivered(&blk) click to toggle source
# File lib/rpush/daemon/batch.rb, line 26
def each_delivered(&blk)
  @delivered.each(&blk)
end
each_notification(&blk) click to toggle source
# File lib/rpush/daemon/batch.rb, line 22
def each_notification(&blk)
  @notifications.each(&blk)
end
mark_all_delivered() click to toggle source
# File lib/rpush/daemon/batch.rb, line 59
def mark_all_delivered
  @mutex.synchronize do
    @delivered = @notifications
  end

  each_notification do |notification|
    Rpush::Daemon.store.mark_delivered(notification, Time.now, persist: false)
  end
end
mark_all_failed(code, message) click to toggle source
# File lib/rpush/daemon/batch.rb, line 78
def mark_all_failed(code, message)
  key = [code, message]
  @mutex.synchronize do
    @failed[key] = @notifications
  end
  each_notification do |notification|
    Rpush::Daemon.store.mark_failed(notification, code, message, Time.now, persist: false)
  end
end
mark_all_retryable(deliver_after, error) click to toggle source
# File lib/rpush/daemon/batch.rb, line 39
def mark_all_retryable(deliver_after, error)
  retryable_count = 0

  each_notification do |notification|
    next if notification.delivered || notification.failed

    retryable_count += 1
    mark_retryable(notification, deliver_after)
  end

  log_warn("Will retry #{retryable_count} of #{@notifications.size} notifications after #{deliver_after.strftime('%Y-%m-%d %H:%M:%S')} due to error (#{error.class.name}, #{error.message})")
end
mark_delivered(notification) click to toggle source
# File lib/rpush/daemon/batch.rb, line 52
def mark_delivered(notification)
  @mutex.synchronize do
    @delivered << notification
  end
  Rpush::Daemon.store.mark_delivered(notification, Time.now, persist: false)
end
mark_failed(notification, code, description) click to toggle source
# File lib/rpush/daemon/batch.rb, line 69
def mark_failed(notification, code, description)
  key = [code, description]
  @mutex.synchronize do
    @failed[key] ||= []
    @failed[key] << notification
  end
  Rpush::Daemon.store.mark_failed(notification, code, description, Time.now, persist: false)
end
mark_retryable(notification, deliver_after) click to toggle source
# File lib/rpush/daemon/batch.rb, line 30
def mark_retryable(notification, deliver_after)
  @mutex.synchronize do
    @retryable[deliver_after] ||= []
    @retryable[deliver_after] << notification
  end

  Rpush::Daemon.store.mark_retryable(notification, deliver_after, persist: false)
end
notification_processed() click to toggle source
# File lib/rpush/daemon/batch.rb, line 88
def notification_processed
  @mutex.synchronize do
    @num_processed += 1
    complete if @num_processed >= @notifications.size
  end
end

Private Instance Methods

complete() click to toggle source
# File lib/rpush/daemon/batch.rb, line 104
def complete
  return if complete?

  [:complete_delivered, :complete_failed, :complete_retried].each do |method|
    begin
      send(method)
    rescue StandardError => e
      Rpush.logger.error(e)
      reflect(:error, e)
    end
  end

  @complete = true
end
complete_delivered() click to toggle source
# File lib/rpush/daemon/batch.rb, line 119
def complete_delivered
  Rpush::Daemon.store.mark_batch_delivered(@delivered)
  @delivered.each do |notification|
    reflect(:notification_delivered, notification)
  end
end
complete_failed() click to toggle source
# File lib/rpush/daemon/batch.rb, line 126
def complete_failed
  @failed.each do |(code, description), notifications|
    Rpush::Daemon.store.mark_batch_failed(notifications, code, description)
    notifications.each do |notification|
      reflect(:notification_failed, notification)
    end
  end
end
complete_retried() click to toggle source
# File lib/rpush/daemon/batch.rb, line 135
def complete_retried
  @retryable.each do |deliver_after, notifications|
    Rpush::Daemon.store.mark_batch_retryable(notifications, deliver_after)
    notifications.each do |notification|
      reflect(:notification_will_retry, notification)
    end
  end
end