Class: Pact::MockService::CLI::Pidfile

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/mock_service/cli/pidfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 options
  @pid_dir = options[:pid_dir] || 'tmp/pids'
  @name = options[:name] || default_name
  @pid = options[:pid] || Process.pid
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/pact/mock_service/cli/pidfile.rb', line 9

def name
  @name
end

#pidObject

Returns the value of attribute pid.



9
10
11
# File 'lib/pact/mock_service/cli/pidfile.rb', line 9

def pid
  @pid
end

#pid_dirObject

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

Returns:

  • (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_nameObject



33
34
35
# File 'lib/pact/mock_service/cli/pidfile.rb', line 33

def default_name
  File.basename($0, File.extname($0)) + ".pid"
end

#deleteObject



65
66
67
# File 'lib/pact/mock_service/cli/pidfile.rb', line 65

def delete
  FileUtils.rm pidfile_path
end

#file_exists?Boolean

Returns:

  • (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

Returns:

  • (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_processObject



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_fileObject



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_pathObject



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

Returns:

  • (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

Returns:

  • (Boolean)


21
22
23
# File 'lib/pact/mock_service/cli/pidfile.rb', line 21

def process_running?
  process_exists? pid_from_file
end

#waitpidObject



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

#writeObject



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