Class: Rpush::Daemon::Store::Mongoid

Inherits:
Object
  • Object
show all
Defined in:
lib/rpush/daemon/store/mongoid.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/mongoid.rb', line 11

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

#app(app_id) ⇒ Object


7
8
9
# File 'lib/rpush/daemon/store/mongoid.rb', line 7

def app(app_id)
  Rpush::Client::Mongoid::App.find(app_id)
end

#create_adm_notification(attrs, data, registration_ids, deliver_after, app) ⇒ Object


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

def create_adm_notification(attrs, data, registration_ids, deliver_after, app)
  notification = Rpush::Client::Mongoid::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


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

def create_apns_feedback(failed_at, device_token, app)
  Rpush::Client::Mongoid::Apns::Feedback.create!(failed_at: failed_at, device_token: device_token, app: app)
end

#create_gcm_notification(attrs, data, registration_ids, deliver_after, app) ⇒ Object


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

def create_gcm_notification(attrs, data, registration_ids, deliver_after, app)
  notification = Rpush::Client::Mongoid::Gcm::Notification.new
  create_gcm_like_notification(notification, attrs, data, registration_ids, deliver_after, app)
end

#deliverable_notifications(limit) ⇒ Object


15
16
17
18
# File 'lib/rpush/daemon/store/mongoid.rb', line 15

def deliverable_notifications(limit)
  relation = ready_for_delivery.limit(limit)
  claim_notifications(relation)
end

#mark_batch_delivered(notifications) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rpush/daemon/store/mongoid.rb', line 27

def mark_batch_delivered(notifications)
  return if notifications.empty?

  now = Time.now
  ids = []
  notifications.each do |n|
    mark_delivered(n, now, persist: false)
    ids << n.id
  end
  Rpush::Client::Mongoid::Notification.in(id: ids).update_all(processing: false, delivered: true, delivered_at: now)
end

#mark_batch_failed(notifications, code, description) ⇒ Object


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

def mark_batch_failed(notifications, code, description)
  now = Time.now
  ids = []
  notifications.each do |n|
    mark_failed(n, code, description, now, persist: false)
    ids << n.id
  end
  mark_ids_failed(ids, code, description, now)
end

#mark_batch_retryable(notifications, deliver_after) ⇒ Object


80
81
82
83
84
85
86
87
# File 'lib/rpush/daemon/store/mongoid.rb', line 80

def mark_batch_retryable(notifications, deliver_after)
  ids = []
  notifications.each do |n|
    mark_retryable(n, deliver_after, persist: false)
    ids << n.id
  end
  mark_ids_retryable(ids, deliver_after)
end

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


20
21
22
23
24
25
# File 'lib/rpush/daemon/store/mongoid.rb', line 20

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


39
40
41
42
43
44
45
46
47
48
# File 'lib/rpush/daemon/store/mongoid.rb', line 39

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


60
61
62
63
64
# File 'lib/rpush/daemon/store/mongoid.rb', line 60

def mark_ids_failed(ids, code, description, time)
  return if ids.empty?

  Rpush::Client::Mongoid::Notification.in(id: ids).update_all(processing: false, delivered: false, delivered_at: nil, failed: true, failed_at: time, error_code: code, error_description: description)
end

#mark_ids_retryable(ids, deliver_after) ⇒ Object


89
90
91
92
93
# File 'lib/rpush/daemon/store/mongoid.rb', line 89

def mark_ids_retryable(ids, deliver_after)
  return if ids.empty?

  Rpush::Client::Mongoid::Notification.in(id: ids).update_all(processing: false, delivered: false, delivered_at: nil, failed: false, failed_at: nil, deliver_after: deliver_after, '$inc' => { retries: 1 })
end

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


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rpush/daemon/store/mongoid.rb', line 66

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)
end

#pending_delivery_countObject


123
124
125
# File 'lib/rpush/daemon/store/mongoid.rb', line 123

def pending_delivery_count
  ready_for_delivery.count
end

#release_connectionObject


117
118
# File 'lib/rpush/daemon/store/mongoid.rb', line 117

def release_connection
end

#reopen_logObject


120
121
# File 'lib/rpush/daemon/store/mongoid.rb', line 120

def reopen_log
end

#translate_integer_notification_id(id) ⇒ Object


127
128
129
# File 'lib/rpush/daemon/store/mongoid.rb', line 127

def translate_integer_notification_id(id)
  Rpush::Client::Mongoid::Notification.find_by(integer_id: id).id
end

#update_app(app) ⇒ Object


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

def update_app(app)
  app.save!
end

#update_notification(notification) ⇒ Object


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

def update_notification(notification)
  notification.save!
end