Class: Rpush::Daemon::Batch

Inherits:
Object
  • Object
show all
Includes:
Reflectable
Defined in:
lib/rpush/daemon/batch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Reflectable

#reflect

Constructor Details

#initialize(notifications) ⇒ Batch

Returns a new instance of Batch.



9
10
11
12
13
14
15
16
# File 'lib/rpush/daemon/batch.rb', line 9

def initialize(notifications)
  @notifications = notifications
  @num_processed = 0
  @delivered = []
  @failed = {}
  @retryable = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#deliveredObject (readonly)

Returns the value of attribute delivered.



6
7
8
# File 'lib/rpush/daemon/batch.rb', line 6

def delivered
  @delivered
end

#failedObject (readonly)

Returns the value of attribute failed.



6
7
8
# File 'lib/rpush/daemon/batch.rb', line 6

def failed
  @failed
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



6
7
8
# File 'lib/rpush/daemon/batch.rb', line 6

def notifications
  @notifications
end

#num_processedObject (readonly)

Returns the value of attribute num_processed.



6
7
8
# File 'lib/rpush/daemon/batch.rb', line 6

def num_processed
  @num_processed
end

#retryableObject (readonly)

Returns the value of attribute retryable.



6
7
8
# File 'lib/rpush/daemon/batch.rb', line 6

def retryable
  @retryable
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rpush/daemon/batch.rb', line 62

def complete?
  @complete == true
end

#describeObject



66
67
68
# File 'lib/rpush/daemon/batch.rb', line 66

def describe
  notifications.map(&:id).join(', ')
end

#mark_delivered(notification) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/rpush/daemon/batch.rb', line 33

def mark_delivered(notification)
  if Rpush.config.batch_storage_updates
    delivered << notification
    Rpush::Daemon.store.mark_delivered(notification, Time.now, :persist => false)
  else
    Rpush::Daemon.store.mark_delivered(notification, Time.now)
    reflect(:notification_delivered, notification)
  end
end

#mark_failed(notification, code, description) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rpush/daemon/batch.rb', line 43

def mark_failed(notification, code, description)
  if Rpush.config.batch_storage_updates
    key = [code, description]
    failed[key] ||= []
    failed[key] << notification
    Rpush::Daemon.store.mark_failed(notification, code, description, Time.now, :persist => false)
  else
    Rpush::Daemon.store.mark_failed(notification, code, description, Time.now)
    reflect(:notification_failed, notification)
  end
end

#mark_retryable(notification, deliver_after) ⇒ Object



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

def mark_retryable(notification, deliver_after)
  if Rpush.config.batch_storage_updates
    retryable[deliver_after] ||= []
    retryable[deliver_after] << notification
    Rpush::Daemon.store.mark_retryable(notification, deliver_after, :persist => false)
  else
    Rpush::Daemon.store.mark_retryable(notification, deliver_after)
    reflect(:notification_will_retry, notification)
  end
end

#notification_dispatchedObject



55
56
57
58
59
60
# File 'lib/rpush/daemon/batch.rb', line 55

def notification_dispatched
  @mutex.synchronize do
    @num_processed += 1
    complete if @num_processed >= @notifications.size
  end
end

#num_notificationsObject



18
19
20
# File 'lib/rpush/daemon/batch.rb', line 18

def num_notifications
  @notifications.size
end