Class: Qs::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/qs/process.rb

Constant Summary collapse

HALT =
'H'.freeze
STOP =
'S'.freeze
RESTART =
'R'.freeze
WAIT_FOR_SIGNALS_TIMEOUT =
15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(daemon, options = nil) ⇒ Process

Returns a new instance of Process.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/qs/process.rb', line 17

def initialize(daemon, options = nil)
  options ||= {}
  @daemon = daemon
  @name   = "qs: #{@daemon.process_label}"
  @logger = @daemon.logger

  @pid_file    = PIDFile.new(@daemon.pid_file)
  @signal_io   = IOPipe.new
  @restart_cmd = RestartCmd.new

  skip_daemonize = ignore_if_blank(ENV['QS_SKIP_DAEMONIZE'])
  @daemonize = !!options[:daemonize] && !skip_daemonize
end

Instance Attribute Details

#daemonObject (readonly)

Returns the value of attribute daemon.



14
15
16
# File 'lib/qs/process.rb', line 14

def daemon
  @daemon
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/qs/process.rb', line 14

def name
  @name
end

#pid_fileObject (readonly)

Returns the value of attribute pid_file.



15
16
17
# File 'lib/qs/process.rb', line 15

def pid_file
  @pid_file
end

#restart_cmdObject (readonly)

Returns the value of attribute restart_cmd.



15
16
17
# File 'lib/qs/process.rb', line 15

def restart_cmd
  @restart_cmd
end

#signal_ioObject (readonly)

Returns the value of attribute signal_io.



15
16
17
# File 'lib/qs/process.rb', line 15

def signal_io
  @signal_io
end

Instance Method Details

#daemonize?Boolean

Returns:

  • (Boolean)


54
# File 'lib/qs/process.rb', line 54

def daemonize?; @daemonize; end

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/qs/process.rb', line 31

def run
  ::Process.daemon(true) if self.daemonize?
  log "Starting Qs daemon for #{@daemon.name}"

  $0 = @name
  @pid_file.write
  log "PID: #{@pid_file.pid}"

  @signal_io.setup
  trap_signals(@signal_io)

  start_daemon(@daemon)

  signal = catch(:signal) do
    wait_for_signals(@signal_io, @daemon)
  end
  @signal_io.teardown

  run_restart_cmd(@daemon, @restart_cmd) if signal == RESTART
ensure
  @pid_file.remove
end