Class: ElasticGraph::Local::DockerRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/local/docker_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(variant, port:, ui_port:, version:, env:, ready_log_line:, daemon_timeout:, output:) ⇒ DockerRunner

Returns a new instance of DockerRunner.



15
16
17
18
19
20
21
22
23
24
# File 'lib/elastic_graph/local/docker_runner.rb', line 15

def initialize(variant, port:, ui_port:, version:, env:, ready_log_line:, daemon_timeout:, output:)
  @variant = variant
  @port = port
  @ui_port = ui_port
  @version = version
  @env = env
  @ready_log_line = ready_log_line
  @daemon_timeout = daemon_timeout
  @output = output
end

Instance Method Details

#bootObject



26
27
28
29
30
31
32
33
34
# File 'lib/elastic_graph/local/docker_runner.rb', line 26

def boot
  # :nocov: -- this is actually covered via a call from `boot_as_daemon` but it happens in a forked process so simplecov doesn't see it.
  halt

  prepare_docker_compose_run "up" do |command|
    exec(command) # we use `exec` so that our process is replaced with that one.
  end
  # :nocov:
end

#boot_as_daemon(halt_command:) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/elastic_graph/local/docker_runner.rb', line 42

def boot_as_daemon(halt_command:)
  with_pipe do |read_io, write_io|
    fork do
      # :nocov: -- simplecov can't track coverage that happens in another process
      read_io.close
      Process.daemon
      pid = Process.pid
      $stdout.reopen(write_io)
      $stderr.reopen(write_io)
      puts pid
      boot
      write_io.close
      # :nocov:
    end

    # The `Process.daemon` call in the subprocess changes the pid so we have to capture it this way instead of using
    # the return value of `fork`.
    pid = read_io.gets.to_i

    @output.puts "Booting #{@variant}; monitoring logs for readiness..."

    ::Timeout.timeout(
      @daemon_timeout,
      ::Timeout::Error,
      "        Timed out after \#{@daemon_timeout} seconds. The expected \"ready\" log line[1] was not found in the logs.\n\n        [1] \#{@ready_log_line.inspect}\n      EOS\n    ) do\n      loop do\n        sleep 0.01\n        line = read_io.gets\n        @output.puts line\n        break if @ready_log_line.match?(line.to_s)\n      end\n    end\n\n    @output.puts\n    @output.puts\n    @output.puts <<~EOS\n      Success! \#{@variant} \#{@version} (pid: \#{pid}) has been booted for the \#{@env} environment on port \#{@port}.\n      It will continue to run in the background as a daemon. To halt it, run:\n\n      \#{halt_command}\n    EOS\n  end\nend\n"

#haltObject



36
37
38
39
40
# File 'lib/elastic_graph/local/docker_runner.rb', line 36

def halt
  prepare_docker_compose_run "down --volumes" do |command|
    system(command)
  end
end