Class: Pact::MockService::CLI::Pidfile
- Inherits:
-
Object
- Object
- Pact::MockService::CLI::Pidfile
- Defined in:
- lib/pact/mock_service/cli/pidfile.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#pid_dir ⇒ Object
Returns the value of attribute pid_dir.
Instance Method Summary collapse
- #can_start? ⇒ Boolean
- #default_name ⇒ Object
- #delete ⇒ Object
- #file_exists? ⇒ Boolean
- #file_exists_and_process_running? ⇒ Boolean
-
#initialize(options) ⇒ Pidfile
constructor
A new instance of Pidfile.
- #kill_process ⇒ Object
- #pid_from_file ⇒ Object
- #pidfile_path ⇒ Object
- #process_exists?(pid) ⇒ Boolean
- #process_running? ⇒ Boolean
- #waitpid ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(options) ⇒ Pidfile
Returns a new instance of Pidfile.
11 12 13 14 15 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 11 def initialize @pid_dir = [:pid_dir] || 'tmp/pids' @name = [:name] || default_name @pid = [:pid] || Process.pid end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 9 def name @name end |
#pid ⇒ Object
Returns the value of attribute pid.
9 10 11 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 9 def pid @pid end |
#pid_dir ⇒ Object
Returns the value of attribute pid_dir.
9 10 11 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 9 def pid_dir @pid_dir end |
Instance Method Details
#can_start? ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 48 def can_start? if file_exists? && process_running? $stderr.puts "Server already running." false elsif file_exists? $stderr.puts "WARN: PID file #{pidfile_path} already exists, but process is not running. Overwriting pidfile." true else true end end |
#default_name ⇒ Object
33 34 35 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 33 def default_name File.basename($0, File.extname($0)) + ".pid" end |
#delete ⇒ Object
65 66 67 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 65 def delete FileUtils.rm pidfile_path end |
#file_exists? ⇒ Boolean
17 18 19 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 17 def file_exists? File.exist?(pidfile_path) end |
#file_exists_and_process_running? ⇒ Boolean
44 45 46 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 44 def file_exists_and_process_running? file_exists? && process_running? end |
#kill_process ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 79 def kill_process if file_exists? begin `ps -ef | grep pact` Process.kill 2, pid_from_file waitpid delete rescue Errno::ESRCH $stderr.puts "Process in PID file #{pidfile_path} not running. Deleting PID file." delete end else $stderr.puts "No PID file found at #{pidfile_path}, server probably not running. Use `ps -ef | grep pact` if you suspect the process is still running." end end |
#pid_from_file ⇒ Object
29 30 31 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 29 def pid_from_file File.read(pidfile_path).to_i end |
#pidfile_path ⇒ Object
25 26 27 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 25 def pidfile_path File.join(pid_dir, name) end |
#process_exists?(pid) ⇒ Boolean
37 38 39 40 41 42 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 37 def process_exists? pid Process.kill 0, pid true rescue Errno::ESRCH false end |
#process_running? ⇒ Boolean
21 22 23 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 21 def process_running? process_exists? pid_from_file end |
#waitpid ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 69 def waitpid tries = 0 sleep_time = 0.1 while process_running? && tries < 100 sleep sleep_time tries += 1 end raise "Process #{pid_from_file} not stopped after {100 * sleep_time} seconds." if process_running? end |
#write ⇒ Object
60 61 62 63 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 60 def write FileUtils.mkdir_p pid_dir File.open(pidfile_path, "w") { |file| file << pid } end |