Class: Freyr::PidFile
- Inherits:
-
Object
- Object
- Freyr::PidFile
- Defined in:
- lib/freyr/pid_file.rb
Class Method Summary collapse
Instance Method Summary collapse
- #alive? ⇒ Boolean
-
#initialize(path, procname = nil) ⇒ PidFile
constructor
A new instance of PidFile.
- #pid(force = false) ⇒ Object
- #pid_from_file ⇒ Object
- #pid_from_procname(force = false) ⇒ Object
- #process_info ⇒ Object
- #wait_for_pid(wait = 40, interval = 0.2) ⇒ Object
Constructor Details
#initialize(path, procname = nil) ⇒ PidFile
Returns a new instance of PidFile.
3 4 5 6 |
# File 'lib/freyr/pid_file.rb', line 3 def initialize path, procname=nil @path = path @procname = procname end |
Class Method Details
.pid_command_hash(force = false) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/freyr/pid_file.rb', line 61 def pid_command_hash force=false @pid_command_hash = nil if force @pid_command_hash ||= `ps -eo pid,command`.split("\n").inject({}) do |r, pid| if m = pid.match(/^\s*(\d+)\s(.+)$/) r[m[2]] = m[1].to_i end r end end |
Instance Method Details
#alive? ⇒ Boolean
12 13 14 15 16 17 18 |
# File 'lib/freyr/pid_file.rb', line 12 def alive? return unless pid Process.getpgid(pid) true rescue Errno::ESRCH false end |
#pid(force = false) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/freyr/pid_file.rb', line 26 def pid force=false if !force && File.exist?(@path) pid_from_file elsif @procname pid_from_procname else pid_from_file end end |
#pid_from_file ⇒ Object
20 21 22 23 24 |
# File 'lib/freyr/pid_file.rb', line 20 def pid_from_file return unless File.exist?(@path) p = File.open(@path).read.chomp p ? p.to_i : nil end |
#pid_from_procname(force = false) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/freyr/pid_file.rb', line 36 def pid_from_procname force=false pids = PidFile.pid_command_hash(force) if procline = pids.keys.find {|p| p.match(@procname)} pids[procline] end end |
#process_info ⇒ Object
8 9 10 |
# File 'lib/freyr/pid_file.rb', line 8 def process_info @proces_info ||= ProcessInfo.new(@pid) if @pid end |
#wait_for_pid(wait = 40, interval = 0.2) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/freyr/pid_file.rb', line 44 def wait_for_pid wait = 40, interval=0.2 OUT.puts "Waiting #{wait}s for pid from match of #{@procname.inspect}" start = Time.now until (pid = pid_from_procname(true)) || (Time.now-start) > wait OUT.print '.';OUT.flush sleep(interval) end raise Timeout, "\n Couldn't find pid after" unless pid OUT.puts '*' pid end |