class PassengerMemoryStatus::MemoryStatus

Constants

MEMORY_LIMIT

Memory in MB

Public Class Methods

new(options = {}) click to toggle source
# File lib/passenger_memory_status.rb, line 11
def initialize(options = {})
    @memory_limit = options[:memory] || MEMORY_LIMIT
    @logger = Logger.new('passenger_memory_status.log')
end
run(options = {}) click to toggle source
# File lib/passenger_memory_status.rb, line 16
def self.run(options = {})
  new(options).bloated_passenger_process
end

Public Instance Methods

bloated_passenger_process() click to toggle source

Find and kill the Bloated Passenger Process

# File lib/passenger_memory_status.rb, line 22
def bloated_passenger_process

  if passenger_installed?

    `passenger-memory-stats`.each_line do |process|

      if process =~ /Passenger RubyApp: /

        pid, pid_memory = getpid(process)

        if pid_memory > @memory_limit.to_i

          @logger.info "Found bloated process #{pid} with size #{pid_memory.to_s}"

          sleep 8

          graceful(pid)

          if Process.running?(pid)
            kill(pid)
          end

        end
      end
    end
  else
    @logger.info "The Command `passenger-memory-stats` is not available"
  end

end
getpid(line) click to toggle source

Get the Process ID from the list

# File lib/passenger_memory_status.rb, line 75
def getpid(line)
  results = line.split
  pid, pid_memory = results[0].to_i, results[4].to_f
end
graceful(pid) click to toggle source

Graceful kill of passenger process

# File lib/passenger_memory_status.rb, line 61
def graceful(pid)
    @logger.info "Killing Passenger-Process #{pid} gracefully"
    Process.kill("SIGUSR1", pid)
end
kill(pid) click to toggle source

Forceful kill of the process

# File lib/passenger_memory_status.rb, line 68
def kill(pid)
    @logger.info "Killing Passenger-Process #{pid} forcefully"
    Process.kill("TERM", pid)
end
passenger_installed?() click to toggle source

Check if Passenger is installed

# File lib/passenger_memory_status.rb, line 82
def passenger_installed?
  installed_path = `which passenger_memory_status`
  return true unless installed_path.empty?
end
running?(pid) click to toggle source

Check if the process is running

# File lib/passenger_memory_status.rb, line 55
def running?(pid)
  return Process.getpid(pid) != -1
end