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.



13
14
15
16
17
# File 'lib/pact/mock_service/cli/pidfile.rb', line 13

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.



11
12
13
# File 'lib/pact/mock_service/cli/pidfile.rb', line 11

def name
  @name
end

#pidObject

Returns the value of attribute pid.



11
12
13
# File 'lib/pact/mock_service/cli/pidfile.rb', line 11

def pid
  @pid
end

#pid_dirObject

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

Returns:

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



35
36
37
# File 'lib/pact/mock_service/cli/pidfile.rb', line 35

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

#deleteObject



67
68
69
# File 'lib/pact/mock_service/cli/pidfile.rb', line 67

def delete
  FileUtils.rm pidfile_path
end

#file_exists?Boolean

Returns:

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

Returns:

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



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_fileObject



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_pathObject



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

Returns:

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

Returns:

  • (Boolean)


23
24
25
# File 'lib/pact/mock_service/cli/pidfile.rb', line 23

def process_running?
  process_exists? pid_from_file
end

#waitpidObject



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

#writeObject



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