Sha256: b5669a27fe83e12045aa5709fee006dfefcdd02479bc6dd98a389392553de6cb

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

require 'get_process_mem'

module WorkerKiller
  class MemoryLimiter

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

    def initialize(min: (1024**3), max: (2 * (1024**3)), check_cycle: 16, verbose: false, &block)
      @min = min
      @max = max
      @limit = @min + WorkerKiller.randomize(@max - @min + 1)
      @check_cycle = check_cycle
      @check_count = 0
      @verbose = verbose
      @reaction = block
    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)"
      @reaction.call(self)

      true
    end

    def logger
      WorkerKiller.configuration.logger
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
worker_killer-0.1.1.30345 lib/worker_killer/memory_limiter.rb
worker_killer-0.1.0.30337 lib/worker_killer/memory_limiter.rb
worker_killer-0.1.0.19844 lib/worker_killer/memory_limiter.rb
worker_killer-0.1.0.19842 lib/worker_killer/memory_limiter.rb
worker_killer-0.1.0.19841 lib/worker_killer/memory_limiter.rb
worker_killer-0.1.0.19840 lib/worker_killer/memory_limiter.rb
worker_killer-0.1.0.19839 lib/worker_killer/memory_limiter.rb
worker_killer-0.0.9.19836 lib/worker_killer/memory_limiter.rb