class Timmy::Runner

Public Class Methods

new(replay_speed:) click to toggle source
# File lib/timmy/runner.rb, line 27
def initialize(replay_speed:)
  @replay_speed = replay_speed
  @last_replay_time = 0
end
run() click to toggle source
# File lib/timmy/runner.rb, line 8
def run
  ConfigLoader.load

  options = OptionParser.parse
  replay_speed = options[:replay_speed] || @replay_speed
  Logger.set_quiet(options[:quiet]) if options.key?(:quiet)
  Logger.set_precision(options[:precision]) if options.key?(:precision)
  Logger.set_profile(options[:profile]) if options.key?(:profile)

  instance = self.new(replay_speed: replay_speed)

  if command = options[:command]
    instance.stream_command(command)
  else
    instance.consume_stdin()
  end
end
set_replay_speed(speed) click to toggle source
# File lib/timmy/runner.rb, line 4
def set_replay_speed(speed)
  @replay_speed = speed
end

Public Instance Methods

consume_stdin() click to toggle source
# File lib/timmy/runner.rb, line 40
def consume_stdin
  around_run_lines do
    STDIN.each_line do |line|
      next if init_replay_mode(line)

      if @replay_mode
        replay_line(line)
      else
        run_line(line.rstrip)
      end
    end
  end
end
stream_command(command) click to toggle source
# File lib/timmy/runner.rb, line 32
def stream_command(command)
  around_run_lines do
    CommandStreamer.stream(command) do |type, line|
      run_line(line.rstrip, type)
    end
  end
end

Private Instance Methods

around_run_lines() { || ... } click to toggle source
# File lib/timmy/runner.rb, line 67
def around_run_lines
  MasterTimer.start

  yield

  Logger.put_eof unless @replay_mode
  TargetedTimerManager.stop_all
  Logger.finalize
end
init_replay_mode(line) click to toggle source
# File lib/timmy/runner.rb, line 56
def init_replay_mode(line)
  if @replay_mode == nil
    if (match = Logger.match_replay_header(line))
      MasterTimer.start(match[:s].to_f)
      @replay_mode = true
    else
      @replay_mode = false
    end
  end
end
replay_line(line) click to toggle source
# File lib/timmy/runner.rb, line 77
def replay_line(line)
  line_match = Logger.match_replay_line(line)
  line_content = line_match[:content]
  line_time = line_match[:s].to_f
  line_type = (line_match.send(:[], :t) rescue nil) == 'e' ? :stderr : :stdout

  duration = line_time - @last_replay_time
  sleep duration / @replay_speed if @replay_speed
  @last_replay_time = line_time

  MasterTimer.set(line_time)

  run_line(line_content, line_type)
end
run_line(line, type = :stdout) click to toggle source
# File lib/timmy/runner.rb, line 92
def run_line(line, type = :stdout)
  TargetedTimerManager.start_for_line(line)
  Logger.put_output(line, type == :stderr)
  TargetedTimerManager.stop_for_line(line)
end