Class: RubyReactor::Map::Dispatcher

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/ruby_reactor/map/dispatcher.rb

Class Method Summary collapse

Methods included from Helpers

apply_collect_block, build_element_inputs, load_parent_context_from_storage, resolve_reactor_class, resume_parent_execution

Class Method Details

.dispatch_batch(source, arguments, parent_context, storage) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_reactor/map/dispatcher.rb', line 63

def self.dispatch_batch(source, arguments, parent_context, storage)
  map_id = arguments[:map_id]
  reactor_class_name = arguments[:parent_reactor_class_name]

  # Fail Fast Check
  if arguments[:fail_fast]
    failed_context_id = storage.retrieve_map_failed_context_id(map_id, reactor_class_name)
    return if failed_context_id
  end

  batch_size = arguments[:batch_size] || source.size # Default to all if no batch_size (async=true only)

  # Atomically reserve a batch
  new_offset = storage.increment_map_offset(map_id, batch_size, reactor_class_name)
  current_offset = new_offset - batch_size

  batch_elements = if source.is_a?(Array)
                     source.slice(current_offset, batch_size) || []
                   elsif source.respond_to?(:offset) && source.respond_to?(:limit)
                     # Optimized for ActiveRecord and similar query builders
                     source.offset(current_offset).limit(batch_size).to_a
                   else
                     # Fallback for generic Enumerable
                     # This is inefficient for huge sets if not Array, but compliant
                     source.drop(current_offset).take(batch_size)
                   end

  return if batch_elements.empty?

  # Queue Jobs
  queue_options = {
    map_id: map_id,
    arguments: arguments,
    context: parent_context,
    reactor_class_info: resolve_reactor_class_info(arguments, parent_context),
    step_name: arguments[:step_name]
  }

  batch_elements.each_with_index do |element, i|
    absolute_index = current_offset + i
    queue_element_job(element, absolute_index, queue_options)
  end
end

.element_at(source, index) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/ruby_reactor/map/dispatcher.rb', line 139

def self.element_at(source, index)
  if source.is_a?(Array)
    source[index]
  elsif source.respond_to?(:offset) && source.respond_to?(:limit)
    source.offset(index).limit(1).to_a.first
  else
    source.drop(index).first
  end
end

.initialize_map_metadata(arguments, storage) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ruby_reactor/map/dispatcher.rb', line 34

def self.(arguments, storage)
  map_id = arguments[:map_id]
  reactor_class_name = arguments[:parent_reactor_class_name]

  # Reset or set initial offset. Use NX to act as a mutex/guard against duplicate initialization.
  storage.set_map_offset_if_not_exists(map_id, 0, reactor_class_name)
end

.perform(arguments) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_reactor/map/dispatcher.rb', line 8

def self.perform(arguments)
  arguments = arguments.transform_keys(&:to_sym)
  parent_reactor_class_name = arguments[:parent_reactor_class_name]

  storage = RubyReactor.configuration.storage_adapter

  # Load parent context to resolve source
  parent_context = load_parent_context_from_storage(
    arguments[:parent_context_id],
    parent_reactor_class_name,
    storage
  )

  # Initialize metadata if first run
  (arguments, storage) unless arguments[:continuation]

  # Resolve Source
  # We need to resolve the source to know what we are iterating.
  # Strict "Array Only" rule means we expect an Array-like object or we handle the
  # "Query Builder" result if user used it.
  source = resolve_source(arguments, parent_context)

  # Dispatch next batch
  dispatch_batch(source, arguments, parent_context, storage)
end

.queue_element_job(element, index, options) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ruby_reactor/map/dispatcher.rb', line 149

def self.queue_element_job(element, index, options)
  arguments = options[:arguments]
  context = options[:context]

  # Resolve mappings
  mappings_template = arguments[:argument_mappings]

  # Fallback: look up from step config if missing (e.g. called from ElementExecutor)
  if mappings_template.nil? && context
    step_name = options[:step_name] || arguments[:step_name]
    step_config = context.reactor_class.steps[step_name.to_sym]
    mappings_template = step_config.arguments[:argument_mappings]
  end

  mappings = if mappings_template.respond_to?(:resolve)
               mappings_template.resolve(context)
             else
               mappings_template || {}
             end

  # Fix for weird structure observed in fallback (wrapped in :source -> Template::Value)
  if mappings.key?(:source) && mappings[:source].respond_to?(:value) && mappings[:source].value.is_a?(Hash)
    mappings = mappings[:source].value
  end

  mapped_inputs = build_element_inputs(mappings, context, element)
  serialized_inputs = ContextSerializer.serialize_value(mapped_inputs)

  RubyReactor.configuration.async_router.perform_map_element_async(
    map_id: options[:map_id],
    element_id: "#{options[:map_id]}:#{index}",
    index: index,
    serialized_inputs: serialized_inputs,
    reactor_class_info: options[:reactor_class_info],
    strict_ordering: arguments[:strict_ordering],
    parent_context_id: context.context_id,
    parent_reactor_class_name: context.reactor_class.name,
    step_name: options[:step_name].to_s,
    batch_size: arguments[:batch_size], # Passed to worker so it knows to trigger next batch?
    fail_fast: arguments[:fail_fast]
  )
end

.requeue_index(map_meta, index) ⇒ Object

Re-dispatch a SPECIFIC index whose result slot is missing (Phase 5c, used by the map sweeper). Index-driven rather than offset-driven: resolve the source from the stored parent context and pick source. Idempotent because store_map_result HSETs by index — a re-run overwrites slot ‘index`, never duplicates.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ruby_reactor/map/dispatcher.rb', line 112

def self.requeue_index(map_meta, index)
  storage = RubyReactor.configuration.storage_adapter
  parent_class_name = map_meta["parent_reactor_class_name"]
  parent_context = load_parent_context_from_storage(map_meta["parent_context_id"], parent_class_name, storage)

  arguments = {
    map_id: map_meta["map_id"],
    step_name: map_meta["step_name"],
    strict_ordering: map_meta["strict_ordering"],
    parent_context_id: map_meta["parent_context_id"],
    parent_reactor_class_name: parent_class_name,
    fail_fast: map_meta["fail_fast"],
    batch_size: map_meta["batch_size"]
  }

  source = resolve_source(arguments, parent_context)
  element = element_at(source, index)

  queue_element_job(element, index, {
                      map_id: map_meta["map_id"],
                      arguments: arguments,
                      context: parent_context,
                      reactor_class_info: map_meta["reactor_class_info"],
                      step_name: map_meta["step_name"]
                    })
end

.resolve_reactor_class_info(arguments, context) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/ruby_reactor/map/dispatcher.rb', line 192

def self.resolve_reactor_class_info(arguments, context)
  mapped_reactor_class = arguments[:mapped_reactor_class]
  step_name = arguments[:step_name]

  if mapped_reactor_class.respond_to?(:name)
    { "type" => "class", "name" => mapped_reactor_class.name }
  else
    { "type" => "inline", "parent" => context.reactor_class.name, "step" => step_name.to_s }
  end
end

.resolve_source(arguments, context) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ruby_reactor/map/dispatcher.rb', line 42

def self.resolve_source(arguments, context)
  # Arguments has :source which is a Template::Input or similar.
  # We need to resolve it against the context.
  source_template = arguments[:source]

  # Fallback: look up from step config if missing (e.g. called from ElementExecutor)
  if source_template.nil? && context
    step_name = arguments[:step_name]
    step_config = context.reactor_class.steps[step_name.to_sym]
    source_template = step_config.arguments[:source][:source]
  end

  # If source is packaged in arguments as a value (deserialized)
  return source_template if source_template.is_a?(Array)

  # Resolve template
  return source_template.resolve(context) if source_template.respond_to?(:resolve)

  source_template
end