Class: Harbour::Port

Inherits:
Object
  • Object
show all
Defined in:
lib/harbour.rb

Defined Under Namespace

Classes: PortFailedToClose

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Port

Returns a new instance of Port.



9
10
11
# File 'lib/harbour.rb', line 9

def initialize(port)
  @port = port
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/harbour.rb', line 5

def port
  @port
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/harbour.rb', line 17

def closed?
  !open?
end

#kill!(options = {}) ⇒ Object



35
36
37
38
39
# File 'lib/harbour.rb', line 35

def kill!(options = {})
  return nil unless open?
  Process.kill('KILL', pid)
  ensure_closed!(options[:timeout] || 10)
end

#open?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/harbour.rb', line 13

def open?
  command "nc -z localhost #{port}"
end

#pidObject



21
22
23
24
# File 'lib/harbour.rb', line 21

def pid
  return nil unless open?
  `lsof -P | grep ':#{port}' | grep 'LISTEN' | awk '{print $2}'`.to_i
end

#term!(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/harbour.rb', line 26

def term!(options = {})
  return nil unless open?
  Process.kill('TERM', pid)
  ensure_closed!(options[:timeout] || 10)
rescue PortFailedToClose
  raise unless options[:try_kill] == true
  kill!(options)
end