Class: Rpush::Daemon::Batch

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_info, #log_warn

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.



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

def delivered
  @delivered
end

#failedObject (readonly)

Returns the value of attribute failed.



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

def failed
  @failed
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



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

def notifications
  @notifications
end

#num_processedObject (readonly)

Returns the value of attribute num_processed.



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

def num_processed
  @num_processed
end

#retryableObject (readonly)

Returns the value of attribute retryable.



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

def retryable
  @retryable
end

Instance Method Details

#all_processedObject



95
96
97
98
99
100
# File 'lib/rpush/daemon/batch.rb', line 95

def all_processed
  @mutex.synchronize do
    @num_processed = @notifications.size
    complete
  end
end

#complete?Boolean

Returns:

  • (Boolean)


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

def complete?
  @complete == true
end

#each_delivered(&blk) ⇒ Object



26
27
28
# File 'lib/rpush/daemon/batch.rb', line 26

def each_delivered(&blk)
  @delivered.each(&blk)
end

#each_notification(&blk) ⇒ Object



22
23
24
# File 'lib/rpush/daemon/batch.rb', line 22

def each_notification(&blk)
  @notifications.each(&blk)
end

#mark_all_deliveredObject



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

def mark_all_delivered
  @mutex.synchronize do
    @delivered = @notifications
  end

  each_notification do |notification|
    Rpush::Daemon.store.mark_delivered(notification, Time.now, persist: false)
  end
end

#mark_all_failed(code, message) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/rpush/daemon/batch.rb', line 78

def mark_all_failed(code, message)
  key = [code, message]
  @mutex.synchronize do
    @failed[key] = @notifications
  end
  each_notification do |notification|
    Rpush::Daemon.store.mark_failed(notification, code, message, Time.now, persist: false)
  end
end

#mark_all_retryable(deliver_after, error) ⇒ Object



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

def mark_all_retryable(deliver_after, error)
  retryable_count = 0

  each_notification do |notification|
    next if notification.delivered || notification.failed

    retryable_count += 1
    mark_retryable(notification, deliver_after)
  end

  log_warn("Will retry #{retryable_count} of #{@notifications.size} notifications after #{deliver_after.strftime('%Y-%m-%d %H:%M:%S')} due to error (#{error.class.name}, #{error.message})")
end

#mark_delivered(notification) ⇒ Object



52
53
54
55
56
57
# File 'lib/rpush/daemon/batch.rb', line 52

def mark_delivered(notification)
  @mutex.synchronize do
    @delivered << notification
  end
  Rpush::Daemon.store.mark_delivered(notification, Time.now, persist: false)
end

#mark_failed(notification, code, description) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/rpush/daemon/batch.rb', line 69

def mark_failed(notification, code, description)
  key = [code, description]
  @mutex.synchronize do
    @failed[key] ||= []
    @failed[key] << notification
  end
  Rpush::Daemon.store.mark_failed(notification, code, description, Time.now, persist: false)
end

#mark_retryable(notification, deliver_after) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/rpush/daemon/batch.rb', line 30

def mark_retryable(notification, deliver_after)
  @mutex.synchronize do
    @retryable[deliver_after] ||= []
    @retryable[deliver_after] << notification
  end

  Rpush::Daemon.store.mark_retryable(notification, deliver_after, persist: false)
end

#notification_processedObject



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

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