Class: Rpush::Daemon::Store::ActiveRecord

Inherits:
Object
  • Object
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}

Constants included from Reconnectable

Reconnectable::ADAPTER_ERRORS

Instance Method Summary collapse

Methods included from Reconnectable

#check_database_is_connected, #database_connection_lost, #reconnect_database, #sleep_to_avoid_thrashing, #with_database_reconnect_and_retry

Instance Method Details

#after_daemonizeObject



126
127
128
# File 'lib/rpush/daemon/store/active_record.rb', line 126

def after_daemonize
  reconnect_database
end

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



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

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



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

def create_apns_feedback(failed_at, device_token, app)
  with_database_reconnect_and_retry do
    Rpush::Apns::Feedback.create!(:failed_at => failed_at,
      :device_token => device_token, :app => app)
  end
end

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



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

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

#deliverable_notifications(apps) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/rpush/daemon/store/active_record.rb', line 13

def deliverable_notifications(apps)
  with_database_reconnect_and_retry do
    batch_size = Rpush.config.batch_size
    relation = Rpush::Notification.ready_for_delivery.for_apps(apps)
    relation = relation.limit(batch_size) unless Rpush.config.push
    relation.to_a
  end
end

#mark_batch_delivered(notifications) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rpush/daemon/store/active_record.rb', line 57

def mark_batch_delivered(notifications)
  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::Notification.where(:id => ids).update_all(['delivered = ?, delivered_at = ?', true, now])
  end
end

#mark_batch_failed(notifications, code, description) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rpush/daemon/store/active_record.rb', line 85

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
  with_database_reconnect_and_retry do
    Rpush::Notification.where(:id => ids).update_all(['delivered = ?, delivered_at = NULL, failed = ?, failed_at = ?, error_code = ?, error_description = ?', false, true, now, code, description])
  end
end

#mark_batch_retryable(notifications, deliver_after) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/rpush/daemon/store/active_record.rb', line 34

def mark_batch_retryable(notifications, deliver_after)
  ids = []
  notifications.each do |n|
    mark_retryable(n, deliver_after, :persist => false)
    ids << n.id
  end
  with_database_reconnect_and_retry do
    Rpush::Notification.where(:id => ids).update_all(['retries = retries + 1, deliver_after = ?', deliver_after])
  end
end

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



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rpush/daemon/store/active_record.rb', line 45

def mark_delivered(notification, time, opts = {})
  opts = DEFAULT_MARK_OPTIONS.dup.merge(opts)
  notification.delivered = true
  notification.delivered_at = time

  if opts[:persist]
    with_database_reconnect_and_retry do
      notification.save!(:validate => false)
    end
  end
end

#mark_failed(notification, code, description, time, opts = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rpush/daemon/store/active_record.rb', line 69

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

  if opts[:persist]
    with_database_reconnect_and_retry do
      notification.save!(:validate => false)
    end
  end
end

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



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rpush/daemon/store/active_record.rb', line 22

def mark_retryable(notification, deliver_after, opts = {})
  opts = DEFAULT_MARK_OPTIONS.dup.merge(opts)
  notification.retries += 1
  notification.deliver_after = deliver_after

  if opts[:persist]
    with_database_reconnect_and_retry do
      notification.save!(:validate => false)
    end
  end
end

#release_connectionObject



130
131
132
133
134
135
136
# File 'lib/rpush/daemon/store/active_record.rb', line 130

def release_connection
  begin
    ::ActiveRecord::Base.connection_pool.release_connection
  rescue StandardError => e
    Rpush.logger.error(e)
  end
end

#update_app(app) ⇒ Object



114
115
116
117
118
# File 'lib/rpush/daemon/store/active_record.rb', line 114

def update_app(app)
  with_database_reconnect_and_retry do
    app.save!
  end
end

#update_notification(notification) ⇒ Object



120
121
122
123
124
# File 'lib/rpush/daemon/store/active_record.rb', line 120

def update_notification(notification)
  with_database_reconnect_and_retry do
    notification.save!
  end
end