Sha256: 102694f184e8f11728cd6030e2a38d5b97ac0ebf40bd5cb8f8ece0d2ea31b704

Contents?: true

Size: 1023 Bytes

Versions: 1

Compression:

Stored size: 1023 Bytes

Contents

require 'get_process_mem'

module WorkerKiller
  class MemoryLimiter

    attr_reader :min, :max, :limit, :started_at, :check_cycle

    def initialize(min: (1024**3), max: (2 * (1024**3)), check_cycle: 16, verbose: false)
      @min = min
      @max = max
      @limit = @min + WorkerKiller.randomize(@max - @min + 1)
      @check_cycle = check_cycle
      @check_count = 0
      @verbose = verbose
    end

    def check
      return nil if @limit <= 1024**2

      @started_at ||= Time.now
      @check_count += 1

      return nil if (@check_count % @check_cycle) != 0

      rss = GetProcessMem.new.bytes
      if @verbose
        logger.info "#{self.class}: worker (pid: #{Process.pid}) using #{rss} bytes(#{rss / 1024 / 1024}mb)."
      end
      @check_count = 0

      return false if rss <= @limit

      logger.warn "#{self.class}: worker (pid: #{Process.pid}) exceeds memory limit (#{rss} bytes > #{@limit} bytes)"

      true
    end

    def logger
      WorkerKiller.configuration.logger
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
worker_killer-1.0.2.187852 lib/worker_killer/memory_limiter.rb