Class: RFlow::DaemonProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/rflow/daemon_process.rb

Overview

Encapsulates a master process being managed by RFlow that can run in the foreground or daemonize.

Direct Known Subclasses

Master

Constant Summary collapse

SIGINFO =

Symbolic constant for SIGINFO as this is only defined on BSD and not in Ruby.

29

Instance Method Summary collapse

Constructor Details

#initialize(name, role = name, options = {}) ⇒ DaemonProcess

Returns a new instance of DaemonProcess.



10
11
12
13
14
# File 'lib/rflow/daemon_process.rb', line 10

def initialize(name, role = name, options = {})
  @name = name
  @role = role
  @pid_file = PIDFile.new(options[:pid_file_path]) if options[:pid_file_path]
end

Instance Method Details

#daemonize!void

This method returns an undefined value.

Daemonize by forking and exiting the parent after handling IO streams and checking successful start of the new copy.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rflow/daemon_process.rb', line 19

def daemonize!
  RFlow.logger.info "#{@name} daemonizing"
  establish_daemon_pipe
  drop_database_connections

  parent = fork
  if parent
    exit_after_daemon_starts
  else
    daemonize_process
  end
end

#run!void

This method returns an undefined value.

Execute the master process. Writes out a pidfile and updates the process name, installs signal handlers, and spawns all the defined subprocesses. Finally executes #run_process; when that returns, it will exit with the resulting return code.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rflow/daemon_process.rb', line 37

def run!
  write_pid_file
  register_logging_context
  update_process_name
  handle_signals
  spawn_subprocesses
  signal_successful_start

  RFlow.logger.info "#{@role} started"
  run_process
ensure
  unhandle_signals
  remove_pid_file
end

#run_processvoid

This method returns an undefined value.

Default implementation. Subclasses should override to provide logic for actually doing something useful.



60
# File 'lib/rflow/daemon_process.rb', line 60

def run_process; end

#shutdown!(reason) ⇒ void

This method returns an undefined value.

Shut down the application. Cleans up the pid file, removes signal handlers, and signals all child processes with SIGQUIT.



69
70
71
72
73
74
75
# File 'lib/rflow/daemon_process.rb', line 69

def shutdown!(reason)
  RFlow.logger.info "#{@name} shutting down due to #{reason}"
  remove_pid_file
  unhandle_signals
  signal_subprocesses('QUIT')
  RFlow.logger.info "#{@name} exiting"
end

#spawn_subprocessesvoid

This method returns an undefined value.

Default implementation. Subclasses should override to provide logic for actually spawning subprocesses.



55
# File 'lib/rflow/daemon_process.rb', line 55

def spawn_subprocesses; end

#subprocessesArray<ChildProcess>

A list of ChildProcesses to start and signal.

Returns:



64
# File 'lib/rflow/daemon_process.rb', line 64

def subprocesses; []; end