Class: Rapns::Daemon::Store::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
Reconnectable
Defined in:
lib/rapns/daemon/store/active_record.rb,
lib/rapns/daemon/store/active_record/reconnectable.rb

Defined Under Namespace

Modules: Reconnectable

Constant Summary

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



89
90
91
# File 'lib/rapns/daemon/store/active_record.rb', line 89

def after_daemonize
  reconnect_database
end

#create_apns_feedback(failed_at, device_token, app) ⇒ Object



69
70
71
72
73
74
# File 'lib/rapns/daemon/store/active_record.rb', line 69

def create_apns_feedback(failed_at, device_token, app)
  with_database_reconnect_and_retry do
    Rapns::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



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rapns/daemon/store/active_record.rb', line 76

def create_gcm_notification(attrs, data, registration_ids, deliver_after, app)
  with_database_reconnect_and_retry do
    notification = Rapns::Gcm::Notification.new
    notification.assign_attributes(attrs)
    notification.data = data
    notification.registration_ids = registration_ids
    notification.deliver_after = deliver_after
    notification.app = app
    notification.save!
    notification
  end
end

#deliverable_notifications(apps) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/rapns/daemon/store/active_record.rb', line 11

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

#mark_batch_delivered(notifications) ⇒ Object



43
44
45
46
47
48
# File 'lib/rapns/daemon/store/active_record.rb', line 43

def mark_batch_delivered(notifications)
  ids = notifications.map(&:id)
  with_database_reconnect_and_retry do
    Rapns::Notification.where(:id => ids).update_all(['delivered = true, delivered_at = ?', Time.now])
  end
end

#mark_batch_failed(notifications, code, description) ⇒ Object



62
63
64
65
66
67
# File 'lib/rapns/daemon/store/active_record.rb', line 62

def mark_batch_failed(notifications, code, description)
  ids = notifications.map(&:id)
  with_database_reconnect_and_retry do
    Rapns::Notification.where(:id => ids).update_all(['delivered = false, delivered_at = NULL, failed = true, failed_at = ?, error_code = ?, error_description = ?', Time.now, code, description])
  end
end

#mark_batch_retryable(notifications, deliver_after) ⇒ Object



28
29
30
31
32
33
# File 'lib/rapns/daemon/store/active_record.rb', line 28

def mark_batch_retryable(notifications, deliver_after)
  ids = notifications.map(&:id)
  with_database_reconnect_and_retry do
    Rapns::Notification.where(:id => ids).update_all(['retries = retries + 1, deliver_after = ?', deliver_after])
  end
end

#mark_delivered(notification) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rapns/daemon/store/active_record.rb', line 35

def mark_delivered(notification)
  with_database_reconnect_and_retry do
    notification.delivered = true
    notification.delivered_at = Time.now
    notification.save!(:validate => false)
  end
end

#mark_failed(notification, code, description) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rapns/daemon/store/active_record.rb', line 50

def mark_failed(notification, code, description)
  with_database_reconnect_and_retry do
    notification.delivered = false
    notification.delivered_at = nil
    notification.failed = true
    notification.failed_at = Time.now
    notification.error_code = code
    notification.error_description = description
    notification.save!(:validate => false)
  end
end

#mark_retryable(notification, deliver_after) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/rapns/daemon/store/active_record.rb', line 20

def mark_retryable(notification, deliver_after)
  with_database_reconnect_and_retry do
    notification.retries += 1
    notification.deliver_after = deliver_after
    notification.save!(:validate => false)
  end
end