fix: multiple connection (worker)

This commit is contained in:
snsd0805 2024-05-30 17:52:19 +08:00
parent 0f7409a9c6
commit b378aa73ec

View File

@ -39,6 +39,8 @@ class ClusterCommunicationModule():
cont = self.handle_command(data) cont = self.handle_command(data)
if not cont: if not cont:
self.client_sock.close()
self.client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
break break
elif data == '[CHECK]': # master side elif data == '[CHECK]': # master side
# build docker swarm # build docker swarm
@ -90,13 +92,15 @@ class ClusterCommunicationModule():
self.client_sock.send(json.dumps(data).encode()) self.client_sock.send(json.dumps(data).encode())
elif command == '[STOP]': elif command == '[STOP]':
print("Receive STOP signal") print("Receive STOP signal")
self.node_manager.docker_client.swarm.leave()
data = {'host': self.host} data = {'host': self.host}
self.client_sock.send(f'[STOP_CHECK] {json.dumps(data)}'.encode()) self.client_sock.send(f'[STOP_CHECK] {json.dumps(data)}'.encode())
self.node_manager.docker_client.swarm.leave()
self.node_manager.status = 'none'
self.node_manager.exit() print("You have leaved the cluster.")
return False
return True
def request(self, host): # master side def request(self, host): # master side
self.client_sock.connect((host, self.port)) self.client_sock.connect((host, self.port))
@ -167,6 +171,7 @@ class ServiceExplorationModule():
while True: while True:
data, addr = self.sock.recvfrom(1024) data, addr = self.sock.recvfrom(1024)
print(self.node_manager.status)
if self.node_manager.status == 'none': if self.node_manager.status == 'none':
if data.decode() == '[EXPLORE]': if data.decode() == '[EXPLORE]':
self.sock.sendto(self.host.encode(), addr) self.sock.sendto(self.host.encode(), addr)