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



90
91
92
93
# 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) ⇒ Object



81
82
83
# 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) ⇒ Object



85
86
87
88
# 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) ⇒ 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| Rpush::Client::Redis::Notification.find(id) }
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



73
74
75
# 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 = {}) ⇒ 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
# 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) ⇒ Object



77
78
79
# 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 = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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_countObject



109
110
111
112
113
114
115
116
# 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_connectionObject



103
104
# File 'lib/rpush/daemon/store/redis.rb', line 103

def release_connection
end

#reopen_logObject



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

def reopen_log
end

#translate_integer_notification_id(id) ⇒ Object



118
119
120
# File 'lib/rpush/daemon/store/redis.rb', line 118

def translate_integer_notification_id(id)
  id
end

#update_app(app) ⇒ Object



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

def update_app(app)
  app.save!
end

#update_notification(notification) ⇒ Object



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

def update_notification(notification)
  notification.save!
end