Class: Fractor::MainLoopHandler4

Inherits:
MainLoopHandler show all
Defined in:
lib/fractor/main_loop_handler4.rb

Overview

Ruby 4.0+ specific implementation of MainLoopHandler. Uses Ractor::Port for worker communication.

Instance Method Summary collapse

Methods inherited from MainLoopHandler

create, #initialize, #initiate_shutdown

Constructor Details

This class inherits a constructor from Fractor::MainLoopHandler

Instance Method Details

#cleanup_ractors_mapvoid

This method returns an undefined value.

Clean up the ractors map after batch processing. In Ruby 4.0, we simply clear the map to allow garbage collection. The main loop already attempted to shut down workers properly.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fractor/main_loop_handler4.rb', line 51

def cleanup_ractors_map
  return if ractors_map.empty?

  puts "Cleaning up ractors map (#{ractors_map.size} entries)..." if @debug

  # Simply clear the map without trying to interact with ractors
  # The main loop already attempted to shut down workers properly
  ractors_map.clear

  # Force garbage collection to help clean up orphaned ractors
  GC.start
  puts "Ractors map cleared and GC forced." if @debug
end

#run_loopObject

Run the main event loop for Ruby 4.0+.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fractor/main_loop_handler4.rb', line 10

def run_loop
  loop do
    # Build mapping of response ports to workers for message routing
    # REBUILD ON EACH ITERATION to handle terminated workers
    port_to_worker = build_port_to_worker_map

    processed_count = get_processed_count

    # Check loop termination condition
    break unless should_continue_running?(processed_count)

    log_processing_status(processed_count)

    active_items = get_active_items

    # Check for new work from callbacks if in continuous mode
    process_work_callbacks if continuous_mode? && @supervisor.callback_registry.has_work_callbacks?

    # Handle edge cases - break if edge case handler indicates we should
    next if handle_edge_cases_with_ports(active_items, port_to_worker,
                                         processed_count)

    # Wait for next message from any active ractor or port
    ready_item, message = select_from_mixed(active_items, port_to_worker)
    next unless ready_item && message

    # Process the received message
    process_message_40(ready_item, message, port_to_worker)
  end

  puts "Main loop finished." if @debug

  # Clean up ractors map after batch mode completion
  cleanup_ractors_map unless continuous_mode?
end