Class: Rpush::Daemon::Store::ActiveRecord
- Inherits:
-
Object
- Object
- Rpush::Daemon::Store::ActiveRecord
show all
- Includes:
- Reconnectable
- Defined in:
- lib/rpush/daemon/store/active_record.rb,
lib/rpush/daemon/store/active_record/reconnectable.rb
Defined Under Namespace
Modules: Reconnectable
Constant Summary
collapse
- DEFAULT_MARK_OPTIONS =
{ persist: true }
Reconnectable::ADAPTER_ERRORS
Instance Method Summary
collapse
-
#all_apps ⇒ Object
-
#app(id) ⇒ Object
-
#create_adm_notification(attrs, data, registration_ids, deliver_after, app) ⇒ Object
-
#create_apns_feedback(failed_at, device_token, app) ⇒ Object
-
#create_gcm_notification(attrs, data, registration_ids, deliver_after, app) ⇒ Object
-
#deliverable_notifications(limit) ⇒ Object
-
#initialize ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
-
#mark_batch_delivered(notifications) ⇒ Object
-
#mark_batch_failed(notifications, code, description) ⇒ Object
-
#mark_batch_retryable(notifications, deliver_after) ⇒ Object
-
#mark_delivered(notification, time, opts = {}) ⇒ Object
-
#mark_failed(notification, code, description, time, opts = {}) ⇒ Object
-
#mark_ids_failed(ids, code, description, time) ⇒ Object
-
#mark_ids_retryable(ids, deliver_after) ⇒ Object
-
#mark_retryable(notification, deliver_after, opts = {}) ⇒ Object
-
#pending_delivery_count ⇒ Object
-
#release_connection ⇒ Object
-
#reopen_log ⇒ Object
-
#translate_integer_notification_id(id) ⇒ Object
-
#update_app(app) ⇒ Object
-
#update_notification(notification) ⇒ Object
#check_database_is_connected, #database_connection_lost, #reconnect_database, #sleep_to_avoid_thrashing, #with_database_reconnect_and_retry
Constructor Details
Returns a new instance of ActiveRecord.
13
14
15
|
# File 'lib/rpush/daemon/store/active_record.rb', line 13
def initialize
reopen_log
end
|
Instance Method Details
#create_adm_notification(attrs, data, registration_ids, deliver_after, app) ⇒ Object
145
146
147
148
|
# File 'lib/rpush/daemon/store/active_record.rb', line 145
def create_adm_notification(attrs, data, registration_ids, deliver_after, app)
notification = Rpush::Client::ActiveRecord::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
133
134
135
136
137
138
|
# File 'lib/rpush/daemon/store/active_record.rb', line 133
def create_apns_feedback(failed_at, device_token, app)
with_database_reconnect_and_retry do
Rpush::Client::ActiveRecord::Apns::Feedback.create!(failed_at: failed_at,
device_token: device_token, app_id: app.id)
end
end
|
#create_gcm_notification(attrs, data, registration_ids, deliver_after, app) ⇒ Object
140
141
142
143
|
# File 'lib/rpush/daemon/store/active_record.rb', line 140
def create_gcm_notification(attrs, data, registration_ids, deliver_after, app)
notification = Rpush::Client::ActiveRecord::Gcm::Notification.new
create_gcm_like_notification(notification, attrs, data, registration_ids, deliver_after, app)
end
|
#deliverable_notifications(limit) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/rpush/daemon/store/active_record.rb', line 29
def deliverable_notifications(limit)
with_database_reconnect_and_retry do
Rpush::Client::ActiveRecord::Notification.transaction do
relation = ready_for_delivery
relation = relation.limit(limit)
notifications = relation.lock(true).to_a
mark_processing(notifications)
notifications
end
end
end
|
#mark_batch_delivered(notifications) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/rpush/daemon/store/active_record.rb', line 84
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
with_database_reconnect_and_retry do
Rpush::Client::ActiveRecord::Notification.where(id: ids).update_all(['processing = ?, delivered = ?, delivered_at = ?', false, true, now])
end
end
|
#mark_batch_failed(notifications, code, description) ⇒ Object
115
116
117
118
119
120
121
122
123
|
# File 'lib/rpush/daemon/store/active_record.rb', line 115
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
54
55
56
57
58
59
60
61
|
# File 'lib/rpush/daemon/store/active_record.rb', line 54
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
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/rpush/daemon/store/active_record.rb', line 71
def mark_delivered(notification, time, opts = {})
opts = DEFAULT_MARK_OPTIONS.dup.merge(opts)
notification.processing = false
notification.delivered = true
notification.delivered_at = time
return unless opts[:persist]
with_database_reconnect_and_retry do
notification.save!(validate: false)
end
end
|
#mark_failed(notification, code, description, time, opts = {}) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/rpush/daemon/store/active_record.rb', line 98
def mark_failed(notification, code, description, time, opts = {})
opts = DEFAULT_MARK_OPTIONS.dup.merge(opts)
notification.processing = false
notification.delivered = false
notification.delivered_at = nil
notification.failed = true
notification.failed_at = time
notification.error_code = code
notification.error_description = description
return unless opts[:persist]
with_database_reconnect_and_retry do
notification.save!(validate: false)
end
end
|
#mark_ids_failed(ids, code, description, time) ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/rpush/daemon/store/active_record.rb', line 125
def mark_ids_failed(ids, code, description, time)
return if ids.empty?
with_database_reconnect_and_retry do
Rpush::Client::ActiveRecord::Notification.where(id: ids).update_all(['processing = ?, delivered = ?, delivered_at = NULL, failed = ?, failed_at = ?, error_code = ?, error_description = ?', false, false, true, time, code, description])
end
end
|
#mark_ids_retryable(ids, deliver_after) ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/rpush/daemon/store/active_record.rb', line 63
def mark_ids_retryable(ids, deliver_after)
return if ids.empty?
with_database_reconnect_and_retry do
Rpush::Client::ActiveRecord::Notification.where(id: ids).update_all(['processing = ?, delivered = ?, delivered_at = ?, failed = ?, failed_at = ?, retries = retries + 1, deliver_after = ?', false, false, nil, false, nil, deliver_after])
end
end
|
#mark_retryable(notification, deliver_after, opts = {}) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/rpush/daemon/store/active_record.rb', line 41
def mark_retryable(notification, deliver_after, opts = {})
opts = DEFAULT_MARK_OPTIONS.dup.merge(opts)
notification.processing = false
notification.retries += 1
notification.deliver_after = deliver_after
return unless opts[:persist]
with_database_reconnect_and_retry do
notification.save!(validate: false)
end
end
|
#pending_delivery_count ⇒ Object
168
169
170
|
# File 'lib/rpush/daemon/store/active_record.rb', line 168
def pending_delivery_count
ready_for_delivery.count
end
|
#release_connection ⇒ Object
162
163
164
165
166
|
# File 'lib/rpush/daemon/store/active_record.rb', line 162
def release_connection
::ActiveRecord::Base.connection.close
rescue StandardError => e
Rpush.logger.error(e)
end
|
#reopen_log ⇒ Object
17
18
19
|
# File 'lib/rpush/daemon/store/active_record.rb', line 17
def reopen_log
::ActiveRecord::Base.logger = Rpush.logger.internal_logger
end
|
#translate_integer_notification_id(id) ⇒ Object
172
173
174
|
# File 'lib/rpush/daemon/store/active_record.rb', line 172
def translate_integer_notification_id(id)
id
end
|
#update_app(app) ⇒ Object
150
151
152
153
154
|
# File 'lib/rpush/daemon/store/active_record.rb', line 150
def update_app(app)
with_database_reconnect_and_retry do
app.save!
end
end
|
#update_notification(notification) ⇒ Object
156
157
158
159
160
|
# File 'lib/rpush/daemon/store/active_record.rb', line 156
def update_notification(notification)
with_database_reconnect_and_retry do
notification.save!
end
end
|