Class: Daemonic::Pidfile
- Inherits:
-
Object
- Object
- Daemonic::Pidfile
- Includes:
- Logging, FileUtils
- Defined in:
- lib/daemonic/pidfile.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Instance Method Summary collapse
- #clean ⇒ Object
-
#initialize(options = {}) ⇒ Pidfile
constructor
A new instance of Pidfile.
- #write ⇒ Object
Methods included from Logging
Constructor Details
#initialize(options = {}) ⇒ Pidfile
Returns a new instance of Pidfile.
10 11 12 13 14 |
# File 'lib/daemonic/pidfile.rb', line 10 def initialize( = {}) @pid = .fetch(:pid) @config = .fetch(:config) @index = [:index] end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/daemonic/pidfile.rb', line 8 def config @config end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
8 9 10 |
# File 'lib/daemonic/pidfile.rb', line 8 def index @index end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
8 9 10 |
# File 'lib/daemonic/pidfile.rb', line 8 def pid @pid end |
Instance Method Details
#clean ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/daemonic/pidfile.rb', line 35 def clean if File.exist?(filename) existing_pid = File.open(filename, 'r').read.chomp if existing_pid == pid.to_s debug "Cleaning up my own pid file at `#{filename}`" rm(filename) else debug "Pid file `#{filename}` did not belong to me, so not cleaning. I am #{Process.pid}, and the pid file says #{existing_pid}" end else debug "Pid file disappeared from `#{filename}`" end end |
#write ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/daemonic/pidfile.rb', line 16 def write mkdir_p(File.dirname(filename)) if File.exist?(filename) existing_pid = File.open(filename, 'r').read.chomp running = Process.getpgid(existing_pid) rescue false if running fatal "Error: PID file already exists at `#{filename}` and a process with PID `#{existing_pid}` is running" exit 1 else debug "Warning: cleaning up stale pid file at `#{filename}`" write! end else debug "Writing pidfile #{filename} with pid #{pid}" write! end end |