Sha256: 9a320cb6e0041ff7cef67d18022ad969abbe6609efcbe973a8edc62f3d1480c8
Contents?: true
Size: 1.42 KB
Versions: 8
Compression:
Stored size: 1.42 KB
Contents
module GoodJob # # Manages daemonization of the current process. # class Daemon # The path of the generated pidfile. # @return [Pathname,String] attr_reader :pidfile # @param pidfile [Pathname,String] Pidfile path def initialize(pidfile:) @pidfile = pidfile end # Daemonizes the current process and writes out a pidfile. # @return [void] def daemonize check_pid Process.daemon write_pid end private # @return [void] def write_pid File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) { |f| f.write(Process.pid.to_s) } at_exit { File.delete(pidfile) if File.exist?(pidfile) } rescue Errno::EEXIST check_pid retry end # @return [void] def delete_pid File.delete(pidfile) if File.exist?(pidfile) end # @return [void] def check_pid case pid_status(pidfile) when :running, :not_owned abort "A server is already running. Check #{pidfile}" when :dead File.delete(pidfile) end end # @param pidfile [Pathname, String] # @return [Symbol] def pid_status(pidfile) return :exited unless File.exist?(pidfile) pid = ::File.read(pidfile).to_i return :dead if pid.zero? Process.kill(0, pid) # check process status :running rescue Errno::ESRCH :dead rescue Errno::EPERM :not_owned end end end
Version data entries
8 entries across 8 versions & 1 rubygems