Class: SideBySide::ProcessToRun

Inherits:
Object
  • Object
show all
Defined in:
lib/side_by_side/process_to_run.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:) ⇒ ProcessToRun



6
7
8
9
10
# File 'lib/side_by_side/process_to_run.rb', line 6

def initialize(command:)
  @command = command
  @output = []
  @monitor = Monitor.new
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



4
5
6
# File 'lib/side_by_side/process_to_run.rb', line 4

def command
  @command
end

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



4
5
6
# File 'lib/side_by_side/process_to_run.rb', line 4

def exit_status
  @exit_status
end

#monitorObject (readonly)

Returns the value of attribute monitor.



4
5
6
# File 'lib/side_by_side/process_to_run.rb', line 4

def monitor
  @monitor
end

#on_output_callbackObject (readonly)

Returns the value of attribute on_output_callback.



4
5
6
# File 'lib/side_by_side/process_to_run.rb', line 4

def on_output_callback
  @on_output_callback
end

#outputObject (readonly)

Returns the value of attribute output.



4
5
6
# File 'lib/side_by_side/process_to_run.rb', line 4

def output
  @output
end

#pidObject (readonly)

Returns the value of attribute pid.



4
5
6
# File 'lib/side_by_side/process_to_run.rb', line 4

def pid
  @pid
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



4
5
6
# File 'lib/side_by_side/process_to_run.rb', line 4

def start_time
  @start_time
end

#stop_timeObject (readonly)

Returns the value of attribute stop_time.



4
5
6
# File 'lib/side_by_side/process_to_run.rb', line 4

def stop_time
  @stop_time
end

#threadObject (readonly)

Returns the value of attribute thread.



4
5
6
# File 'lib/side_by_side/process_to_run.rb', line 4

def thread
  @thread
end

Instance Method Details

#joinObject



12
13
14
15
16
17
18
19
# File 'lib/side_by_side/process_to_run.rb', line 12

def join
  while !thread
    puts "Pass - waiting for thread"
    Thread.pass
  end

  thread.join
end

#on_output(&blk) ⇒ Object



21
22
23
24
# File 'lib/side_by_side/process_to_run.rb', line 21

def on_output(&blk)
  puts "Setting on output for: #{command}"
  @on_output_callback = blk
end

#output!(out) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/side_by_side/process_to_run.rb', line 26

def output!(out)
  type = out.fetch(:type)

  if type == :stdout
    $stdout << out.fetch(:output)
  elsif type == :stderr
    $stderr << out.fetch(:output)
  else
    raise "Unknown type: #{type}"
  end
end

#run_asyncObject



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
76
77
78
79
80
81
82
83
# File 'lib/side_by_side/process_to_run.rb', line 38

def run_async
  @thread = Thread.new do
    @start_time = Time.new
    stderr_reader, stderr_writer = IO.pipe

    PTY.spawn(command, err: stderr_writer.fileno) do |stdout, stdin, pid|
      @pid = pid

      puts "Command running: #{command} - #{stdout.class.name}"

      stdout_reader_thread = Thread.new do
        begin
          stdout.each_char do |chunk|
            monitor.synchronize do
              out = {type: :stdout, output: chunk}
              output << out
              on_output_callback&.call(out)
            end
          end
        rescue Errno::EIO => e
          # Process done
        ensure
          status = Process::Status.wait(@pid, 0)

          @exit_status = status.exitstatus
          stderr_writer.close
        end
      end

      stderr_reader_thread = Thread.new do
        stderr_reader.each_char do |chunk|
          monitor.synchronize do
            out = {type: :stderr, output: chunk}
            output << out
            on_output_callback&.call(out)
          end
        end
      end

      stdout_reader_thread.join
      stderr_reader_thread.join

      @stop_time = Time.new
    end
  end
end

#secondsObject



85
86
87
# File 'lib/side_by_side/process_to_run.rb', line 85

def seconds
  @stop_time.to_f - @start_time.to_f
end