class Rpush::Daemon::Store::Redis

Constants

DEFAULT_MARK_OPTIONS

Public Instance Methods

all_apps() click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 11
def all_apps
  Rpush::Client::Redis::App.all
end
app(app_id) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 7
def app(app_id)
  Rpush::Client::Redis::App.find(app_id)
end
create_adm_notification(attrs, data, registration_ids, deliver_after, app) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 90
def create_adm_notification(attrs, data, registration_ids, deliver_after, app)
  notification = Rpush::Client::Redis::Adm::Notification.new
  create_gcm_like_notification(notification, attrs, data, registration_ids, deliver_after, app)
end
create_apns_feedback(failed_at, device_token, app) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 81
def create_apns_feedback(failed_at, device_token, app)
  Rpush::Client::Redis::Apns::Feedback.create!(failed_at: failed_at, device_token: device_token, app_id: app.id)
end
create_gcm_notification(attrs, data, registration_ids, deliver_after, app) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 85
def create_gcm_notification(attrs, data, registration_ids, deliver_after, app)
  notification = Rpush::Client::Redis::Gcm::Notification.new
  create_gcm_like_notification(notification, attrs, data, registration_ids, deliver_after, app)
end
deliverable_notifications(limit) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 15
def deliverable_notifications(limit)
  retryable_ids = retryable_notification_ids
  limit -= retryable_ids.size
  pending_ids = limit > 0 ? pending_notification_ids(limit) : []
  ids = retryable_ids + pending_ids
  ids.map { |id| Rpush::Client::Redis::Notification.find(id) }
end
mark_batch_delivered(notifications) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 30
def mark_batch_delivered(notifications)
  now = Time.now
  notifications.each { |n| mark_delivered(n, now) }
end
mark_batch_failed(notifications, code, description) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 46
def mark_batch_failed(notifications, code, description)
  now = Time.now
  notifications.each { |n| mark_failed(n, code, description, now) }
end
mark_batch_retryable(notifications, deliver_after) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 73
def mark_batch_retryable(notifications, deliver_after)
  notifications.each { |n| mark_retryable(n, deliver_after) }
end
mark_delivered(notification, time, opts = {}) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 23
def mark_delivered(notification, time, opts = {})
  opts = DEFAULT_MARK_OPTIONS.dup.merge(opts)
  notification.delivered = true
  notification.delivered_at = time
  notification.save!(validate: false) if opts[:persist]
end
mark_failed(notification, code, description, time, opts = {}) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 35
def mark_failed(notification, code, description, time, opts = {})
  opts = DEFAULT_MARK_OPTIONS.dup.merge(opts)
  notification.delivered = false
  notification.delivered_at = nil
  notification.failed = true
  notification.failed_at = time
  notification.error_code = code
  notification.error_description = description
  notification.save!(validate: false) if opts[:persist]
end
mark_ids_failed(ids, code, description, time) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 51
def mark_ids_failed(ids, code, description, time)
  ids.each { |id| mark_failed(Rpush::Client::Redis::Notification.find(id), code, description, time) }
end
mark_ids_retryable(ids, deliver_after) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 77
def mark_ids_retryable(ids, deliver_after)
  ids.each { |id| mark_retryable(Rpush::Client::Redis::Notification.find(id), deliver_after) }
end
mark_retryable(notification, deliver_after, opts = {}) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 55
def mark_retryable(notification, deliver_after, opts = {})
  opts = DEFAULT_MARK_OPTIONS.dup.merge(opts)
  notification.delivered = false
  notification.delivered_at = nil
  notification.failed = false
  notification.failed_at = nil
  notification.retries += 1
  notification.deliver_after = deliver_after

  return unless opts[:persist]

  notification.save!(validate: false)
  namespace = Rpush::Client::Redis::Notification.absolute_retryable_namespace
  Modis.with_connection do |redis|
    redis.zadd(namespace, deliver_after.to_i, notification.id)
  end
end
pending_delivery_count() click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 109
def pending_delivery_count
  Modis.with_connection do |redis|
    pending = redis.zrange(Rpush::Client::Redis::Notification.absolute_pending_namespace, 0, -1)
    retryable = redis.zrangebyscore(Rpush::Client::Redis::Notification.absolute_retryable_namespace, 0, Time.now.to_i)

    pending.count + retryable.count
  end
end
release_connection() click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 103
def release_connection
end
reopen_log() click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 106
def reopen_log
end
translate_integer_notification_id(id) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 118
def translate_integer_notification_id(id)
  id
end
update_app(app) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 95
def update_app(app)
  app.save!
end
update_notification(notification) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 99
def update_notification(notification)
  notification.save!
end

Private Instance Methods

create_gcm_like_notification(notification, attrs, data, registration_ids, deliver_after, app) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 124
def create_gcm_like_notification(notification, attrs, data, registration_ids, deliver_after, app) # rubocop:disable ParameterLists
  notification.assign_attributes(attrs)
  notification.data = data
  notification.registration_ids = registration_ids
  notification.deliver_after = deliver_after
  notification.app = app
  notification.save!
  notification
end
pending_notification_ids(limit) click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 148
def pending_notification_ids(limit)
  limit = [0, limit - 1].max # 'zrange key 0 1' will return 2 values, not 1.
  pending_ns = Rpush::Client::Redis::Notification.absolute_pending_namespace

  Modis.with_connection do |redis|
    pending_results = redis.multi do
      redis.zrange(pending_ns, 0, limit)
      redis.zremrangebyrank(pending_ns, 0, limit)
    end

    pending_results.first
  end
end
retryable_notification_ids() click to toggle source
# File lib/rpush/daemon/store/redis.rb, line 134
def retryable_notification_ids
  retryable_ns = Rpush::Client::Redis::Notification.absolute_retryable_namespace

  Modis.with_connection do |redis|
    retryable_results = redis.multi do
      now = Time.now.to_i
      redis.zrangebyscore(retryable_ns, 0, now)
      redis.zremrangebyscore(retryable_ns, 0, now)
    end

    retryable_results.first
  end
end