Class: Fractor::CallbackRegistry
- Inherits:
-
Object
- Object
- Fractor::CallbackRegistry
- Defined in:
- lib/fractor/callback_registry.rb
Overview
Registry for managing work and error callbacks in Supervisor and related classes. Provides a clean interface for registering and invoking callbacks with proper error handling and isolation.
Instance Attribute Summary collapse
-
#error_callbacks ⇒ Object
readonly
Returns the value of attribute error_callbacks.
-
#work_callbacks ⇒ Object
readonly
Returns the value of attribute work_callbacks.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all callbacks.
-
#has_error_callbacks? ⇒ Boolean
Check if there are any error callbacks registered.
-
#has_work_callbacks? ⇒ Boolean
Check if there are any work callbacks registered.
-
#initialize(debug: false) ⇒ CallbackRegistry
constructor
Initialize a new callback registry.
-
#invoke_error_callbacks(error_result, worker_name, worker_class) ⇒ Object
Invoke all error callbacks with the given error context.
-
#process_work_callbacks ⇒ Array<Work>
Process all work callbacks and return collected work items.
-
#register_error_callback { ... } ⇒ Object
Register an error callback.
-
#register_work_source { ... } ⇒ Object
Register a work source callback.
-
#size ⇒ Hash
Get the total number of registered callbacks.
Constructor Details
#initialize(debug: false) ⇒ CallbackRegistry
Initialize a new callback registry.
13 14 15 16 17 |
# File 'lib/fractor/callback_registry.rb', line 13 def initialize(debug: false) @work_callbacks = [] @error_callbacks = [] @debug = debug end |
Instance Attribute Details
#error_callbacks ⇒ Object (readonly)
Returns the value of attribute error_callbacks.
8 9 10 |
# File 'lib/fractor/callback_registry.rb', line 8 def error_callbacks @error_callbacks end |
#work_callbacks ⇒ Object (readonly)
Returns the value of attribute work_callbacks.
8 9 10 |
# File 'lib/fractor/callback_registry.rb', line 8 def work_callbacks @work_callbacks end |
Instance Method Details
#clear ⇒ Object
Clear all callbacks. Useful for cleanup or testing.
91 92 93 94 |
# File 'lib/fractor/callback_registry.rb', line 91 def clear @work_callbacks.clear @error_callbacks.clear end |
#has_error_callbacks? ⇒ Boolean
Check if there are any error callbacks registered.
53 54 55 |
# File 'lib/fractor/callback_registry.rb', line 53 def has_error_callbacks? !@error_callbacks.empty? end |
#has_work_callbacks? ⇒ Boolean
Check if there are any work callbacks registered.
46 47 48 |
# File 'lib/fractor/callback_registry.rb', line 46 def has_work_callbacks? !@work_callbacks.empty? end |
#invoke_error_callbacks(error_result, worker_name, worker_class) ⇒ Object
Invoke all error callbacks with the given error context. Errors in callbacks are caught and logged to prevent cascading failures.
81 82 83 84 85 86 87 |
# File 'lib/fractor/callback_registry.rb', line 81 def invoke_error_callbacks(error_result, worker_name, worker_class) @error_callbacks.each do |callback| callback.call(error_result, worker_name, worker_class) rescue StandardError => e puts "Error in error callback: #{e.}" if @debug end end |
#process_work_callbacks ⇒ Array<Work>
Process all work callbacks and return collected work items. Each callback is invoked and any returned work items are collected.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/fractor/callback_registry.rb', line 61 def process_work_callbacks new_work = [] @work_callbacks.each do |callback| result = callback.call next unless result next if result.empty? new_work.concat(Array(result)) rescue StandardError => e puts "Error in work callback: #{e.}" if @debug end new_work end |
#register_error_callback { ... } ⇒ Object
Register an error callback. The callback receives (error_result, worker_name, worker_class).
39 40 41 |
# File 'lib/fractor/callback_registry.rb', line 39 def register_error_callback(&block) @error_callbacks << block end |
#register_work_source { ... } ⇒ Object
Register a work source callback. The callback should return nil or empty array when no new work is available.
27 28 29 |
# File 'lib/fractor/callback_registry.rb', line 27 def register_work_source(&block) @work_callbacks << block end |
#size ⇒ Hash
Get the total number of registered callbacks.
99 100 101 102 103 104 |
# File 'lib/fractor/callback_registry.rb', line 99 def size { work: @work_callbacks.size, error: @error_callbacks.size, } end |