changement d'utilisation de l'IPC sock

This commit is contained in:
2022-11-17 23:38:18 +01:00
parent 193573524a
commit 0f83fb780c
3 changed files with 65 additions and 29 deletions

View File

@@ -23,7 +23,6 @@ from ConfBack.account import account
from ConfBack.params import params
from ConfBack.schedule import schedule
from ConfBack.log import log as logs
from ConfBack.manager import sock
from ConfBack.infos import info
#########################################################
@@ -53,7 +52,6 @@ def create_app(config=None, app_name=None):
configure_log(app)
configure_blueprints(app)
ret = configure_connexion(app)
return ret, app
@@ -139,19 +137,3 @@ def configure_blueprints(app=None):
'''
for bp in [auth, logs, account, params, schedule, info]:
app.register_blueprint(bp)
def configure_connexion(app=None):
''' Confgure AF_UNIX connexion with KineIntercom
process
'''
ret = False
try:
sock.connect(app.config['UNIX_ADDR'])
ret = True
except TimeoutError as e:
app.logger.error("Erreur de connexion avec le processus KineIntercom")
ret = False
except ConnectionRefusedError as e:
app.logger.error("Connexion refusée avec le processus KineIntercom")
ret = False
return ret

View File

@@ -8,14 +8,63 @@
import sys, re, os
import socket
import time
#########################################################
# Corps principal du programme #
# Classes et Methodes #
class Sock:
''' Socket class '''
def __init__(self, addr="", logger=None):
''' Class constructor
'''
self.addr = addr
self.log = logger
self.isConnected = False
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
def connect(self):
''' connect to KineIntercom server
'''
ret = False
idx = 0
while not ret and idx < 3:
try:
self.sock.connect(self.addr)
ret = True
except TimeoutError as e:
self.log.error("Erreur de connexion avec le processus KineIntercom")
except ConnectionRefusedError as e:
self.log.error("Connexion refusée avec le processus KineIntercom")
except FileNotFoundError as e:
self.log.error("Connexion inexistante")
idx = idx + 1
time.sleep(1)
self.isConnected = ret
return ret
def disconnect(self):
''' Disconnect from KineIntercom server
'''
self.sock.close()
def send(self, msg):
''' Send message to Server
'''
totalsent = 0
ret = True
while totalsent < len(msg):
sent = self.sock.send(msg[totalsent:])
if sent == 0:
self.log.error("socket connection broken")
ret = False
break
totalsent = totalsent + sent
return ret
#########################################################
# Decorators #
#########################################################
# Instantiation #
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

View File

@@ -24,7 +24,7 @@ import shutil
import hashlib
from werkzeug.exceptions import HTTPException
from ConfBack.manager import sock
from ConfBack.manager import Sock
#########################################################
# Class et Methods #
@@ -136,14 +136,19 @@ def update_params():
with open(current_app.config['DB_PATH'], 'w') as f:
json.dump(data, f)
try:
# send order to KineIntercom process
sock.send(b"RELOAD_DB\n")
except:
current_app.logger.error("Erreur d'envoi de l'ordre au processus KineIntercom")
abort(status.HTTP_406_NOT_ACCEPTABLE, description="Erreur d'envoi de l'ordre au processus")
conn = Sock(addr=current_app.config['UNIX_ADDR'], logger=current_app.logger)
if not conn.connect():
current_app.logger.error("impossible de se connecter au serveur")
abort(status.HTTP_406_NOT_ACCEPTABLE, description="Impossible de se connecter au serveur")
# send order to KineIntercom process
if not conn.send(b"RELOAD_DB\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")
conn.disconnect()
content = {'message':'maj parameters successful!'}
return content, status.HTTP_200_OK