Module: Alt::Foreman

Defined in:
lib/alt-foreman.rb,
lib/alt-foreman/process.rb,
lib/alt-foreman/version.rb

Defined Under Namespace

Classes: Process

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.kill_tcp_server!(need_port, name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/alt-foreman/process.rb', line 35

def kill_tcp_server!(need_port, name)
  tries = 0
  loop do
    found = false

    `netstat -nWlp 2>/dev/null|grep '^tcp '`.each_line do |line|
      _, _, _, addr, _, what, program = line.strip.split(/\s+/)
      ip, port = addr.split(%r/:/)
      pid, process = program.split(%r:/:)

      if (port.to_i == need_port.to_i) && (pid.to_i > 0)
        found = true
        sig = (tries <= 5 ? 'TERM' : 'KILL')
        Alt::Foreman.log(name, "Found conflicting server process - sending #{sig} to #{pid} & sleeping for 2s", :red)
        ::Process.kill(sig, pid.to_i)
        sleep 2
        tries += 1
      end
    end

    return unless found

    if found && tries > 15
      STDERR.puts "Failed to kill existing process - continuing anyway."
      return
    end
  end
end

.log(sender, line, line_colour = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/alt-foreman.rb', line 10

def self.log(sender, line, line_colour = nil)
  @mutex.synchronize do
    colour = @senders[sender]
    unless colour
      colour = @senders[sender] = @colours[@senders.size % @colours.size]
    end
    line = line.chomp
    if line_colour
      line = Term::ANSIColor.send(line_colour, line)
    end
    puts Term::ANSIColor.send(colour, Term::ANSIColor.bold(sprintf '%s %15s | ', Time.now.strftime('%H:%M:%S'), sender) + line)
  end
end