Class: Fractor::Worker
- Inherits:
-
Object
- Object
- Fractor::Worker
- Defined in:
- lib/fractor/worker.rb
Overview
Base class for defining work processors.
Subclasses must implement the process method.
Direct Known Subclasses
Fractor::Workflow::Helpers::FilterWorker, Fractor::Workflow::Helpers::MapWorker, Fractor::Workflow::Helpers::ReduceWorker, Fractor::Workflow::Helpers::SimpleWorker, Fractor::Workflow::Helpers::ValidationWorker
Class Attribute Summary collapse
-
.input_type_class ⇒ Object
readonly
Returns the value of attribute input_type_class.
-
.output_type_class ⇒ Object
readonly
Returns the value of attribute output_type_class.
-
.worker_timeout ⇒ Object
readonly
Returns the value of attribute worker_timeout.
Class Method Summary collapse
-
.effective_timeout ⇒ Numeric?
Get the effective timeout for this worker.
-
.input_type(klass) ⇒ Object
Declare the input type for this worker.
-
.output_type(klass) ⇒ Object
Declare the output type for this worker.
-
.timeout(seconds) ⇒ Object
Set a timeout for this worker's process method.
Instance Method Summary collapse
-
#initialize(name: nil, **options) ⇒ Worker
constructor
A new instance of Worker.
- #process(work) ⇒ Object
-
#timeout ⇒ Numeric?
Get the timeout for this worker instance.
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, **) @name = name @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_class ⇒ Object (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_class ⇒ Object (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_timeout ⇒ Object (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_timeout ⇒ Numeric?
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.
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.
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.
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.
34 35 36 |
# File 'lib/fractor/worker.rb', line 34 def timeout(seconds) @worker_timeout = seconds end |
Instance Method Details
#process(work) ⇒ Object
73 74 75 76 |
# File 'lib/fractor/worker.rb', line 73 def process(work) raise NotImplementedError, "Subclasses must implement the 'process' method." end |
#timeout ⇒ Numeric?
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.
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 |