Class: RubynCode::Background::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/background/notifier.rb

Overview

Thread-safe notification queue for background job completions. Uses Ruby’s stdlib Queue which is already thread-safe for push/pop, but we guard drain with a mutex to prevent interleaved partial drains.

Instance Method Summary collapse

Constructor Details

#initializeNotifier

Returns a new instance of Notifier.



9
10
11
12
# File 'lib/rubyn_code/background/notifier.rb', line 9

def initialize
  @queue = Queue.new
  @drain_mutex = Mutex.new
end

Instance Method Details

#drainArray

Drains all pending notifications in a single atomic operation. Returns an empty array if nothing is pending.

Returns:

  • (Array)

    all pending notifications



26
27
28
29
30
31
32
# File 'lib/rubyn_code/background/notifier.rb', line 26

def drain
  @drain_mutex.synchronize do
    notifications = []
    notifications << @queue.pop until @queue.empty?
    notifications
  end
end

#pending?Boolean

Returns true if there are notifications waiting.

Returns:

  • (Boolean)


37
38
39
# File 'lib/rubyn_code/background/notifier.rb', line 37

def pending?
  !@queue.empty?
end

#push(notification) ⇒ void

This method returns an undefined value.

Enqueues a notification.

Parameters:

  • notification (Hash, String, Object)

    arbitrary notification payload



18
19
20
# File 'lib/rubyn_code/background/notifier.rb', line 18

def push(notification)
  @queue.push(notification)
end