Module: Puma::JRubyRestart

Extended by:
FFI::Library
Defined in:
lib/puma/jruby_restart.rb

Constant Summary collapse

PermKey =
'PUMA_DAEMON_PERM'
RestartKey =
'PUMA_DAEMON_RESTART'

Class Method Summary collapse

Class Method Details

.chdir_exec(dir, argv) ⇒ Object

Raises:

  • (SystemCallError)


14
15
16
17
18
19
20
21
22
# File 'lib/puma/jruby_restart.rb', line 14

def self.chdir_exec(dir, argv)
  chdir(dir)
  cmd = argv.first
  argv = ([:string] * argv.size).zip(argv).flatten
  argv << :string
  argv << nil
  execlp(cmd, *argv)
  raise SystemCallError.new(FFI.errno)
end

.daemon?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/puma/jruby_restart.rb', line 34

def self.daemon?
  ENV.key?(PermKey) || ENV.key?(RestartKey)
end

.daemon_initObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puma/jruby_restart.rb', line 38

def self.daemon_init
  return true if ENV.key?(PermKey)

  return false unless ENV.key? RestartKey

  master = ENV[RestartKey]

  # In case the master disappears early
  begin
    Process.kill "SIGUSR2", master.to_i
  rescue SystemCallError => e
  end

  ENV[RestartKey] = ""

  setsid

  null = File.open "/dev/null", "w+"
  STDIN.reopen null
  STDOUT.reopen null
  STDERR.reopen null

  true
end

.daemon_start(dir, argv) ⇒ Object

Raises:

  • (SystemCallError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puma/jruby_restart.rb', line 63

def self.daemon_start(dir, argv)
  ENV['PUMA_DAEMON_RESTART'] = Process.pid.to_s

  if k = ENV['PUMA_JRUBY_DAEMON_OPTS']
    ENV['JRUBY_OPTS'] = k
  end

  cmd = argv.first
  argv = ([:string] * argv.size).zip(argv).flatten
  argv << :string
  argv << nil

  chdir(dir)
  ret = fork
  return ret if ret != 0
  execlp(cmd, *argv)
  raise SystemCallError.new(FFI.errno)
end

.perm_daemonizeObject

Called to tell things “Your now always in daemon mode, don’t try to reenter it.”



30
31
32
# File 'lib/puma/jruby_restart.rb', line 30

def self.perm_daemonize
  ENV[PermKey] = "1"
end