Class: Fractor::MainLoopHandler
- Inherits:
-
Object
- Object
- Fractor::MainLoopHandler
- 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
Class Method Summary collapse
-
.create(supervisor, debug: false) ⇒ MainLoopHandler
Factory method to create the appropriate MainLoopHandler implementation based on the current Ruby version.
Instance Method Summary collapse
-
#cleanup_ractors_map ⇒ void
Clean up the ractors map after batch processing.
-
#initialize(supervisor, debug: false) ⇒ MainLoopHandler
constructor
A new instance of MainLoopHandler.
-
#initiate_shutdown ⇒ void
Initiate graceful shutdown.
-
#run_loop ⇒ void
Run the main event loop.
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.
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_map ⇒ void
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_shutdown ⇒ void
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_loop ⇒ void
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).
34 35 36 |
# File 'lib/fractor/main_loop_handler.rb', line 34 def run_loop raise NotImplementedError, "Subclasses must implement #run_loop" end |