Class: CC::Analyzer::Engine

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

Constant Summary collapse

TIMEOUT =

15m

15 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, metadata, code_path, config_json, label) ⇒ Engine

Returns a new instance of Engine.



11
12
13
14
15
16
17
# File 'lib/cc/analyzer/engine.rb', line 11

def initialize(name, , code_path, config_json, label)
  @name = name
  @metadata = 
  @code_path = code_path
  @config_json = config_json
  @label = label.to_s
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/cc/analyzer/engine.rb', line 7

def name
  @name
end

Instance Method Details

#run(stdout_io, stderr_io = StringIO.new) ⇒ Object



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

def run(stdout_io, stderr_io = StringIO.new)
  timed_out = false
  pid, _, out, err = POSIX::Spawn.popen4(*docker_run_command)
  Analyzer.statsd.increment("cli.engines.started")

  t_out = Thread.new do
    out.each_line("\0") do |chunk|
      stdout_io.write(chunk.chomp("\0"))
    end
  end

  t_err = Thread.new do
    err.each_line do |line|
      if stderr_io
        stderr_io.write(line)
      end
    end
  end

  t_timeout = Thread.new do
    sleep TIMEOUT
    run_command("docker kill #{container_name} || true")
    timed_out = true
  end

  pid, status = Process.waitpid2(pid)
  t_timeout.kill

  Analyzer.statsd.increment("cli.engines.finished")

  if timed_out
    Analyzer.statsd.increment("cli.engines.result.error")
    Analyzer.statsd.increment("cli.engines.result.error.timeout")
    Analyzer.statsd.increment("cli.engines.names.#{name}.result.error")
    Analyzer.statsd.increment("cli.engines.names.#{name}.result.error.timeout")
    raise EngineTimeout, "engine #{name} ran past #{TIMEOUT} seconds and was killed"
  elsif status.success?
    Analyzer.statsd.increment("cli.engines.names.#{name}.result.success")
    Analyzer.statsd.increment("cli.engines.result.success")
  else
    Analyzer.statsd.increment("cli.engines.names.#{name}.result.error")
    Analyzer.statsd.increment("cli.engines.result.error")
    raise EngineFailure, "engine #{name} failed with status #{status.exitstatus} and stderr #{stderr_io.string.inspect}"
  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