Class: Perennial::Daemon

Inherits:
Object show all
Includes:
Loggable
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

Methods included from Loggable

included, #logger

Class Method Details

.alive?(pid) ⇒ Boolean

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

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
# File 'lib/perennial/daemon.rb', line 46

def alive?(pid)
  Process.kill(0, pid)
  return true
rescue Errno::ESRCH
  return false
rescue SystemCallError => e
  			return true
end

.any_alive?(type = :all) ⇒ Boolean

Returns:

  • (Boolean)


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

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.



87
88
89
90
# File 'lib/perennial/daemon.rb', line 87

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

.daemonize!(should_exit = true) ⇒ 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



69
70
71
72
73
74
75
76
# File 'lib/perennial/daemon.rb', line 69

def daemonize!(should_exit = true)
  if detached_fork { exit if should_exit }.nil?
    Process.setsid
    detached_fork { exit }
    reinitialize_stdio
    yield if block_given?
  end
end

.daemonize_current_type!Object



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

def daemonize_current_type!
  daemonize! do
    write_pid
    Perennial::Settings.verbose = false
  end
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.



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

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.



93
94
95
# File 'lib/perennial/daemon.rb', line 93

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