Class: Pgai::PortForward

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

Instance Method Summary collapse

Constructor Details

#initialize(config:, hostname:) ⇒ PortForward

Returns a new instance of PortForward.



8
9
10
11
# File 'lib/pgai/port_forward.rb', line 8

def initialize(config:, hostname:)
  @config = config
  @hostname = hostname
end

Instance Method Details

#conditionally_stop(port, ignored_pids = []) ⇒ Object



26
27
28
29
30
31
# File 'lib/pgai/port_forward.rb', line 26

def conditionally_stop(port, ignored_pids = [])
  pids = lsof(port, ignored_pids + [Process.pid])
  pf_pids = port_forward_pids(port)

  stop(port) if (pids - pf_pids).empty?
end

#start(port) ⇒ Object



13
14
15
16
17
18
# File 'lib/pgai/port_forward.rb', line 13

def start(port)
  return if ready?(port)

  system("ssh -fNTML #{port.to_i}:#{escape hostname}:#{port.to_i} #{escape config.proxy}")
  wait_until_ready(port)
end

#stop(port = nil) ⇒ Object



20
21
22
23
24
# File 'lib/pgai/port_forward.rb', line 20

def stop(port = nil)
  port_forward_pids(port).each do |pid|
    Process.kill("HUP", pid.to_i)
  end
end