Class: Prosody::CancellationToken

Inherits:
Object
  • Object
show all
Defined in:
lib/prosody/processor.rb

Overview

Provides a mechanism for canceling asynchronous tasks.

This class implements a simple cancellation mechanism using a Ruby Queue, allowing tasks to be safely canceled while they’re in progress. Each token maintains its own queue for signaling cancellation.

Instance Method Summary collapse

Constructor Details

#initializeCancellationToken

Creates a new cancellation token with an internal queue for signaling.



16
17
18
# File 'lib/prosody/processor.rb', line 16

def initialize
  @queue = Queue.new
end

Instance Method Details

#cancelObject

Signals that the associated task should be canceled.

This method pushes a cancellation signal to the internal queue, which will wake up any threads waiting on #wait.



24
25
26
# File 'lib/prosody/processor.rb', line 24

def cancel
  @queue.push(:cancel)
end

#waitBoolean

Blocks until cancellation is requested.

This method blocks the current thread until the token is canceled by another thread calling #cancel.

Returns:

  • (Boolean)

    Always returns true after cancellation is received



34
35
36
37
# File 'lib/prosody/processor.rb', line 34

def wait
  @queue.pop
  true
end