Class: Teek::BackgroundRactor4x::BackgroundWork::TaskContext

Inherits:
Object
  • Object
show all
Defined in:
lib/teek/background_ractor4x.rb

Overview

Context object passed to the worker block inside the Ractor. Provides methods for yielding results, sending/receiving messages, and responding to pause/stop signals. :nocov: – TaskContext runs exclusively inside a Ractor; invisible to Coverage.so

Instance Method Summary collapse

Constructor Details

#initialize(output_port, msg_queue) ⇒ TaskContext

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of TaskContext.



367
368
369
370
371
# File 'lib/teek/background_ractor4x.rb', line 367

def initialize(output_port, msg_queue)
  @output_port = output_port
  @msg_queue = msg_queue
  @paused = false
end

Instance Method Details

#check_messageObject?

Non-blocking check for a message from the main thread. Handles built-in control messages (:pause, :resume, :stop) automatically; :stop raises StopIteration.

Returns:

  • (Object, nil)

    the message, or nil if none pending



385
386
387
388
389
390
391
392
# File 'lib/teek/background_ractor4x.rb', line 385

def check_message
  return nil if @msg_queue.empty?
  msg = @msg_queue.pop(true)
  handle_control_message(msg)
  msg
rescue ThreadError
  nil
end

#check_pauseObject

Block while paused, returning immediately if not paused. Call this periodically in long-running loops to honor pause requests.



412
413
414
# File 'lib/teek/background_ractor4x.rb', line 412

def check_pause
  check_pause_loop
end

#send_message(msg) ⇒ Object

Send a custom message back to the main thread. Arrives in the Teek::BackgroundRactor4x::BackgroundWork#on_message callback.

Parameters:

  • msg (Object)

    any Ractor-shareable value



406
407
408
# File 'lib/teek/background_ractor4x.rb', line 406

def send_message(msg)
  @output_port.send([:message, msg])
end

#wait_messageObject

Blocking wait for the next message from the main thread. Handles control messages automatically.

Returns:

  • (Object)

    the message



397
398
399
400
401
# File 'lib/teek/background_ractor4x.rb', line 397

def wait_message
  msg = @msg_queue.pop
  handle_control_message(msg)
  msg
end

#yield(value) ⇒ Object

Send a result to the main thread. Blocks while paused. The value arrives in the Teek::BackgroundRactor4x::BackgroundWork#on_progress callback.

Parameters:

  • value (Object)

    any Ractor-shareable value



376
377
378
379
# File 'lib/teek/background_ractor4x.rb', line 376

def yield(value)
  check_pause_loop
  @output_port.send([:result, value])
end