Class: Push::Daemon::DeliveryQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/push/daemon/delivery_queue.rb

Defined Under Namespace

Classes: WakeupError

Instance Method Summary collapse

Constructor Details

#initializeDeliveryQueue

Returns a new instance of DeliveryQueue.



5
6
7
8
9
10
# File 'lib/push/daemon/delivery_queue.rb', line 5

def initialize
  @num_notifications = 0
  @queue = []
  @waiting = []
  @mutex = Mutex.new
end

Instance Method Details

#notification_processedObject



23
24
25
# File 'lib/push/daemon/delivery_queue.rb', line 23

def notification_processed
  @mutex.synchronize { @num_notifications -= 1 }
end

#notifications_processed?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/push/daemon/delivery_queue.rb', line 27

def notifications_processed?
  @mutex.synchronize { @num_notifications == 0 }
end

#popObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/push/daemon/delivery_queue.rb', line 45

def pop
  @mutex.synchronize do
    while true
      if @queue.empty?
        @waiting.push Thread.current
        @mutex.sleep
      else
        return @queue.shift
      end
    end
  end
end

#push(notification) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/push/daemon/delivery_queue.rb', line 31

def push(notification)
  @mutex.synchronize do
    @num_notifications += 1
    @queue.push(notification)

    begin
      t = @waiting.shift
      t.wakeup if t
    rescue ThreadError
      retry
    end
  end
end

#sizeObject



19
20
21
# File 'lib/push/daemon/delivery_queue.rb', line 19

def size
  @mutex.synchronize { @queue.size }
end

#wakeup(thread) ⇒ Object



12
13
14
15
16
17
# File 'lib/push/daemon/delivery_queue.rb', line 12

def wakeup(thread)
  @mutex.synchronize do
    t = @waiting.delete(thread)
    t.raise WakeupError if t
  end
end