rbg 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rbg.rb +40 -6
  2. metadata +4 -4
data/lib/rbg.rb CHANGED
@@ -25,7 +25,7 @@ module Rbg
25
25
  self.child_processes = Array.new
26
26
 
27
27
  # Set the process name (Parent)
28
- $0="#{self.config.name}[P]"
28
+ $0="#{self.config.name}[Parent]"
29
29
 
30
30
  # Debug information
31
31
  puts "New parent process: #{Process.pid}"
@@ -55,12 +55,26 @@ module Rbg
55
55
  Signal.trap('INT', proc {})
56
56
 
57
57
  # Parent loop, the purpose of this is simply to do nothing until we get a signal
58
+ # We will exit if all child processes die
58
59
  # We may add memory management code here in the future
59
60
  loop do
60
- sleep 1
61
+ sleep 2
62
+ self.child_processes.dup.each do |p|
63
+ begin
64
+ Process.getpgid( p )
65
+ rescue Errno::ESRCH
66
+ puts "Child process #{p} has died"
67
+ child_processes.delete(p)
68
+ end
69
+ end
70
+ if child_processes.empty?
71
+ puts "All child processes died, exiting parent"
72
+ Process.exit(0)
73
+ end
61
74
  end
62
-
63
75
  end
76
+ # Ensure the new parent is detached
77
+ Process.detach(self.child_processes.last)
64
78
  end
65
79
 
66
80
  # Wrapper to fork multiple workers
@@ -123,7 +137,7 @@ module Rbg
123
137
  STDOUT.flush
124
138
 
125
139
  # Set the process name
126
- $0="#{self.config.name}[M]"
140
+ $0="#{self.config.name}[Master]"
127
141
 
128
142
  # Fork a Parent process
129
143
  # This will load the before_fork in a clean process then fork the script as required
@@ -154,9 +168,17 @@ module Rbg
154
168
  Process.exit(0)
155
169
  })
156
170
 
157
- # Main loop, the purpose of this is simply to do nothing until we get a signal
171
+ # Main loop, we mostly idle, but check if the parent we created has died and exit
158
172
  loop do
159
- sleep 1
173
+ sleep 2
174
+ self.child_processes.each do |p|
175
+ begin
176
+ Process.getpgid( p )
177
+ rescue Errno::ESRCH
178
+ puts "Parent process #{p} has died, exiting master"
179
+ Process.exit(0)
180
+ end
181
+ end
160
182
  end
161
183
  end
162
184
 
@@ -179,6 +201,18 @@ module Rbg
179
201
  self.config_file = config_file
180
202
  self.load_config
181
203
 
204
+ # If the PID file is set and exists, check that the process is not running
205
+ if self.config.pid_path and File.exists?(self.config.pid_path)
206
+ oldpid = File.read(self.config.pid_path)
207
+ begin
208
+ Process.getpgid( oldpid.to_i )
209
+ raise Error, "Process already running! PID #{oldpid}"
210
+ rescue Errno::ESRCH
211
+ # No running process
212
+ false
213
+ end
214
+ end
215
+
182
216
  # Initialize child process array
183
217
  self.child_processes = Array.new
184
218
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbg
3
3
  version: !ruby/object:Gem::Version
4
- hash: 57
4
+ hash: 63
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 1
10
- version: 0.9.1
9
+ - 2
10
+ version: 0.9.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Charlie Smurthwaite
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-25 00:00:00 +01:00
18
+ date: 2010-10-26 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21