Class: Perennial::Daemon

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

Overview

Perennial::Daemon provides a relatively simple

interface to creating unix daemons from a given process. Namely, it focuses on three providing tools for a couple of simple purposes.

Turning a process into a daemon

This approach is as as simple as calling Perennial::Daemon.daemonize! - the current process will then be a daemon, the non-daemon processes will be killed and if started by the loader, it will also write a pid out.

Checking whether a process is alive

Perennial::Daemon.alive?(pid) is a super simple way to check if a pid (as an integer) is current active / alive,

Perennial::Daemon.any_alive? takes a type and tells you if any processes with the associated type are alive.

Storing / Retrieving Pids for a Given Loader Type

Perennial::Daemon.daemonize! will store the pid for the current process if Perennial::Loader.current_type is present.

Perennial::Daemon.any_alive?(type = :all) and Perennial::Daemon.kill_all(type = :all) can be used to deal with checking the status of and killing processes associated with a given loader type.

Class Method Summary collapse

Class Method Details

.alive?(pid) ⇒ Boolean

Returns true / false depending on whether a process with the given pid exists.

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/perennial/daemon.rb', line 44

def alive?(pid)
  return Process.getpgid(pid) != -1
rescue Errno::ESRCH
  return false
end

.any_alive?(type = :all) ⇒ Boolean

Returns:

  • (Boolean)


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

def any_alive?(type = :all)
  !pid_file_for(type).empty?
end

.cleanup!Object

Cleans up processes for the current application type (deteremined by the loader) and then removes the pid file.



78
79
80
81
# File 'lib/perennial/daemon.rb', line 78

def cleanup!
  f = pids_file_for(Loader.current_type)
  FileUtils.rm_f(f) if (pids_from(f) - Process.pid).blank?
end

.daemonize!Object

Converts the current process into a Unix-daemon using the double fork approach. Also, changes process file mask to 000 and reopens STDIN / OUT to /dev/null



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/perennial/daemon.rb', line 64

def daemonize!
  fork_off_and_die
  Process.setsid
  fork_off_and_die
  self.write_pid
  File.umask    0000
  STDIN.reopen  "/dev/null"
  STDOUT.reopen "/dev/null", "a"
  STDERR.reopen STDOUT
  Perennial::Settings.verbose = false
end

.kill_all(type = :all) ⇒ Object

Kills all processes associated with a certain app type. E.g. Given a loader is starting a :client, kill_all(:client) would kill all associated processes (usually after daemonizing) and kill_all would do the same - but if we also started a process for :server, kill_all(:client wouldn’t kill the the process where as kill_all would.



56
57
58
59
# File 'lib/perennial/daemon.rb', line 56

def kill_all(type = :all)
  kill_all_from(pid_file_for(type))
  return false
end

.pids_for_type(type) ⇒ Object

Returns an array of pid’s associated with a given type.



84
85
86
# File 'lib/perennial/daemon.rb', line 84

def pids_for_type(type)
  pids_from(pid_file_for(type))
end