Class: StarlingServer::ProcessHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/starling/server_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(log_file = nil, pid_file = nil, user = nil, group = nil) ⇒ ProcessHelper

Returns a new instance of ProcessHelper.



193
194
195
196
197
198
# File 'lib/starling/server_runner.rb', line 193

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_handlesObject



237
238
239
240
241
242
243
244
245
246
# File 'lib/starling/server_runner.rb', line 237

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

#daemonizeObject



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/starling/server_runner.rb', line 211

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_terminalObject



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/starling/server_runner.rb', line 224

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_ioObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/starling/server_runner.rb', line 248

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_fileObject



280
281
282
283
# File 'lib/starling/server_runner.rb', line 280

def remove_pid_file
  return unless @pid_file
  File.unlink(@pid_file) if File.exists?(@pid_file)
end

#rescue_exceptionObject



266
267
268
269
270
271
# File 'lib/starling/server_runner.rb', line 266

def rescue_exception
  begin
    yield
  rescue Exception
  end
end

#running?Boolean

Returns:

  • (Boolean)


285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/starling/server_runner.rb', line 285

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

#safeforkObject



200
201
202
203
204
205
206
207
208
209
# File 'lib/starling/server_runner.rb', line 200

def safefork
  begin
    if pid = fork
      return pid
    end
  rescue Errno::EWOULDBLOCK
    sleep 5
    retry
  end
end

#write_pid_fileObject



273
274
275
276
277
278
# File 'lib/starling/server_runner.rb', line 273

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