Class: WorkerKiller::MemoryLimiter
- Inherits:
-
Object
- Object
- WorkerKiller::MemoryLimiter
- Defined in:
- lib/worker_killer/memory_limiter.rb
Instance Attribute Summary collapse
-
#check_cycle ⇒ Object
readonly
Returns the value of attribute check_cycle.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
Instance Method Summary collapse
- #check ⇒ Object
- #do_check(rss) ⇒ Object
-
#initialize(min:, max:, check_cycle: 16, verbose: false) ⇒ MemoryLimiter
constructor
A new instance of MemoryLimiter.
- #initialize_limits(rss) ⇒ Object
- #logger ⇒ Object
- #set_limits(min, max) ⇒ Object
Constructor Details
#initialize(min:, max:, check_cycle: 16, verbose: false) ⇒ MemoryLimiter
Returns a new instance of MemoryLimiter.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/worker_killer/memory_limiter.rb', line 8 def initialize(min:, max:, check_cycle: 16, verbose: false) if min # set static memory limits @min = min @max = max else # prepare for relative memory limits @max_percent = max end @started_at = Time.now @check_cycle = check_cycle @check_count = 0 @triggered = false @verbose = verbose end |
Instance Attribute Details
#check_cycle ⇒ Object (readonly)
Returns the value of attribute check_cycle.
6 7 8 |
# File 'lib/worker_killer/memory_limiter.rb', line 6 def check_cycle @check_cycle end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
6 7 8 |
# File 'lib/worker_killer/memory_limiter.rb', line 6 def limit @limit end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
6 7 8 |
# File 'lib/worker_killer/memory_limiter.rb', line 6 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
6 7 8 |
# File 'lib/worker_killer/memory_limiter.rb', line 6 def min @min end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
6 7 8 |
# File 'lib/worker_killer/memory_limiter.rb', line 6 def started_at @started_at end |
Instance Method Details
#check ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/worker_killer/memory_limiter.rb', line 34 def check @check_count += 1 return nil if (@check_count % @check_cycle) != 0 return true if @triggered rss = GetProcessMem.new.bytes # initialize relative memory limits on first check initialize_limits(rss) if @limit.nil? do_check(rss) end |
#do_check(rss) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/worker_killer/memory_limiter.rb', line 48 def do_check(rss) rss_mb = (rss / 1024 / 1024).round(1) if @verbose logger.info "#{self.class}: worker (pid: #{Process.pid}) using #{rss_mb} MB (of #{@limit_mb} MB)" end @check_count = 0 return false if rss <= @limit @triggered = true logger.warn "#{self.class}: worker (pid: #{Process.pid}) exceeds memory limit (#{rss_mb} MB > #{@limit_mb} MB)" true end |
#initialize_limits(rss) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/worker_killer/memory_limiter.rb', line 26 def initialize_limits(rss) if min.nil? set_limits(rss, rss + rss * @max_percent) else set_limits(min, min + WorkerKiller.randomize(max - min + 1)) end end |
#logger ⇒ Object
71 72 73 |
# File 'lib/worker_killer/memory_limiter.rb', line 71 def logger WorkerKiller.configuration.logger end |
#set_limits(min, max) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/worker_killer/memory_limiter.rb', line 64 def set_limits(min, max) @min = min @limit = max @max ||= max @limit_mb = (@limit / 1024 / 1024).round(1) end |