Class: RubynCode::Background::Notifier
- Inherits:
-
Object
- Object
- RubynCode::Background::Notifier
- 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
-
#drain ⇒ Array
Drains all pending notifications in a single atomic operation.
-
#initialize ⇒ Notifier
constructor
A new instance of Notifier.
-
#pending? ⇒ Boolean
Returns true if there are notifications waiting.
-
#push(notification) ⇒ void
Enqueues a notification.
Constructor Details
#initialize ⇒ Notifier
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
#drain ⇒ Array
Drains all pending notifications in a single atomic operation. Returns an empty array if nothing is pending.
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.
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.
18 19 20 |
# File 'lib/rubyn_code/background/notifier.rb', line 18 def push(notification) @queue.push(notification) end |