Class: StarlingServer::ProcessHelper
- Inherits:
-
Object
- Object
- StarlingServer::ProcessHelper
- Defined in:
- lib/starling/server_runner.rb
Instance Method Summary collapse
- #close_io_handles ⇒ Object
- #daemonize ⇒ Object
- #detach_from_terminal ⇒ Object
-
#initialize(log_file = nil, pid_file = nil, user = nil, group = nil) ⇒ ProcessHelper
constructor
A new instance of ProcessHelper.
- #redirect_io ⇒ Object
- #remove_pid_file ⇒ Object
- #rescue_exception ⇒ Object
- #running? ⇒ Boolean
- #safefork ⇒ Object
- #write_pid_file ⇒ Object
Constructor Details
#initialize(log_file = nil, pid_file = nil, user = nil, group = nil) ⇒ ProcessHelper
Returns a new instance of ProcessHelper.
183 184 185 186 187 188 |
# File 'lib/starling/server_runner.rb', line 183 def initialize(log_file = nil, pid_file = nil, user = nil, group = nil) @log_file = log_file @pid_file = pid_file @user = user @group = group end |
Instance Method Details
#close_io_handles ⇒ Object
227 228 229 230 231 232 233 234 235 236 |
# File 'lib/starling/server_runner.rb', line 227 def close_io_handles ObjectSpace.each_object(IO) do |io| unless [STDIN, STDOUT, STDERR].include?(io) begin io.close unless io.closed? rescue Exception end end end end |
#daemonize ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/starling/server_runner.rb', line 201 def daemonize sess_id = detach_from_terminal exit if pid = safefork Dir.chdir("/") File.umask 0000 close_io_handles redirect_io return sess_id end |
#detach_from_terminal ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/starling/server_runner.rb', line 214 def detach_from_terminal srand safefork and exit unless sess_id = Process.setsid raise "Couldn't detache from controlling terminal." end trap 'SIGHUP', 'IGNORE' sess_id end |
#redirect_io ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/starling/server_runner.rb', line 238 def redirect_io begin; STDIN.reopen('/dev/null'); rescue Exception; end if @log_file begin STDOUT.reopen(@log_file, "a") STDOUT.sync = true rescue Exception begin; STDOUT.reopen('/dev/null'); rescue Exception; end end else begin; STDOUT.reopen('/dev/null'); rescue Exception; end end begin; STDERR.reopen(STDOUT); rescue Exception; end STDERR.sync = true end |
#remove_pid_file ⇒ Object
270 271 272 273 |
# File 'lib/starling/server_runner.rb', line 270 def remove_pid_file return unless @pid_file File.unlink(@pid_file) if File.exists?(@pid_file) end |
#rescue_exception ⇒ Object
256 257 258 259 260 261 |
# File 'lib/starling/server_runner.rb', line 256 def rescue_exception begin yield rescue Exception end end |
#running? ⇒ Boolean
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/starling/server_runner.rb', line 275 def running? return false unless @pid_file pid = File.read(@pid_file).chomp.to_i rescue nil pid = nil if pid == 0 return false unless pid begin Process.kill(0, pid) return pid rescue Errno::ESRCH return nil rescue Errno::EPERM return pid end end |
#safefork ⇒ Object
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/starling/server_runner.rb', line 190 def safefork begin if pid = fork return pid end rescue Errno::EWOULDBLOCK sleep 5 retry end end |
#write_pid_file ⇒ Object
263 264 265 266 267 268 |
# File 'lib/starling/server_runner.rb', line 263 def write_pid_file return unless @pid_file FileUtils.mkdir_p(File.dirname(@pid_file)) File.open(@pid_file, "w") { |f| f.write(Process.pid) } File.chmod(0644, @pid_file) end |