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:



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

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.



3
4
5
# File 'lib/racecar/daemon.rb', line 3

def pidfile
  @pidfile
end

Instance Method Details

#check_pidObject



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

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



15
16
17
18
19
20
# File 'lib/racecar/daemon.rb', line 15

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

#pidObject



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

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

#pid_statusObject



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

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)


11
12
13
# File 'lib/racecar/daemon.rb', line 11

def pidfile?
  !pidfile.nil?
end

#redirect_output(logfile) ⇒ Object



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

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)


62
63
64
# File 'lib/racecar/daemon.rb', line 62

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

#stop!Object



66
67
68
# File 'lib/racecar/daemon.rb', line 66

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

#suppress_inputObject



33
34
35
# File 'lib/racecar/daemon.rb', line 33

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

#suppress_outputObject



37
38
39
40
# File 'lib/racecar/daemon.rb', line 37

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

#write_pidObject



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

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.exists?(pidfile)
  end
rescue Errno::EEXIST
  check_pid
  retry
end