Teek API Documentation

Unified background work API. Delegates to a mode-specific implementation selected via the mode: parameter.

Modes: - :thread — traditional threading; best for I/O-bound work where the GVL is released during blocking calls. Always available. - :ractor — true parallel execution via Ractor (Ruby 4.x+ only); best for CPU-bound work.

Inherits: Object

Class Methods

background_mode_class(name)

@api private

background_modes

@api private

register_background_mode(name, klass)

@api private

Instance Methods

close

Force-close the worker and associated resources.

Returns self

done?

Returns Boolean

initialize(app, data, mode: :thread, worker: nil, &block)

Parameters
  • app Teek::App — the application instance
  • data Object — data passed to the worker block
  • mode Symbol:thread or :ractor
  • worker Class, nil — optional worker class (must respond to #call(task, data))

Returns BackgroundWork — a new instance of BackgroundWork

@raise ArgumentError if mode is unknown

@yield task, data block executed in the background

@yieldparam task BackgroundThread::BackgroundWork::TaskContext, BackgroundRactor4x::BackgroundWork::TaskContext

@yieldparam data Object

mode

Returns Symbol — the active mode (:thread or :ractor)

on_done(&block)

Returns self

@yield called on the main thread when the worker completes

on_message(&block)

Returns self

@yield msg called on the main thread with custom worker messages

on_progress(&block)

Returns self

@yield value called on the main thread with each result

pause

Pause the worker.

Returns self

paused?

Returns Boolean

resume

Resume a paused worker.

Returns self

send_message(msg)

Send a message to the worker.

Parameters
  • msg Object — any value (must be Ractor-shareable in :ractor mode)

Returns self

start

Explicitly start the worker. Called automatically by #on_progress and #on_done.

Returns self

stop

Request the worker to stop.

Returns self

Attributes

abort_on_error [R]

Returns Boolean — when true, raise on ractor errors instead of warning (default false)

drop_intermediate [R]

Returns Boolean — when true, only the latest progress value per poll cycle is delivered (default true)

poll_ms [R]

Returns Integer — UI poll interval in milliseconds (default 16)

name [R]

Returns String, nil — optional name for this task