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.



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

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

#all_processedObject



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

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

#complete?Boolean

Returns:

  • (Boolean)


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

def complete?
  @complete == true
end

#each_delivered(&blk) ⇒ Object



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

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

#each_notification(&blk) ⇒ Object



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

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

#mark_all_deliveredObject



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

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



62
63
64
65
66
67
68
69
70
# File 'lib/rpush/daemon/batch.rb', line 62

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_delivered(notification) ⇒ Object



37
38
39
40
41
42
# File 'lib/rpush/daemon/batch.rb', line 37

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



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

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



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

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



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

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