Class: Hermetic::Limits

Inherits:
Object
  • Object
show all
Defined in:
lib/hermetic/limits.rb

Overview

Resource ceilings for a run. Set on the box at construction, overridable per run via limits: (merged over the box defaults).

Constant Summary collapse

DEFAULTS =
{ memory: "512m", cpus: "1", pids: 256 }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(memory: DEFAULTS[:memory], cpus: DEFAULTS[:cpus], pids: DEFAULTS[:pids]) ⇒ Limits

Returns a new instance of Limits.



18
19
20
21
22
23
24
25
# File 'lib/hermetic/limits.rb', line 18

def initialize(memory: DEFAULTS[:memory], cpus: DEFAULTS[:cpus], pids: DEFAULTS[:pids])
  @memory = memory.to_s
  @cpus = cpus.to_s
  @pids = Integer(pids)
  raise ConfigError, "pids must be positive, got #{pids.inspect}" unless @pids.positive?
rescue ArgumentError, TypeError
  raise ConfigError, "pids must be an integer, got #{pids.inspect}"
end

Instance Attribute Details

#cpusObject (readonly)

Returns the value of attribute cpus.



7
8
9
# File 'lib/hermetic/limits.rb', line 7

def cpus
  @cpus
end

#memoryObject (readonly)

Returns the value of attribute memory.



7
8
9
# File 'lib/hermetic/limits.rb', line 7

def memory
  @memory
end

#pidsObject (readonly)

Returns the value of attribute pids.



7
8
9
# File 'lib/hermetic/limits.rb', line 7

def pids
  @pids
end

Class Method Details

.build(value) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/hermetic/limits.rb', line 9

def self.build(value)
  case value
  when nil then new
  when Limits then value
  when Hash then new.merge(value)
  else raise ConfigError, "limits must be a Hash or Hermetic::Limits, got #{value.inspect}"
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



39
# File 'lib/hermetic/limits.rb', line 39

def ==(other) = other.is_a?(Limits) && other.to_h == to_h

#hashObject



42
# File 'lib/hermetic/limits.rb', line 42

def hash = to_h.hash

#merge(overrides) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/hermetic/limits.rb', line 27

def merge(overrides)
  return self if overrides.nil?

  overrides = overrides.to_h.transform_keys(&:to_sym)
  unknown = overrides.keys - DEFAULTS.keys
  raise ConfigError, "unknown limit(s) #{unknown.inspect} — known: #{DEFAULTS.keys.inspect}" unless unknown.empty?

  self.class.new(**to_h.merge(overrides))
end

#to_hObject



37
# File 'lib/hermetic/limits.rb', line 37

def to_h = { memory: memory, cpus: cpus, pids: pids }