Class: PrettyMultitask::RunCallable

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_multitask/run_callable.rb

Overview

This class will run a callable, and wrap it’s output adding nice format

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ RunCallable

Returns a new instance of RunCallable.



8
9
10
# File 'lib/pretty_multitask/run_callable.rb', line 8

def initialize(opts = {})
  @opts = opts
end

Instance Method Details

#close_streams(streams) ⇒ Object



72
73
74
# File 'lib/pretty_multitask/run_callable.rb', line 72

def close_streams(streams)
  streams.each(&:close)
end

#consume_and_print(reader, writer, name, error = false) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pretty_multitask/run_callable.rb', line 98

def consume_and_print(reader, writer, name, error = false)
  Thread.new do
    begin
      fmt = "%#{@padding}s |"
      colored = Color.red format(fmt, name) if error
      colored = Color.green format(fmt, name) unless error
      reader.each_line do |line|
        writer.write "#{colored} #{line}"
        next unless @opts[:out_file]

        File.open(@opts[:out_file], 'a+') do |f|
          f.puts "#{colored} #{line}" if error
          f.puts "#{colored} #{line}" unless error
        end
      end
    rescue Errno::EIO
      nil
    rescue IOError
      nil
    end
  end
end

#consume_and_store(reader, store) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/pretty_multitask/run_callable.rb', line 46

def consume_and_store(reader, store)
  t = Thread.new do
    sleep 0.1 until reader.ready?
    3.times do
      store << reader.getc while reader.ready?
    end
  end
  t
end

#join_threads(threads) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/pretty_multitask/run_callable.rb', line 62

def join_threads(threads)
  threads.each do |t|
    begin
      Timeout.timeout(0.1) { t.join }
    rescue Timeout::Error
      nil
    end
  end
end

#run(opts = @opts) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pretty_multitask/run_callable.rb', line 12

def run(opts = @opts)
  cmd = opts[:cmd]
  name = opts[:name]
  out = STDOUT
  @padding = opts[:padding]


  master_out, slave_out = PTY.open
  master_err, slave_err = PTY.open
  r, w = UNIXSocket.pair(:STREAM, 0)
  pid = run_on_fork(cmd, slave_err, slave_out, w)

  Trapper.trap pid

  t_out = consume_and_print master_out, out, name, false
  t_err = consume_and_print master_err, out, name, true

  chars = []
  t = consume_and_store r, chars

  Process.wait pid

  wait_until_streams_are_ready [slave_out, slave_err]

  join_threads [t_out, t_err, t]

  close_streams [master_out, slave_out, master_err, slave_err]

  result = Marshal.load(chars.join)
  raise result[:error] if result[:error]

  result[:result]
end

#run_on_fork(cmd, err_w, out_w, socket_w) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pretty_multitask/run_callable.rb', line 76

def run_on_fork(cmd, err_w, out_w, socket_w)
  pid = fork do
    STDERR.reopen err_w
    STDOUT.reopen out_w

    begin
      result = cmd.call
      obj = { result: result, error: nil }
    rescue StandardError => e
      new_error = e.class.new(e.message)
      new_error.set_backtrace e.backtrace
      LOGGER.reopen STDERR
      LOGGER.error new_error
      obj = { result: nil, error: new_error }
      socket_w.puts Marshal.dump(obj), 0
    end
    socket_w.puts Marshal.dump(obj), 0
    socket_w.close
  end
  pid
end

#wait_until_streams_are_ready(streams) ⇒ Object



56
57
58
59
60
# File 'lib/pretty_multitask/run_callable.rb', line 56

def wait_until_streams_are_ready(streams)
  streams.each do |s|
    loop { break unless s.ready? }
  end
end