Class: Fractor::MainLoopHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/fractor/main_loop_handler.rb

Overview

Handles the main event loop for a Supervisor. Responsible for processing Ractor messages and coordinating work distribution.

This class extracts the main loop logic from Supervisor to follow the Single Responsibility Principle.

Direct Known Subclasses

MainLoopHandler3, MainLoopHandler4

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(supervisor, debug: false) ⇒ MainLoopHandler

Returns a new instance of MainLoopHandler.



10
11
12
13
14
# File 'lib/fractor/main_loop_handler.rb', line 10

def initialize(supervisor, debug: false)
  @supervisor = supervisor
  @debug = debug
  @shutting_down = false
end

Class Method Details

.create(supervisor, debug: false) ⇒ MainLoopHandler

Factory method to create the appropriate MainLoopHandler implementation based on the current Ruby version.

Parameters:

  • supervisor (Fractor::Supervisor)

    The supervisor instance

  • debug (Boolean) (defaults to: false)

    Whether debug mode is enabled

Returns:



22
23
24
25
26
27
28
# File 'lib/fractor/main_loop_handler.rb', line 22

def self.create(supervisor, debug: false)
  if Fractor::RUBY_4_0_OR_HIGHER
    MainLoopHandler4.new(supervisor, debug: debug)
  else
    MainLoopHandler3.new(supervisor, debug: debug)
  end
end

Instance Method Details

#cleanup_ractors_mapvoid

This method returns an undefined value.

Clean up the ractors map after batch processing. This is critical on Windows Ruby 3.4 where workers may not respond to shutdown if they're stuck in Ractor.receive.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fractor/main_loop_handler.rb', line 43

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
  # On Windows Ruby 3.4, some workers may be stuck in Ractor.receive
  # and will never acknowledge shutdown - we must not block on them
  ractors_map.clear

  # Force garbage collection to help clean up orphaned ractors
  # This is a workaround for Ruby 3.4 Windows where orphaned ractors
  # can block creation of new ractors in subsequent tests
  GC.start
  puts "Ractors map cleared and GC forced." if @debug
end

#initiate_shutdownvoid

This method returns an undefined value.

Initiate graceful shutdown. Sets the shutting_down flag to allow the main loop to process shutdown acknowledgments before exiting.



66
67
68
69
# File 'lib/fractor/main_loop_handler.rb', line 66

def initiate_shutdown
  @shutting_down = true
  puts "Main loop shutdown initiated" if @debug
end

#run_loopvoid

This method returns an undefined value.

Run the main event loop. This method blocks until all work is processed (batch mode) or until stopped (continuous mode).

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/fractor/main_loop_handler.rb', line 34

def run_loop
  raise NotImplementedError, "Subclasses must implement #run_loop"
end