class Baleen::Runner

Public Class Methods

new(task, connection=nil) click to toggle source
# File lib/baleen/runner.rb, line 46
def initialize(task, connection=nil)
  @container  = Docker::Container.create('Cmd' => ["bash", "-c", task.commands], 'Image' => task.image)
  @connection = connection ? connection : Connection.new
  @task = task
end

Public Instance Methods

run() click to toggle source
# File lib/baleen/runner.rb, line 52
def run
  max_retry = 3; count = 0

  begin
    notify_info("Start container #{@container.id}")
    @container.start
    @container.wait(600) #TODO move to configuration
    notify_info("Finish container #{@container.id}")

    if @task.commit
      notify_info("Committing the change of container #{@container.id}")
      @container.commit({repo: @task.image}) if @task.commit
    end
  rescue Excon::Errors::NotFound => e
    count += 1
    if count > max_retry
      raise Baleen::Error::StartContainerFail
    else
      retry
    end
  end

  stdout, stderr = *@container.attach(:stream => false, :stdout => true, :stderr => true, :logs => true)

  return {
    status_code: @container.json["State"]["ExitCode"],
    container_id: @container.id,
    stdout: stdout,
    stderr: stderr,
    file: @task.files,
  }
end