gestion du message de vie vers le processus KineIntercom

This commit is contained in:
Vincent BENOIT
2022-11-29 14:48:55 +01:00
parent ec9ecae0c7
commit 2ee5f01062
2 changed files with 44 additions and 1 deletions

View File

@@ -23,7 +23,7 @@ import platform
import psutil
from werkzeug.exceptions import HTTPException
from ConfBack.manager import Sock
from ConfBack.manager import Sock, TimeoutError
#########################################################
# Class et Methods #
@@ -121,12 +121,21 @@ def is_alive():
current_app.logger.error("impossible de se connecter au serveur")
abort(status.HTTP_406_NOT_ACCEPTABLE, description="Impossible de se connecter au serveur")
current_app.logger.info("envoie de la demande de message de vie ...")
# send order to KineIntercom process
if not conn.send(b"ALIVE\n"):
conn.disconnect()
current_app.logger.error("impossible de communiquer avec le serveur")
abort(status.HTTP_406_NOT_ACCEPTABLE, description="Impossible de communiquer avec le serveur")
try:
datas = conn.recv_timeout(6, 10)
if not datas.decode('utf-8').startswith('ALIVE'):
logger.warning("message de vie non valide ...")
ret = False
except TimeoutError as e:
current_app.logger.warning("Pas de message de vie de la part du processus KineIntercom")
ret = False
conn.disconnect()
content = {'alive':ret}

View File

@@ -8,11 +8,18 @@
import sys, re, os
import socket
import select
import time
#########################################################
# Classes et Methodes #
class TimeoutError(Exception):
''' TimeoutError for socket '''
def __init__(self):
super().__init__("socket timeout")
class Sock:
''' Socket class '''
@@ -51,6 +58,12 @@ class Sock:
def send(self, msg):
''' Send message to Server
:param msg:
message (bytes) send to server
return bool:
True if OK, otherwise False
'''
totalsent = 0
ret = True
@@ -63,6 +76,27 @@ class Sock:
totalsent = totalsent + sent
return ret
def recv_timeout(self, bytes_to_read=0, timeout_seconds=0):
''' Receive message from Server
:param bytes_to_read:
bytes to read from socket
:param timeout_seconds:
timeout to raise custom exception
:return bytes:
datas received from socket
'''
self.sock.setblocking(0)
self.log.debug("waiting datas ...")
ready = select.select([self.sock], [], [], timeout_seconds)
if ready[0]:
return self.sock.recv(bytes_to_read)
self.log.error("sock timeout")
raise TimeoutError
#########################################################
# Decorators #