Sha256: 95a9652148a8b037712fdd13bd4e71ccbdb5b12758120176cf47103d0c1ad81e

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true
class Coursemology::Evaluator::DockerContainer < Docker::Container
  class << self
    def create(image, argv: nil)
      pull_image(image)

      ActiveSupport::Notifications.instrument('create.docker.evaluator.coursemology',
                                              image: image) do |payload|
        options = { 'Image' => image }
        options['Cmd'] = argv if argv && !argv.empty?

        payload[:container] = super(options)
      end
    end

    private

    def pull_image(image)
      ActiveSupport::Notifications.instrument('pull.docker.evaluator.coursemology',
                                              image: image) do
        Docker::Image.create('fromImage' => image)
      end
    end
  end

  # Waits for the container to exit the Running state.
  #
  # This will time out for long running operations, so keep retrying until we return.
  #
  # @param [Fixnum|nil] time The amount of time to wait.
  # @return [Fixnum] The exit code of the container.
  def wait(time = nil)
    container_state = info
    while container_state.fetch('State', {}).fetch('Running', true)
      super
      refresh!
      container_state = info
    end

    container_state['State']['ExitCode']
  rescue Docker::Error::TimeoutError
    retry
  end

  # Gets the exit code of the container.
  #
  # @return [Fixnum] The exit code of the container, if +wait+ was called before.
  # @return [nil] If the container is still running, or +wait+ was not called.
  def exit_code
    info.fetch('State', {})['ExitCode']
  end

  def delete
    ActiveSupport::Notifications.instrument('destroy.docker.evaluator.coursemology',
                                            container: id) do
      super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coursemology-evaluator-0.1.3 lib/coursemology/evaluator/docker_container.rb