Class: Rpush::Daemon::Store::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/rpush/daemon/store/redis.rb

Constant Summary collapse

DEFAULT_MARK_OPTIONS =
{ persist: true }

Instance Method Summary collapse

Instance Method Details

#all_appsObject



11
12
13
# File 'lib/rpush/daemon/store/redis.rb', line 11

def all_apps
  Rpush::Client::Redis::App.all
end

#app(app_id) ⇒ Object



7
8
9
# 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) ⇒ Object



100
101
102
103
# File 'lib/rpush/daemon/store/redis.rb', line 100

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) ⇒ Object



91
92
93
# File 'lib/rpush/daemon/store/redis.rb', line 91

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) ⇒ Object



95
96
97
98
# File 'lib/rpush/daemon/store/redis.rb', line 95

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) ⇒ Object



15
16
17
18
19
20
21
# 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| find_notification_by_id(id) }.compact
end

#mark_batch_delivered(notifications) ⇒ Object



30
31
32
33
# 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) ⇒ Object



46
47
48
49
# 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) ⇒ Object



78
79
80
# File 'lib/rpush/daemon/store/redis.rb', line 78

def mark_batch_retryable(notifications, deliver_after)
  notifications.each { |n| mark_retryable(n, deliver_after) }
end

#mark_delivered(notification, time, opts = {}) ⇒ Object



23
24
25
26
27
28
# 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 = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/rpush/daemon/store/redis.rb', line 51

def mark_ids_failed(ids, code, description, time)
  ids.each do |id|
    notification = find_notification_by_id(id)
    next unless notification

    mark_failed(notification, code, description, time)
  end
end

#mark_ids_retryable(ids, deliver_after) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/rpush/daemon/store/redis.rb', line 82

def mark_ids_retryable(ids, deliver_after)
  ids.each do |id|
    notification = find_notification_by_id(id)
    next unless notification

    mark_retryable(notification, deliver_after)
  end
end

#mark_retryable(notification, deliver_after, opts = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rpush/daemon/store/redis.rb', line 60

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_countObject



119
120
121
122
123
124
125
126
# File 'lib/rpush/daemon/store/redis.rb', line 119

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_connectionObject



113
114
# File 'lib/rpush/daemon/store/redis.rb', line 113

def release_connection
end

#reopen_logObject



116
117
# File 'lib/rpush/daemon/store/redis.rb', line 116

def reopen_log
end

#translate_integer_notification_id(id) ⇒ Object



128
129
130
# File 'lib/rpush/daemon/store/redis.rb', line 128

def translate_integer_notification_id(id)
  id
end

#update_app(app) ⇒ Object



105
106
107
# File 'lib/rpush/daemon/store/redis.rb', line 105

def update_app(app)
  app.save!
end

#update_notification(notification) ⇒ Object



109
110
111
# File 'lib/rpush/daemon/store/redis.rb', line 109

def update_notification(notification)
  notification.save!
end