Class: CC::Analyzer::Container
- Inherits:
-
Object
- Object
- CC::Analyzer::Container
- Defined in:
- lib/cc/analyzer/container.rb
Defined Under Namespace
Classes: ContainerData
Constant Summary collapse
- ImageRequired =
Class.new(StandardError)
- DEFAULT_TIMEOUT =
15m
15 * 60
Instance Method Summary collapse
-
#initialize(image:, name:, command: nil, listener: ContainerListener.new, timeout: DEFAULT_TIMEOUT) ⇒ 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, timeout: DEFAULT_TIMEOUT) ⇒ Container
Returns a new instance of Container.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cc/analyzer/container.rb', line 17 def initialize( image:, name:, command: nil, listener: ContainerListener.new, timeout: DEFAULT_TIMEOUT ) raise ImageRequired if image.blank? @image = image @name = name @command = command @listener = listener @timeout = timeout @output_delimeter = "\n" @on_output = ->(*) { } @timed_out = false @stderr_io = StringIO.new end |
Instance Method Details
#on_output(delimeter = "\n", &block) ⇒ Object
38 39 40 41 |
# File 'lib/cc/analyzer/container.rb', line 38 def on_output(delimeter = "\n", &block) @output_delimeter = delimeter @on_output = block end |
#run(options = []) ⇒ Object
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 |
# File 'lib/cc/analyzer/container.rb', line 43 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(@pid) _, status = Process.waitpid2(@pid) duration = ((Time.now - started) * 1000).round @listener.finished(container_data(duration: duration, status: status)) t_timeout.kill ensure t_timeout.kill if t_timeout if @timed_out @listener.timed_out(container_data(duration: @timeout)) 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
72 73 74 75 76 77 78 79 |
# File 'lib/cc/analyzer/container.rb', line 72 def stop # Prevents the processing of more output after first error @on_output = ->(*) { } Process.kill("TERM", @pid) if @pid rescue Errno::ESRCH Analyzer.statsd.increment("container.kill_process_rescue") end |