Sha256: 1e518602c843cfe3d986b017b628c0ad8211b1f7ac682f68eba5b6ff4ac9a3ce

Contents?: true

Size: 1011 Bytes

Versions: 8

Compression:

Stored size: 1011 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}: worker (pid: #{Process.pid}) using #{rss} bytes(#{rss / 1024 / 1024}mb)."
      end
      @check_count = 0

      return false if rss <= @limit

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

      true
    end

    def logger
      WorkerKiller.configuration.logger
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
worker_killer-1.0.2.187805 lib/worker_killer/memory_limiter.rb
worker_killer-1.0.2.187732 lib/worker_killer/memory_limiter.rb
worker_killer-1.0.1.83603 lib/worker_killer/memory_limiter.rb
worker_killer-1.0.1.75538 lib/worker_killer/memory_limiter.rb
worker_killer-1.0.1.39842 lib/worker_killer/memory_limiter.rb
worker_killer-1.0.0.39841 lib/worker_killer/memory_limiter.rb
worker_killer-1.0.0.39839 lib/worker_killer/memory_limiter.rb
worker_killer-0.1.1.39838 lib/worker_killer/memory_limiter.rb