Class: Baykit::BayServer::Agent::Signal::SignalSender

Inherits:
Object
  • Object
show all
Includes:
Bcf, Docker::BuiltIn, Util
Defined in:
lib/baykit/bayserver/agent/signal/signal_sender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Docker::BuiltIn

#find

Constructor Details

#initializeSignalSender

Returns a new instance of SignalSender.



19
20
21
22
# File 'lib/baykit/bayserver/agent/signal/signal_sender.rb', line 19

def initialize
  @bay_port = BuiltInHarborDocker::DEFAULT_CONTROL_PORT
  @pid_file = BuiltInHarborDocker::DEFAULT_PID_FILE
end

Instance Attribute Details

#control_portObject (readonly)

Returns the value of attribute control_port.



16
17
18
# File 'lib/baykit/bayserver/agent/signal/signal_sender.rb', line 16

def control_port
  @control_port
end

#pid_fileObject (readonly)

Returns the value of attribute pid_file.



17
18
19
# File 'lib/baykit/bayserver/agent/signal/signal_sender.rb', line 17

def pid_file
  @pid_file
end

Instance Method Details

#kill(pid, sig) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/baykit/bayserver/agent/signal/signal_sender.rb', line 81

def kill(pid, sig)
  BayLog.info("Send signal pid=#{pid} sig=#{sig}")
  if SysUtil.run_on_windows()
    system("taskkill /PID #{pid} /F")
  else
    Process.kill(sig, pid)
  end
end

#parse_bay_port(plan) ⇒ Object

Parse plan file and get port number of SignalAgent



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/baykit/bayserver/agent/signal/signal_sender.rb', line 48

def parse_bay_port(plan)
  p = BcfParser.new()
  doc = p.parse(plan)
  doc.content_list.each do |elm|
    if elm.kind_of?(BcfElement)
      if elm.name.casecmp?("harbor")
        elm.content_list.each do |kv|
          if kv.key.casecmp?("controlPort")
            @bay_port = kv.value.to_i()
          elsif kv.key.casecmp?("pidFile")
            @pid_file = kv.value
          end
        end
      end
    end
  end
end

#read_pid_fileObject



90
91
92
93
94
# File 'lib/baykit/bayserver/agent/signal/signal_sender.rb', line 90

def read_pid_file()
  File.open(BayServer.get_location(@pid_file), "r") do |f|
    return f.readline().to_i()
  end
end

#send(host, port, cmd) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/baykit/bayserver/agent/signal/signal_sender.rb', line 67

def send(host, port, cmd)
  begin
    a = Addrinfo.tcp(host, port)
    s = Socket.new(a.ipv4? ? Socket::AF_INET : Socket::AF_INET6, Socket::SOCK_STREAM)
    s.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, [60, 0].pack("l_2"))
    s.connect(a)
    s.write(cmd + "\n")
    s.flush();
    line = s.readline()
  ensure
    s.close()
  end
end

#send_command(cmd) ⇒ Object

Send running BayServer a command



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/baykit/bayserver/agent/signal/signal_sender.rb', line 28

def send_command(cmd)
  parse_bay_port(BayServer.bserv_plan)

  if @bay_port < 0
    pid = read_pid_file()
    sig = SignalAgent.get_signal_from_command(cmd)
    if sig == nil
      raise StandardError("Invalid command: " + cmd)
    else
      kill(pid, sig)
    end
  else
    BayLog.info(BayMessage.get(:MSG_SENDING_COMMAND, cmd))
    send("127.0.0.1", @bay_port, cmd)
  end
end