rbg 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/rbg.rb +41 -14
  2. data/lib/rbg/config.rb +13 -3
  3. metadata +2 -2
data/lib/rbg.rb CHANGED
@@ -63,14 +63,37 @@ module Rbg
63
63
  sleep 2
64
64
  child_processes.dup.each do |id, opts|
65
65
  begin
66
+
66
67
  Process.getpgid(opts[:pid])
68
+
69
+ if config.memory_limit
70
+ # Lookup the memory usge for this PID
71
+ memory_usage = `ps -o rss= -p #{opts[:pid]}`.strip.to_i / 1024
72
+ if memory_usage > config.memory_limit
73
+ puts "#{self.config.name}[#{id}] is using #{memory_usage}MB of memory (limit: #{config.memory_limit}MB). It will be killed."
74
+ kill_child_process(id)
75
+ end
76
+ end
77
+
67
78
  rescue Errno::ESRCH
68
79
  puts "Child process #{config.name}[#{id}] has died (from PID #{opts[:pid]})"
69
80
  child_processes[id][:pid] = nil
70
81
 
71
- if config.respawn_limit > opts[:respawns]
72
- child_processes[id][:respawns] += 1
73
- fork_worker(id)
82
+ if config.respawn
83
+ if opts[:started_at] > Time.now - config.respawn_limits[1]
84
+ if opts[:respawns] >= config.respawn_limits[0]
85
+ puts "Process #{config.name}[#{id}] has instantly respawned #{opts[:respawns]} times. It won't be respawned again."
86
+ child_processes.delete(id)
87
+ else
88
+ puts "Process has died within #{config.respawn_limits[1]}s of the last spawn."
89
+ child_processes[id][:respawns] += 1
90
+ fork_worker(id)
91
+ end
92
+ else
93
+ puts "Process was started more than #{config.respawn_limits[1]}s since the last spawn. Resetting spawn counter"
94
+ child_processes[id][:respawns] = 0
95
+ fork_worker(id)
96
+ end
74
97
  else
75
98
  child_processes.delete(id)
76
99
  end
@@ -128,16 +151,15 @@ module Rbg
128
151
  Process.detach(pid)
129
152
 
130
153
  # Save the worker PID into the Parent's child process list
131
- self.child_processes[id] ||= {}
132
- self.child_processes[id][:pid] ||= pid
133
- self.child_processes[id][:respawns] ||= 0
154
+ self.child_processes[id] ||= {}
155
+ self.child_processes[id][:pid] ||= pid
156
+ self.child_processes[id][:respawns] ||= 0
157
+ self.child_processes[id][:started_at] = Time.now
134
158
  end
135
159
 
136
- # Kill all child processes
137
- def kill_child_processes
138
- puts 'Killing child processes...'
139
- STDOUT.flush
140
- self.child_processes.each do |id, opts|
160
+ # Kill a given child process
161
+ def kill_child_process(id)
162
+ if opts = self.child_processes[id]
141
163
  puts "Killing #{config.name}[#{id}] (with PID #{opts[:pid]})"
142
164
  STDOUT.flush
143
165
  begin
@@ -146,7 +168,13 @@ module Rbg
146
168
  puts "Process already gone away"
147
169
  end
148
170
  end
149
- # Clear the child process list because we just killed them all
171
+ end
172
+
173
+ # Kill all child processes
174
+ def kill_child_processes
175
+ puts 'Killing child processes...'
176
+ STDOUT.flush
177
+ self.child_processes.keys.each { |id| kill_child_process(id) }
150
178
  self.child_processes = Hash.new
151
179
  end
152
180
 
@@ -278,11 +306,10 @@ module Rbg
278
306
  def pid_from_file
279
307
  raise Error, "PID not defined in '#{config_file}'" unless self.config.pid_path
280
308
  begin
281
- pid = File.read(self.config.pid_path).strip.to_i
309
+ File.read(self.config.pid_path).strip.to_i
282
310
  rescue
283
311
  raise Error, "PID file not found"
284
312
  end
285
- return pid
286
313
  end
287
314
 
288
315
  # Stop the running instance
@@ -7,7 +7,9 @@ module Rbg
7
7
  attr_accessor :log_path
8
8
  attr_accessor :pid_path
9
9
  attr_accessor :workers
10
- attr_accessor :respawn_limit
10
+ attr_accessor :respawn
11
+ attr_accessor :respawn_limits
12
+ attr_accessor :memory_limit
11
13
 
12
14
  def root
13
15
  @root || File.expand_path('./')
@@ -25,8 +27,16 @@ module Rbg
25
27
  block_given? ? @script = block : @script
26
28
  end
27
29
 
28
- def respawn_limit
29
- @respawn_limit || 0
30
+ def respawn
31
+ @respawn || false
32
+ end
33
+
34
+ def respawn_limits
35
+ @respawn_limits || [5, 30]
36
+ end
37
+
38
+ def memory_limit
39
+ @memory_limit || nil
30
40
  end
31
41
 
32
42
  def before_fork(&block)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-21 00:00:00.000000000 Z
12
+ date: 2014-02-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: charlie@atechmedia.com