Class: CC::Analyzer::Container
- Inherits:
-
Object
- Object
- CC::Analyzer::Container
- 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
Instance Method Summary collapse
-
#initialize(image:, name:, command: nil, listener: ContainerListener.new) ⇒ Container
constructor
A new instance of Container.
- #on_output(delimeter = "\n", &block) ⇒ Object
- #run(options = []) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(image:, name:, command: nil, listener: ContainerListener.new) ⇒ Container
Returns a new instance of Container.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cc/analyzer/container.rb', line 18 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 @stderr_io = StringIO.new end |
Instance Method Details
#on_output(delimeter = "\n", &block) ⇒ Object
30 31 32 33 |
# File 'lib/cc/analyzer/container.rb', line 30 def on_output(delimeter = "\n", &block) @output_delimeter = delimeter @on_output = block end |
#run(options = []) ⇒ Object
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 |
# File 'lib/cc/analyzer/container.rb', line 35 def run( = []) started = Time.now @listener.started(container_data) pid, _, out, err = POSIX::Spawn.popen4(*docker_run_command()) t_out = read_stdout(out) t_err = read_stderr(err) t_timeout = timeout_thread _, status = Process.waitpid2(pid) if @timed_out @listener.timed_out(container_data(duration: timeout)) Result.new(status.exitstatus, true, timeout, @stderr_io.string) else duration = ((Time.now - started) * 1000).round @listener.finished(container_data(duration: duration, status: status)) Result.new(status.exitstatus, false, duration, @stderr_io.string) end ensure t_timeout.kill if t_timeout if @timed_out t_out.kill if t_out t_err.kill if t_err else t_out.join if t_out t_err.join if t_err end end |
#stop ⇒ Object
65 66 67 68 69 70 |
# File 'lib/cc/analyzer/container.rb', line 65 def stop # Prevents the processing of more output after first error @on_output = ->(*) {} reap_running_container end |