class Coursemology::Evaluator::DockerContainer

Public Class Methods

create(image, argv: nil) click to toggle source
Calls superclass method
# File lib/coursemology/evaluator/docker_container.rb, line 4
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 Class Methods

cached(key, options = {}, &proc) click to toggle source

Cache the result of the given block using the key given.

@param [Array, String, Symbol] key The key to use. This will be expanded with

+ActiveSupport::Cache.expand_cache_key+.

@param [Hash] options The options to use. These are the same as

+ActiveSupport::Cache::Store#fetch+.
# File lib/coursemology/evaluator/docker_container.rb, line 40
def cached(key, options = {}, &proc)
  key = ActiveSupport::Cache.expand_cache_key(key, name.underscore)
  Coursemology::Evaluator.cache.fetch(key, options, &proc)
end
pull_image(image) click to toggle source

Pulls the given image from Docker Hub.

This caches images for the specified time, because the overhead for querying for images is quite high.

@param [String] image The image to pull.

# File lib/coursemology/evaluator/docker_container.rb, line 24
def pull_image(image)
  ActiveSupport::Notifications.instrument('pull.docker.evaluator.coursemology',
                                          image: image) do |payload|
    cached([:image, image], expires_in: Coursemology::Evaluator.config.image_lifetime) do
      Docker::Image.create('fromImage' => image)
      payload[:cached] = false
    end
  end
end

Public Instance Methods

delete() click to toggle source
Calls superclass method
# File lib/coursemology/evaluator/docker_container.rb, line 73
def delete
  ActiveSupport::Notifications.instrument('destroy.docker.evaluator.coursemology',
                                          container: id) do
    super
  end
end
exit_code() click to toggle source

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.

# File lib/coursemology/evaluator/docker_container.rb, line 69
def exit_code
  info.fetch('State', {})['ExitCode']
end
wait(time = nil) click to toggle source

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.

Calls superclass method
# File lib/coursemology/evaluator/docker_container.rb, line 52
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