Class: CC::Analyzer::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/cc/analyzer/container.rb

Defined Under Namespace

Classes: ContainerData, Result

Constant Summary collapse

ImageRequired =
Class.new(StandardError)
DEFAULT_TIMEOUT =

15m

15 * 60
DEFAULT_MAXIMUM_OUTPUT_BYTES =
500_000_000

Instance Method Summary collapse

Constructor Details

#initialize(image:, name:, command: nil, listener: ContainerListener.new) ⇒ Container

Returns a new instance of Container.

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cc/analyzer/container.rb', line 27

def initialize(image:, name:, command: nil, listener: ContainerListener.new)
  raise ImageRequired if image.blank?
  @image = image
  @name = name
  @command = command
  @listener = listener
  @output_delimeter = "\n"
  @on_output = ->(*) {}
  @timed_out = false
  @maximum_output_exceeded = false
  @stderr_io = StringIO.new
  @output_byte_count = 0
  @counter_mutex = Mutex.new
end

Instance Method Details

#on_output(delimeter = "\n", &block) ⇒ Object



42
43
44
45
# File 'lib/cc/analyzer/container.rb', line 42

def on_output(delimeter = "\n", &block)
  @output_delimeter = delimeter
  @on_output = block
end

#run(options = []) ⇒ Object



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
# File 'lib/cc/analyzer/container.rb', line 47

def run(options = [])
  started = Time.now
  @listener.started(container_data)

  pid, _, out, err = POSIX::Spawn.popen4(*docker_run_command(options))

  t_out = read_stdout(out)
  t_err = read_stderr(err)
  t_timeout = timeout_thread

  _, status = Process.waitpid2(pid)

  if @timed_out
    duration = timeout * 1000
    @listener.timed_out(container_data(duration: duration))
  else
    sleep 1 until !t_out.alive? && !t_err.alive?
    duration = ((Time.now - started) * 1000).round
    @listener.finished(container_data(duration: duration, status: status))
  end

  Result.new(
    status.exitstatus,
    @timed_out,
    duration,
    @maximum_output_exceeded,
    output_byte_count,
    @stderr_io.string,
  )
ensure
  [t_timeout, t_out, t_err].each { |t| t.kill if t }
end

#stopObject



80
81
82
83
84
85
# File 'lib/cc/analyzer/container.rb', line 80

def stop
  # Prevents the processing of more output after first error
  @on_output = ->(*) {}

  reap_running_container
end