Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a7137494c9
Fetching contributors…

Cannot retrieve contributors at this time

executable file 110 lines (96 sloc) 4.19 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#!/usr/bin/env ruby
# Phusion Passenger - http://www.modrails.com/
# Copyright (c) 2010 Phusion
#
# "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# 2012, Louis Opter <louis@dotcloud.com>, create a script inspired by
# passenger-status that kill stuck Ruby processes

source_root = File.expand_path(File.dirname(__FILE__) + "/..")
$LOAD_PATH.unshift("#{source_root}/lib")
require 'phusion_passenger'
require 'phusion_passenger/platform_info'
require 'phusion_passenger/admin_tools/server_instance'

include PhusionPassenger::AdminTools

# Each check_interval seconds this daemon inspects the Ruby processes launched
# by Passenger. If a Ruby process didn't handled more request than at the last
# check this daemon sigkill it.
#
# Note: the first processes will be killed after 2 * CHECK_INTERVAL seconds.
$CHECK_INTERVAL = 60

def watch_processes(server_instance)
    server_instance.connect(:passenger_status) do
        last = {}
        while 1 do
            current = {}

            # Inspect each Rack process in each Passenger group
            server_instance.groups.each do |group|
                current[group.name] = {}

                group.processes.each do |process|
                    # We don't care about idle processes
                    if process.sessions > 0

                        # Check if we know this process already and if it is stuck
                        if last.has_key?(group.name) and last[group.name].has_key?(process.pid) \
                           and last[group.name][process.pid] == process.processed
                            puts "Killing stuck process #{process.pid} (processed #{process.processed} requests)"
                            Process.kill("KILL", process.pid)
                        else
                            # Refresh its status in our hash
                            current[group.name][process.pid] = process.processed
                        end

                    end
                end

            end

            last = current
            sleep $CHECK_INTERVAL
        end
    end
end

def run
    if ARGV.empty?
        while 1 do
            server_instances = ServerInstance.list
            if server_instances.empty?
                STDERR.puts("ERROR: Phusion Passenger doesn't seem to be running, retrying in #{$CHECK_INTERVAL}s.")
                sleep $CHECK_INTERVAL
            else
                break
            end
        end

        if server_instances.size == 1
            watch_processes(server_instances.first)
        else
            puts "It appears that multiple Passenger instances are running. Please select a"
            puts "specific one by running:"
            puts
            puts " passenger-kill-stuck-workers <PID>"
            puts
            puts "The following Passenger instances are running:"
            server_instances.each do |instance|
                puts " PID: #{instance.pid}"
            end
            exit 1
        end
    else
        server_instance = ServerInstance.for_pid(ARGV[0].to_i)
        watch_processes(server_instance)
    end
end

run
Something went wrong with that request. Please try again.