Sha256: f967e1a6cceaa49e6c1032034f8cada7810f1a330f835e34ff964b3cbf12d8ff

Contents?: true

Size: 1.67 KB

Versions: 5

Compression:

Stored size: 1.67 KB

Contents

module PumaWorkerKiller
  class PumaMemory
    def initialize(master = nil)
      @master  = master || get_master
    end

    def master
      @master
    end

    def size
      workers.size
    end

    def term_largest_worker
      largest_worker.term
    #   Process.wait(largest_worker.pid)
    # rescue Errno::ECHILD
    end

    def running?
      @master && workers.any?
    end

    def smallest_worker
      smallest, _ = workers.to_a.first
      smallest
    end

    def smallest_worker_memory
      _, smallest_mem = workers.to_a.first
      smallest_mem
    end

    def largest_worker
      largest_worker, _ = workers.to_a.last
      largest_worker
    end

    def largest_worker_memory
      _, largest_memory_used = workers.to_a.last
      largest_memory_used
    end

    # Will refresh @workers
    def get_total(workers = set_workers)
      master_memory = GetProcessMem.new(Process.pid).mb
      worker_memory = workers.map {|_, mem| mem }.inject(&:+) || 0
      worker_memory + master_memory
    end
    alias :get_total_memory :get_total

    def workers
      @workers || set_workers
    end

    private

    def get_master
      ObjectSpace.each_object(Puma::Cluster).map { |obj| obj }.first if defined?(Puma::Cluster)
    end

    # Returns sorted hash, keys are worker objects, values are memory used per worker
    # sorted by memory ascending (smallest first, largest last)
    def set_workers
      workers = {}
      @master.instance_variable_get("@workers").each do |worker|
        workers[worker] = GetProcessMem.new(worker.pid).mb
      end
      if workers.any?
        @workers = Hash[ workers.sort_by {|_, mem| mem } ]
      else
        {}
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
puma_worker_killer-0.0.6 lib/puma_worker_killer/puma_memory.rb
puma_worker_killer-0.0.5 lib/puma_worker_killer/puma_memory.rb
puma_worker_killer-0.0.4 lib/puma_worker_killer/puma_memory.rb
puma_worker_killer-0.0.3 lib/puma_worker_killer/puma_memory.rb
puma_worker_killer-0.0.2 lib/puma_worker_killer/puma_memory.rb