Class: Fractor::CallbackRegistry

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(debug: false) ⇒ CallbackRegistry

Initialize a new callback registry.

Parameters:

  • debug (Boolean) (defaults to: false)

    Whether to enable debug logging



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_callbacksObject (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_callbacksObject (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

#clearObject

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.

Returns:

  • (Boolean)

    true if error callbacks exist



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.

Returns:

  • (Boolean)

    true if work callbacks exist



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.

Parameters:

  • error_result (WorkResult)

    The error result

  • worker_name (String)

    Name of the worker that encountered the error

  • worker_class (Class)

    The worker class



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.message}" if @debug
  end
end

#process_work_callbacksArray<Work>

Process all work callbacks and return collected work items. Each callback is invoked and any returned work items are collected.

Returns:

  • (Array<Work>)

    Array of work items from all callbacks



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.message}" if @debug
  end
  new_work
end

#register_error_callback { ... } ⇒ Object

Register an error callback. The callback receives (error_result, worker_name, worker_class).

Examples:

registry.register_error_callback do |err, worker, klass|
  logger.error("Error in #{klass}: #{err.error}")
end

Yields:

  • Block that handles errors



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.

Examples:

registry.register_work_source do
  fetch_more_work_from_queue
end

Yields:

  • Block that returns work items or nil/empty array



27
28
29
# File 'lib/fractor/callback_registry.rb', line 27

def register_work_source(&block)
  @work_callbacks << block
end

#sizeHash

Get the total number of registered callbacks.

Returns:

  • (Hash)

    Hash with :work and :error callback counts



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