Class: Exekutor::Internal::CLI::Daemon
- Inherits:
-
Object
- Object
- Exekutor::Internal::CLI::Daemon
- Defined in:
- lib/exekutor/internal/cli/daemon.rb
Overview
Manages daemonization of the current process.
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#pidfile ⇒ String
readonly
The path of the generated pidfile.
Instance Method Summary collapse
-
#daemonize ⇒ void
Daemonizes the current process and writes out a pidfile.
-
#initialize(pidfile:) ⇒ Daemon
constructor
A new instance of Daemon.
-
#pid ⇒ Integer?
The process ID for this daemon, if known.
-
#status ⇒ :running, ...
The process status for this daemon.
-
#status?(*statuses) ⇒ Boolean
Checks whether #status matches any of the given statuses.
-
#validate! ⇒ void
Raises an Error if a daemon is already running.
Constructor Details
#initialize(pidfile:) ⇒ Daemon
Returns a new instance of Daemon.
15 16 17 |
# File 'lib/exekutor/internal/cli/daemon.rb', line 15 def initialize(pidfile:) @pidfile = pidfile end |
Instance Attribute Details
#pidfile ⇒ String (readonly)
The path of the generated pidfile.
12 13 14 |
# File 'lib/exekutor/internal/cli/daemon.rb', line 12 def pidfile @pidfile end |
Instance Method Details
#daemonize ⇒ void
This method returns an undefined value.
Daemonizes the current process and writes out a pidfile.
21 22 23 24 25 |
# File 'lib/exekutor/internal/cli/daemon.rb', line 21 def daemonize validate! ::Process.daemon true write_pid end |
#pid ⇒ Integer?
The process ID for this daemon, if known
30 31 32 33 34 35 36 37 |
# File 'lib/exekutor/internal/cli/daemon.rb', line 30 def pid return nil unless ::File.exist? pidfile pid = ::File.read(pidfile) raise Error, "Corrupt PID-file. Check #{pidfile}" unless pid.to_i.positive? pid.to_i end |
#status ⇒ :running, ...
The process status for this daemon. Possible states are:
-
:running
when the daemon is running; -
:not_running
when the daemon is not running; -
:dead
when the daemon is dead. (Ie. the PID is known, but the process is gone); -
:not_owned
when the daemon cannot be accessed.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/exekutor/internal/cli/daemon.rb', line 45 def status pid = self.pid return :not_running if pid.nil? # If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for # the existence of a process ID or process group ID. ::Process.kill(0, pid) :running rescue Errno::ESRCH :dead rescue Errno::EPERM :not_owned end |
#status?(*statuses) ⇒ Boolean
Checks whether #status matches any of the given statuses.
63 64 65 |
# File 'lib/exekutor/internal/cli/daemon.rb', line 63 def status?(*statuses) statuses.include? status end |
#validate! ⇒ void
This method returns an undefined value.
Raises an Error if a daemon is already running. Deletes the pidfile is the process is dead.
70 71 72 73 74 75 76 77 78 |
# File 'lib/exekutor/internal/cli/daemon.rb', line 70 def validate! case status when :running, :not_owned raise Error, "A worker is already running. Check #{pidfile}" else delete_pid end nil end |