Class: Makit::Commands::Strategies::Factory
- Inherits:
-
Object
- Object
- Makit::Commands::Strategies::Factory
- Defined in:
- lib/makit/commands/strategies/factory.rb
Overview
Factory for creating command execution strategies with fallback support
This factory allows easy switching between different execution strategies (ChildProcess, Open3, etc.) with automatic fallback to Open3 if the preferred strategy fails to load or execute.
Constant Summary collapse
- STRATEGIES =
Available strategy types
{ "childprocess" => "ChildProcess", "open3" => "Synchronous", "auto" => "AutoDetect", }.freeze
Class Method Summary collapse
-
.childprocess_available? ⇒ Boolean
Check if ChildProcess strategy is available.
-
.create(options = {}) ⇒ Strategies::Base
Get the configured strategy instance.
-
.create_auto_detect_strategy(options) ⇒ Strategies::Base
Create auto-detect strategy that tries ChildProcess first.
-
.create_childprocess_strategy(options) ⇒ Strategies::Base
Create ChildProcess strategy with fallback.
-
.create_open3_strategy(options) ⇒ Strategies::Base
Create Open3 strategy.
-
.determine_strategy_type ⇒ String
Get the current strategy type from environment or default.
-
.strategy_info ⇒ Hash
Get information about available strategies.
Class Method Details
.childprocess_available? ⇒ Boolean
Check if ChildProcess strategy is available
125 126 127 128 129 130 131 132 |
# File 'lib/makit/commands/strategies/factory.rb', line 125 def self.childprocess_available? begin require "childprocess" true rescue LoadError false end end |
.create(options = {}) ⇒ Strategies::Base
Get the configured strategy instance
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/makit/commands/strategies/factory.rb', line 36 def self.create( = {}) strategy_type = determine_strategy_type begin case strategy_type when "childprocess" create_childprocess_strategy() when "open3" create_open3_strategy() when "auto" create_auto_detect_strategy() else Makit::Logging.warn("Unknown strategy '#{strategy_type}', falling back to Open3") if defined?(Makit::Logging) create_open3_strategy() end rescue => e # Ultimate fallback - create basic synchronous strategy Makit::Logging.warn("Failed to create strategy: #{e.}") if defined?(Makit::Logging) Synchronous.new end end |
.create_auto_detect_strategy(options) ⇒ Strategies::Base
Create auto-detect strategy that tries ChildProcess first
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/makit/commands/strategies/factory.rb', line 98 def self.create_auto_detect_strategy() # Always use Open3 for now to avoid circular dependency issues begin Makit::Logging.debug("Using Open3 strategy") if defined?(Makit::Logging) Synchronous.new(**) rescue => e # Fallback to basic strategy if there are any issues Makit::Logging.warn("Failed to create Open3 strategy: #{e.}") if defined?(Makit::Logging) Synchronous.new end end |
.create_childprocess_strategy(options) ⇒ Strategies::Base
Create ChildProcess strategy with fallback
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/makit/commands/strategies/factory.rb', line 75 def self.create_childprocess_strategy() begin require_relative "child_process" ChildProcess.new(**) rescue LoadError, StandardError => e Makit::Logging.warn("Failed to load ChildProcess strategy: #{e.}") Makit::Logging.info("Falling back to Open3 strategy") create_open3_strategy() end end |
.create_open3_strategy(options) ⇒ Strategies::Base
Create Open3 strategy
90 91 92 |
# File 'lib/makit/commands/strategies/factory.rb', line 90 def self.create_open3_strategy() Synchronous.new(**) end |
.determine_strategy_type ⇒ String
Get the current strategy type from environment or default
61 62 63 64 65 66 67 68 69 |
# File 'lib/makit/commands/strategies/factory.rb', line 61 def self.determine_strategy_type env_strategy = ENV["MAKIT_STRATEGY"]&.downcase if env_strategy && STRATEGIES.key?(env_strategy) env_strategy else "auto" # Default to auto-detect end end |
.strategy_info ⇒ Hash
Get information about available strategies
113 114 115 116 117 118 119 120 |
# File 'lib/makit/commands/strategies/factory.rb', line 113 def self.strategy_info { current: determine_strategy_type, available: STRATEGIES.keys, childprocess_available: childprocess_available?, open3_available: true, # Always available } end |