class Mcrain::Base

Attributes

skip_reset_after_teardown[RW]

Public Class Methods

new(attrs = {}) click to toggle source
# File lib/mcrain/base.rb, line 26
def initialize(attrs = {})
  attrs.each do |key, value|
    send("#{key}=", value)
  end
end
work_dir() click to toggle source
# File lib/mcrain/base.rb, line 108
def work_dir
  File.join(Mcrain.home_dir, server_name.to_s)
end

Public Instance Methods

logger() click to toggle source
# File lib/mcrain/base.rb, line 15
def logger
  Mcrain.logger
end
reset() click to toggle source
# File lib/mcrain/base.rb, line 20
def reset
  instance_variables.each do |var|
    instance_variable_set(var, nil)
  end
end
setup() click to toggle source
# File lib/mcrain/base.rb, line 43
def setup
  logger.info("#{self.class.name}#setup STARTED")
  return false if Mcrain.before_setup && !Mcrain.before_setup.call(self)
  Timeout.timeout(30) do
    DockerMachine.setup_docker_options
    container.start!
  end
  logger.info("#{self.class.name}#setup COMPLETED")
  return container
end
start(&block) click to toggle source
# File lib/mcrain/base.rb, line 32
def start(&block)
  r = setup
  return nil unless r
  if block_given?
    start_callback(&block)
  else
    wait
  end
  return self
end
start_callback() { |self| ... } click to toggle source
# File lib/mcrain/base.rb, line 54
def start_callback
  begin
    wait_port
    wait
    return yield(self)
  rescue Exception => e
    logs = container.logs(stdout: 1, stderr: 1)
    logger.error("[#{e.class.name}] #{e.message}\nthe container logs...\n#{logs}")
    raise e
  ensure
    teardown
  end
end
stop_or_kill_and_remove() click to toggle source
# File lib/mcrain/base.rb, line 98
def stop_or_kill_and_remove
  begin
    container.stop!
  rescue => e
    container.kill!
  end
  container.remove(v: "1") unless ENV['MCRAIN_KEEP_CONTAINERS'] =~ /true|yes|on|1/i
end
teardown() click to toggle source
# File lib/mcrain/base.rb, line 93
def teardown
  stop_or_kill_and_remove
  reset unless skip_reset_after_teardown
end
wait() click to toggle source

ポートはdockerがまずLISTENしておいて、その後コンテナ内のミドルウェアが起動するので、 実際にそのAPIを叩いてみて例外が起きないことを確認します。

# File lib/mcrain/base.rb, line 75
def wait
  logger.info("#{self.class.name}#wait STARTED")
  Timeout.timeout(60) do
    begin
      wait_for_ready
    rescue => e
      $stderr.puts "[#{e.class}] #{e.message}"
      sleep(1)
      retry
    end
  end
  logger.info("#{self.class.name}#wait COMPLETED")
end
wait_for_ready() click to toggle source
# File lib/mcrain/base.rb, line 89
def wait_for_ready
  raise NotImplementedError
end
wait_port() click to toggle source

ポートがLISTENされるまで待つ

# File lib/mcrain/base.rb, line 69
def wait_port
  Mcrain.wait_port_opened(host, port, interval: 0.5, timeout: 30)
end