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.
208 209 210 211 212 213 |
# File 'lib/starling/server_runner.rb', line 208 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
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/starling/server_runner.rb', line 252 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
226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/starling/server_runner.rb', line 226 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
239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/starling/server_runner.rb', line 239 def detach_from_terminal srand safefork and exit unless sess_id = Process.setsid raise "Couldn't detach from controlling terminal." end trap 'SIGHUP', 'IGNORE' sess_id end |
#redirect_io ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/starling/server_runner.rb', line 263 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
295 296 297 298 |
# File 'lib/starling/server_runner.rb', line 295 def remove_pid_file return unless @pid_file File.unlink(@pid_file) if File.exists?(@pid_file) end |
#rescue_exception ⇒ Object
281 282 283 284 285 286 |
# File 'lib/starling/server_runner.rb', line 281 def rescue_exception begin yield rescue Exception end end |
#running? ⇒ Boolean
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/starling/server_runner.rb', line 300 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
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/starling/server_runner.rb', line 215 def safefork begin if pid = fork return pid end rescue Errno::EWOULDBLOCK sleep 5 retry end end |
#write_pid_file ⇒ Object
288 289 290 291 292 293 |
# File 'lib/starling/server_runner.rb', line 288 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 |