Class: Fractor::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/fractor/worker.rb

Overview

Base class for defining work processors. Subclasses must implement the process method.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, **options) ⇒ Worker

Returns a new instance of Worker.



62
63
64
65
66
67
68
69
70
71
# File 'lib/fractor/worker.rb', line 62

def initialize(name: nil, **options)
  @name = name
  @options = options
  # If timeout is not provided or is nil, fall back to class-level timeout
  # Note: This must only be called from the main ractor, not from within worker ractors.
  # In the ractor context, timeout should always be passed explicitly.
  if !@options.key?(:timeout) || @options[:timeout].nil?
    @options[:timeout] = self.class.worker_timeout
  end
end

Class Attribute Details

.input_type_classObject (readonly)

Returns the value of attribute input_type_class.



10
11
12
# File 'lib/fractor/worker.rb', line 10

def input_type_class
  @input_type_class
end

.output_type_classObject (readonly)

Returns the value of attribute output_type_class.



10
11
12
# File 'lib/fractor/worker.rb', line 10

def output_type_class
  @output_type_class
end

.worker_timeoutObject (readonly)

Returns the value of attribute worker_timeout.



10
11
12
# File 'lib/fractor/worker.rb', line 10

def worker_timeout
  @worker_timeout
end

Class Method Details

.effective_timeoutNumeric?

Get the effective timeout for this worker. Returns the worker-specific timeout, or the global default if not set. Note: This method accesses Fractor.config and should be called from the main ractor context, not from within worker ractors.

Returns:

  • (Numeric, nil)

    Timeout in seconds, or nil if not configured



44
45
46
47
48
49
# File 'lib/fractor/worker.rb', line 44

def effective_timeout
  @worker_timeout || begin
    # Access config safely - this must be called from main ractor
    Fractor.config.default_worker_timeout
  end
end

.input_type(klass) ⇒ Object

Declare the input type for this worker. Used by the workflow system to validate data flow.

Parameters:

  • klass (Class)

    A Lutaml::Model::Serializable subclass



16
17
18
19
# File 'lib/fractor/worker.rb', line 16

def input_type(klass)
  validate_type_class!(klass, "input_type")
  @input_type_class = klass
end

.output_type(klass) ⇒ Object

Declare the output type for this worker. Used by the workflow system to validate data flow.

Parameters:

  • klass (Class)

    A Lutaml::Model::Serializable subclass



25
26
27
28
# File 'lib/fractor/worker.rb', line 25

def output_type(klass)
  validate_type_class!(klass, "output_type")
  @output_type_class = klass
end

.timeout(seconds) ⇒ Object

Set a timeout for this worker's process method. If the process method takes longer than this, a Timeout::Error will be raised.

Parameters:

  • seconds (Numeric)

    Timeout in seconds



34
35
36
# File 'lib/fractor/worker.rb', line 34

def timeout(seconds)
  @worker_timeout = seconds
end

Instance Method Details

#process(work) ⇒ Object

Raises:

  • (NotImplementedError)


73
74
75
76
# File 'lib/fractor/worker.rb', line 73

def process(work)
  raise NotImplementedError,
        "Subclasses must implement the 'process' method."
end

#timeoutNumeric?

Get the timeout for this worker instance. Uses the class-level timeout if not overridden. Note: This method is safe to call from within ractors as it only accesses instance variables that were set at initialization time.

Returns:

  • (Numeric, nil)

    Timeout in seconds, or nil if not configured



84
85
86
87
88
# File 'lib/fractor/worker.rb', line 84

def timeout
  # The timeout is always set at initialization time via options,
  # so we can safely access it from within the ractor
  @options[:timeout]
end