passenger_reaper 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/passenger_reaper CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'passenger_reaper'
4
- PassengerReaper::Runner.run(ARGV)
4
+ PassengerReaper::Runner.run
@@ -3,21 +3,30 @@ require "passenger_reaper/ps_etime"
3
3
  require "passenger_reaper/passenger_process"
4
4
 
5
5
  module PassengerReaper
6
- MINIMUM_ETIME = 600 # 10 minutes
7
-
8
6
  class Runner
9
- def self.run(args)
10
- if %W(active inactive debug status).include?(ARGV.first)
11
- case ARGV.first
12
- when 'active'
13
- PassengerProcess.kill_stale_passengers
14
- when 'inactive'
15
- PassengerProcess.kill_inactive_passengers
16
- when 'debug'
17
- PassengerProcess.inactive_passengers_last_log_entry
18
- when 'status'
19
- puts "Total of passengers: #{PassengerProcess.all_passenger_pids.count}"
20
- puts "Stale passengers: #{PassengerProcess.stale.count}"
7
+ VALID_COMMANDS = %w(old active inactive all debug status)
8
+
9
+ def self.run
10
+ commands = ARGV.select {|cmd| VALID_COMMANDS.include?(cmd) }
11
+ unless commands.empty?
12
+ commands.each do |arg|
13
+ case arg
14
+ when 'old'
15
+ PassengerProcess.kill_old_passengers
16
+ when 'active'
17
+ PassengerProcess.kill_stale_passengers
18
+ when 'inactive'
19
+ PassengerProcess.kill_inactive_passengers
20
+ when 'all'
21
+ PassengerProcess.kill_old_passengers
22
+ PassengerProcess.kill_stale_passengers
23
+ PassengerProcess.kill_inactive_passengers
24
+ when 'debug'
25
+ PassengerProcess.inactive_passengers_last_log_entry
26
+ when 'status'
27
+ puts "Total of passengers: #{PassengerProcess.all_passenger_pids.count}"
28
+ puts "Stale passengers: #{PassengerProcess.stale.count}"
29
+ end
21
30
  end
22
31
  else
23
32
  help =<<-EOF
@@ -28,8 +37,10 @@ Error: please use the following syntax:
28
37
  Commands:
29
38
 
30
39
  status displays the number of total passenger workers and the number of active workers
40
+ old kills old workers that passengers has in the pool
31
41
  active kills stale workers that passengers has in the pool
32
42
  inactive kills workers that passenger no longer controls
43
+ all kills both active, inactive & old workers
33
44
  debug shows the last log entry from each inactive worker
34
45
 
35
46
  Options:
@@ -2,6 +2,8 @@ require 'chronic'
2
2
 
3
3
  module PassengerReaper
4
4
  class PassengerProcess
5
+ MINIMUM_ETIME = 600 # 10 minutes
6
+
5
7
  attr_accessor :pid, :uptime
6
8
 
7
9
  def initialize(passenger_status_line)
@@ -31,6 +33,15 @@ module PassengerReaper
31
33
  passengers
32
34
  end
33
35
 
36
+ def self.old
37
+ passengers = []
38
+ passenger_status.each_line do |line|
39
+ ps = PassengerProcess.new(line)
40
+ passengers << ps if ps.uptime_in_seconds > MINIMUM_ETIME
41
+ end
42
+ passengers
43
+ end
44
+
34
45
  def self.stale
35
46
  stale_passengers = []
36
47
  potentially_stale_processes = active.select { |p| p.uptime_in_seconds > 600 }
@@ -46,8 +57,7 @@ module PassengerReaper
46
57
  end
47
58
 
48
59
  def self.last_log_entry(pid)
49
- pwd = `pwd`.chomp
50
- `grep 'rails\\[#{pid}\\]' #{pwd}/log/production.log | tail -n 1`.chomp
60
+ `grep 'rails\\[#{pid}\\]' #{Dir.pwd}/log/production.log | tail -n 1`.chomp
51
61
  end
52
62
 
53
63
  def self.last_log_entry_time(pid)
@@ -91,11 +101,27 @@ module PassengerReaper
91
101
  inactive_passenger_pids
92
102
  end
93
103
 
104
+ def self.old_passenger_pids
105
+ old_passenger_pids = []
106
+ all_passenger_pids.each do |pid|
107
+ raw_etime = `ps -p #{pid} --no-headers -o etime`.chomp
108
+ etime = PsEtime.new(raw_etime)
109
+ old_passenger_pids << pid unless (etime.age_in_seconds < (MINIMUM_ETIME || 600))
110
+ end
111
+ old_passenger_pids
112
+ end
113
+
94
114
  def self.kill_inactive_passengers
95
115
  inactive_passenger_pids.each do |pid|
96
116
  kill(pid)
97
117
  end
98
118
  end
119
+
120
+ def self.kill_old_passengers
121
+ old.each do |ps|
122
+ kill(ps.pid)
123
+ end
124
+ end
99
125
 
100
126
  def self.kill_stale_passengers
101
127
  stale.each do |p|
@@ -1,3 +1,3 @@
1
1
  module PassengerReaper
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -20,6 +20,12 @@ EOF
20
20
  passenger.uptime.should eql('40s')
21
21
  end
22
22
 
23
+ it "should return old passenger processes" do
24
+ old_pids = PassengerReaper::PassengerProcess.old
25
+ old_pids.count.should eql(2)
26
+ old_pids.map{|p| p.pid}.should eql([14521, 14525])
27
+ end
28
+
23
29
  it "should parse the uptime into seconds" do
24
30
  all_passenger_processes = PassengerReaper::PassengerProcess.active
25
31
  all_passenger_processes[0].uptime_in_seconds.should eql(40)
data/spec/test_helper.rb CHANGED
@@ -1,2 +1 @@
1
- #require 'minitest/autorun'
2
1
  require 'passenger_reaper'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passenger_reaper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-06 00:00:00.000000000 Z
12
+ date: 2012-03-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chronic
16
- requirement: &70248854448020 !ruby/object:Gem::Requirement
16
+ requirement: &70093686377000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70248854448020
24
+ version_requirements: *70093686377000
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70248854447440 !ruby/object:Gem::Requirement
27
+ requirement: &70093686376280 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70248854447440
35
+ version_requirements: *70093686376280
36
36
  description: Gemification of https://gist.github.com/596401/db750535df61e679aad69e8d9c9750f8640a234f.
37
37
  email:
38
38
  - joshbuddy@gmail.com
@@ -65,12 +65,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
65
  - - ! '>='
66
66
  - !ruby/object:Gem::Version
67
67
  version: '0'
68
+ segments:
69
+ - 0
70
+ hash: 3239212710653592446
68
71
  required_rubygems_version: !ruby/object:Gem::Requirement
69
72
  none: false
70
73
  requirements:
71
74
  - - ! '>='
72
75
  - !ruby/object:Gem::Version
73
76
  version: '0'
77
+ segments:
78
+ - 0
79
+ hash: 3239212710653592446
74
80
  requirements: []
75
81
  rubyforge_project: passenger_reaper
76
82
  rubygems_version: 1.8.15
@@ -80,4 +86,3 @@ summary: Gemification of https://gist.github.com/596401/db750535df61e679aad69e8d
80
86
  test_files:
81
87
  - spec/passenger_reaper_spec.rb
82
88
  - spec/test_helper.rb
83
- has_rdoc: