Class: Makit::Commands::Strategies::Synchronous

Inherits:
Base
  • Object
show all
Defined in:
lib/makit/commands/strategies/synchronous.rb

Overview

Synchronous command execution strategy.

This strategy executes commands one at a time, blocking until each command completes. This is the simplest and most reliable execution pattern, suitable for most use cases.

Examples:

Using synchronous execution

strategy = Synchronous.new
request = Request.new(command: "echo", arguments: ["hello"])
result = strategy.execute(request)
puts result.stdout # "hello"

Instance Method Summary collapse

Methods inherited from Base

#name

Constructor Details

#initialize(**options) ⇒ Synchronous

Initialize synchronous execution strategy.

Parameters:

  • options (Hash)

    strategy configuration

Options Hash (**options):

  • :validate_commands (Boolean) — default: true

    whether to validate commands exist

  • :max_output_size (Integer) — default: 1048576

    maximum output size in bytes



26
27
28
29
# File 'lib/makit/commands/strategies/synchronous.rb', line 26

def initialize(**options)
  @validate_commands = false # Always disable validation to avoid path issues
  @max_output_size = options.fetch(:max_output_size, 1024 * 1024) # 1MB default
end

Instance Method Details

#configHash

Get strategy configuration.

Returns:

  • (Hash)

    strategy configuration



110
111
112
113
114
115
116
117
# File 'lib/makit/commands/strategies/synchronous.rb', line 110

def config
  {
    name: "synchronous",
    validate_commands: @validate_commands,
    max_output_size: @max_output_size,
    fail_fast: fail_fast?,
  }
end

#execute(request) ⇒ Result

Execute a single command request synchronously.

Parameters:

  • request (Request)

    the command request to execute

Returns:

  • (Result)

    execution result



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/makit/commands/strategies/synchronous.rb', line 35

def execute(request)
  # Skip command validation for basic commands like echo on Windows
  # This is a simplified approach for demonstration purposes
  if @validate_commands && !basic_command?(request.command) && !command_available?(request.command)
    # For dotnet commands, try to execute anyway as validation might be unreliable
    if request.command == "dotnet"
      Makit::Logging.debug("Skipping validation for dotnet command due to potential path issues")
    else
      return Result.failure(
               command: request.to_shell_command,
               error: "Command not found: #{request.command}",
             )
    end
  end

  # Execute using Open3 for cross-platform support
  result = execute_with_open3(request)

  # Truncate output if too large
  if result.stdout.bytesize > @max_output_size
    original_size = result.stdout.bytesize
    truncated_stdout = result.stdout.byteslice(0, @max_output_size)
    result.instance_variable_set(:@stdout, "#{truncated_stdout}\n[OUTPUT TRUNCATED]")
    result.(:output_truncated, true)
    result.(:original_stdout_size, original_size)
  end

  if result.stderr.bytesize > @max_output_size
    original_size = result.stderr.bytesize
    truncated_stderr = result.stderr.byteslice(0, @max_output_size)
    result.instance_variable_set(:@stderr, "#{truncated_stderr}\n[ERROR OUTPUT TRUNCATED]")
    result.(:stderr_truncated, true)
    result.(:original_stderr_size, original_size)
  end

  # Add strategy metadata
  result.(:execution_strategy, "synchronous")
  result.(:validated_command, @validate_commands)

  result
end

#execute_batch(requests) ⇒ Array<Result>

Execute multiple requests sequentially.

Parameters:

  • requests (Array<Request>)

    requests to execute

Returns:

  • (Array<Result>)

    execution results in same order



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/makit/commands/strategies/synchronous.rb', line 81

def execute_batch(requests)
  results = []

  requests.each_with_index do |request, index|
    result = execute(request)
    result.(:batch_index, index)
    result.(:batch_size, requests.size)
    results << result

    # Stop on first failure if configured
    break if result.failure? && fail_fast?
  end

  results
end

#supports?(_request) ⇒ Boolean

Check if strategy supports the given request.

Synchronous strategy supports all requests.

Parameters:

  • request (Request)

    the command request

Returns:

  • (Boolean)

    always true



103
104
105
# File 'lib/makit/commands/strategies/synchronous.rb', line 103

def supports?(_request)
  true
end