Class: Racecar::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/racecar/daemon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pidfile) ⇒ Daemon

Returns a new instance of Daemon.

Raises:



7
8
9
10
11
# File 'lib/racecar/daemon.rb', line 7

def initialize(pidfile)
  raise Racecar::Error, "No pidfile specified" if pidfile.nil?

  @pidfile = pidfile
end

Instance Attribute Details

#pidfileObject (readonly)

Returns the value of attribute pidfile.



5
6
7
# File 'lib/racecar/daemon.rb', line 5

def pidfile
  @pidfile
end

Instance Method Details

#check_pidObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/racecar/daemon.rb', line 44

def check_pid
  if pidfile?
    case pid_status
    when :running, :not_owned
      $stderr.puts "=> Racecar is already running with that PID file (#{pidfile})"
      exit(1)
    when :dead
      File.delete(pidfile)
    end
  end
end

#daemonize!Object



17
18
19
20
21
22
# File 'lib/racecar/daemon.rb', line 17

def daemonize!
  exit if fork
  Process.setsid
  exit if fork
  Dir.chdir "/"
end

#pidObject



56
57
58
59
60
61
62
# File 'lib/racecar/daemon.rb', line 56

def pid
  if File.exist?(pidfile)
    File.read(pidfile).to_i
  else
    nil
  end
end

#pid_statusObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/racecar/daemon.rb', line 72

def pid_status
  return :exited if pid.nil?
  return :dead if pid == 0

  # This will raise Errno::ESRCH if the process doesn't exist.
  Process.kill(0, pid)

  :running
rescue Errno::ESRCH
  :dead
rescue Errno::EPERM
  :not_owned
end

#pidfile?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/racecar/daemon.rb', line 13

def pidfile?
  !pidfile.nil?
end

#redirect_output(logfile) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/racecar/daemon.rb', line 24

def redirect_output(logfile)
  FileUtils.mkdir_p(File.dirname(logfile), mode: 0755)
  FileUtils.touch(logfile)

  File.chmod(0644, logfile)

  $stderr.reopen(logfile, 'a')
  $stdout.reopen($stderr)
  $stdout.sync = $stderr.sync = true
end

#running?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/racecar/daemon.rb', line 64

def running?
  pid_status == :running || pid_status == :not_owned
end

#stop!Object



68
69
70
# File 'lib/racecar/daemon.rb', line 68

def stop!
  Process.kill("TERM", pid)
end

#suppress_inputObject



35
36
37
# File 'lib/racecar/daemon.rb', line 35

def suppress_input
  $stdin.reopen('/dev/null')
end

#suppress_outputObject



39
40
41
42
# File 'lib/racecar/daemon.rb', line 39

def suppress_output
  $stderr.reopen('/dev/null', 'a')
  $stdout.reopen($stderr)
end

#write_pidObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/racecar/daemon.rb', line 86

def write_pid
  File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) do |f|
    f.write(Process.pid.to_s)
  end

  at_exit do
    File.delete(pidfile) if File.exist?(pidfile)
  end
rescue Errno::EEXIST
  check_pid
  retry
end