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.
13 14 15 16 17 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 13 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.
11 12 13 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 11 def name @name end |
#pid ⇒ Object
Returns the value of attribute pid.
11 12 13 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 11 def pid @pid end |
#pid_dir ⇒ Object
Returns the value of attribute pid_dir.
11 12 13 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 11 def pid_dir @pid_dir end |
Instance Method Details
#can_start? ⇒ Boolean
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 50 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
35 36 37 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 35 def default_name File.basename($0, File.extname($0)) + ".pid" end |
#delete ⇒ Object
67 68 69 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 67 def delete FileUtils.rm pidfile_path end |
#file_exists? ⇒ Boolean
19 20 21 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 19 def file_exists? File.exist?(pidfile_path) end |
#file_exists_and_process_running? ⇒ Boolean
46 47 48 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 46 def file_exists_and_process_running? file_exists? && process_running? end |
#kill_process ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 81 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
31 32 33 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 31 def pid_from_file File.read(pidfile_path).to_i end |
#pidfile_path ⇒ Object
27 28 29 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 27 def pidfile_path File.join(pid_dir, name) end |
#process_exists?(pid) ⇒ Boolean
39 40 41 42 43 44 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 39 def process_exists? pid Process.kill 0, pid true rescue Errno::ESRCH false end |
#process_running? ⇒ Boolean
23 24 25 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 23 def process_running? process_exists? pid_from_file end |
#waitpid ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 71 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
62 63 64 65 |
# File 'lib/pact/mock_service/cli/pidfile.rb', line 62 def write FileUtils.mkdir_p pid_dir File.open(pidfile_path, "w") { |file| file << pid } end |