Class: ContainedMr::Mock::Runner

Inherits:
Object
  • Object
show all
Includes:
RunnerLogic
Defined in:
lib/contained_mr/mock/runner.rb

Overview

See Also:

  • {ContainedMr{ContainedMr::Runner}

Instance Attribute Summary collapse

Attributes included from RunnerLogic

#container_id, #ended_at, #output, #started_at, #status_code, #stderr, #stdout, #timed_out

Instance Method Summary collapse

Methods included from RunnerLogic

#json_file, #ran_for

Constructor Details

#initialize(container_options, time_limit, output_path) ⇒ Runner

Returns a new instance of Runner.

See Also:

  • ContainedMr::Mock::Runner.{ContainedMr{ContainedMr::Runner{ContainedMr::Runner#initialize}


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/contained_mr/mock/runner.rb', line 23

def initialize(container_options, time_limit, output_path)
  @_container_options = container_options
  @_time_limit = time_limit
  @_output_path = output_path

  @container_id = nil
  @started_at = @ended_at = nil
  @status_code = nil
  @timed_out = nil
  @stdout = @stderr = nil
  @output = nil

  @performed = false
  @destroyed = false
end

Instance Attribute Details

#_container_optionsHash<String, Object> (readonly)

Returns the options passed to the constructor.

Returns:

  • (Hash<String, Object>)

    the options passed to the constructor



4
5
6
# File 'lib/contained_mr/mock/runner.rb', line 4

def _container_options
  @_container_options
end

#_output_pathHash<String, Object> (readonly)

Returns the output path passed to the constructor.

Returns:

  • (Hash<String, Object>)

    the output path passed to the constructor



8
9
10
# File 'lib/contained_mr/mock/runner.rb', line 8

def _output_path
  @_output_path
end

#_time_limitHash<String, Object> (readonly)

Returns the time limit passed to the constructor.

Returns:

  • (Hash<String, Object>)

    the time limit passed to the constructor



6
7
8
# File 'lib/contained_mr/mock/runner.rb', line 6

def _time_limit
  @_time_limit
end

Instance Method Details

#_logsNumber

Convenience method for looking up the log size limit in container options.

Returns:

  • (Number)

    the container’s log limit, in megabytes



122
123
124
125
126
127
128
129
# File 'lib/contained_mr/mock/runner.rb', line 122

def _logs
  return nil unless host_config = @_container_options['HostConfig']
  return nil unless log_config = host_config['LogConfig']
  return nil unless config = log_config['Config']
  return nil unless max_size = config['max-size']

  max_size.to_i / (1024 * 1024).to_f
end

#_mock_set(attributes) ⇒ ContainedMr::Runner

Sets the container execution data returned by the mock.

Parameters:

  • attributes (Hash<Symbol, Object>)

    values describing the result of running the job

Returns:



56
57
58
59
60
61
62
63
64
65
# File 'lib/contained_mr/mock/runner.rb', line 56

def _mock_set(attributes)
  @started_at = attributes[:started_at]
  @ended_at = attributes[:ended_at]
  @status_code = attributes[:status_code]
  @timed_out = attributes[:timed_out]
  @stdout = attributes[:stdout]
  @stderr = attributes[:stderr]
  @output = attributes[:output]
  self
end

#_ram_limitNumber

Convenience method for looking up the RAM limit in the container options.

Returns:

  • (Number)

    the container’s RAM limit, in megabytes



90
91
92
93
94
# File 'lib/contained_mr/mock/runner.rb', line 90

def _ram_limit
  return nil unless host_config = @_container_options['HostConfig']
  return nil unless memory = host_config['Memory']
  memory / (1024 * 1024).to_f
end

#_swap_limitNumber

Convenience method for looking up the swap limit in the container options.

Returns:

  • (Number)

    the container’s swap limit, in megabytes



99
100
101
102
103
104
105
# File 'lib/contained_mr/mock/runner.rb', line 99

def _swap_limit
  return nil unless host_config = @_container_options['HostConfig']
  return nil unless memory = host_config['Memory']
  return nil unless memory_swap = host_config['MemorySwap']

  (memory_swap - memory) / (1024 * 1024).to_f
end

#_ulimit(name) ⇒ Number

Convenience method for looking up an ulimit in the container options.

Parameters:

  • name (String)

    the ulimit’s name, such as ‘cpu’ or ‘rss’

Returns:

  • (Number)

    the ulimit’s hard and soft value, or nil if the ulimit was not found

Raises:

  • (RuntimeError)

    if the ulimit’s hard and soft values don’t match



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/contained_mr/mock/runner.rb', line 73

def _ulimit(name)
  return nil unless ulimits = @_container_options['Ulimits']

  ulimits.each do |ulimit|
    if ulimit['Name'] == name
      if ulimit['Hard'] != ulimit['Soft']
        raise RuntimeError, "Hard/soft ulimit mismatch for #{name}"
      end
      return ulimit['Hard']
    end
  end
  nil
end

#_vcpusNumber

Convenience method for looking up CPU allocation in the container options.

Returns:

  • (Number)

    the number of CPU cores allocated to the container; this can be a fraction



111
112
113
114
115
116
117
# File 'lib/contained_mr/mock/runner.rb', line 111

def _vcpus
  return nil unless host_config = @_container_options['HostConfig']
  return nil unless period = host_config['CpuPeriod']
  return nil unless quota = host_config['CpuQuota']

  quota / period.to_f
end

#destroy!Object

See Also:

  • ContainedMr::Mock::Runner.{ContainedMr{ContainedMr::Runner{ContainedMr::Runner#destroy!}


46
47
48
49
# File 'lib/contained_mr/mock/runner.rb', line 46

def destroy!
  @destroyed = true
  self
end

#destroyed?Boolean

Returns true if #destroy! was called.

Returns:



16
17
18
# File 'lib/contained_mr/mock/runner.rb', line 16

def destroyed?
  @destroyed
end

#performObject

See Also:

  • ContainedMr::Mock::Runner.{ContainedMr{ContainedMr::Runner{ContainedMr::Runner#perform}


40
41
42
43
# File 'lib/contained_mr/mock/runner.rb', line 40

def perform
  @performed = true
  self
end

#performed?Boolean

Returns true if #perform was called.

Returns:

  • (Boolean)

    true if #perform was called



11
12
13
# File 'lib/contained_mr/mock/runner.rb', line 11

def performed?
  @performed
end