Class: PassengerMemoryStatus::MemoryStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/passenger_memory_status.rb

Constant Summary collapse

MEMORY_LIMIT =

Memory in MB

500

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MemoryStatus

Returns a new instance of MemoryStatus.



11
12
13
14
# 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

Class Method Details

.run(options = {}) ⇒ Object



16
17
18
# File 'lib/passenger_memory_status.rb', line 16

def self.run(options = {})
  new(options).bloated_passenger_process
end

Instance Method Details

#bloated_passenger_processObject

Find and kill the Bloated Passenger Process



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object

Get the Process ID from the list



75
76
77
78
# 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) ⇒ Object

Graceful kill of passenger process



61
62
63
64
# 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) ⇒ Object

Forceful kill of the process



68
69
70
71
# 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?Boolean

Check if Passenger is installed

Returns:

  • (Boolean)


82
83
84
85
# 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) ⇒ Boolean

Check if the process is running

Returns:

  • (Boolean)


55
56
57
# File 'lib/passenger_memory_status.rb', line 55

def running?(pid)
  return Process.getpid(pid) != -1
end