Added exceptioning handling
This commit is contained in:
parent
c11943cc67
commit
b599a4a483
1 changed files with 52 additions and 30 deletions
|
@ -33,56 +33,74 @@ def main():
|
|||
if os.path.exists(socketFile):
|
||||
os.remove(socketFile)
|
||||
|
||||
stop_event = threading.Event()
|
||||
stopEvent = threading.Event()
|
||||
failureEvent = threading.Event()
|
||||
|
||||
dataBuffer = []
|
||||
|
||||
def publishData(stop_event):
|
||||
def publishData(stopEvent):
|
||||
|
||||
if config.loggingMode == 'rabbitmq':
|
||||
try:
|
||||
|
||||
import rabbitmq
|
||||
rabbitmq = rabbitmq.rabbitMQClient(config.rabbitmqca,config.rabbitmqcacert,config.rabbitmqcakey,config.rabbitmqHost,config.rabbitmqPort,config.rabbitmqRoutingKey)
|
||||
if config.loggingMode == 'rabbitmq':
|
||||
|
||||
while not (stop_event.is_set()):
|
||||
if dataBuffer:
|
||||
data = dataBuffer.pop(0)
|
||||
import rabbitmq
|
||||
rabbitmq = rabbitmq.rabbitMQClient(config.rabbitmqca,config.rabbitmqcacert,config.rabbitmqcakey,config.rabbitmqHost,config.rabbitmqPort,config.rabbitmqRoutingKey)
|
||||
|
||||
if config.loggingMode == 'rabbitmq':
|
||||
rabbitmq.publish(f"{data}")
|
||||
else:
|
||||
print("Not yet implemented")
|
||||
while not (stopEvent.is_set() and not failureEvent.is_set()):
|
||||
|
||||
if dataBuffer:
|
||||
|
||||
data = dataBuffer.pop(0)
|
||||
|
||||
if config.loggingMode == 'rabbitmq':
|
||||
|
||||
rabbitmq.publish(f"{data}")
|
||||
|
||||
else:
|
||||
|
||||
print("Not yet implemented")
|
||||
|
||||
except Exception:
|
||||
failureEvent.set()
|
||||
|
||||
def cleanup(signum, frame):
|
||||
|
||||
print("Signal received, shutting down...")
|
||||
stop_event.set()
|
||||
stopEvent.set()
|
||||
|
||||
signal.signal(signal.SIGTERM, cleanup)
|
||||
signal.signal(signal.SIGINT, cleanup)
|
||||
|
||||
def server(stop_event):
|
||||
def server(stopEvent):
|
||||
|
||||
serverSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
serverSocket.bind(socketFile)
|
||||
serverSocket.listen(50)
|
||||
try:
|
||||
|
||||
while not (stop_event.is_set()):
|
||||
serverSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
serverSocket.bind(socketFile)
|
||||
serverSocket.listen(50)
|
||||
|
||||
readable, _, _ = select.select([serverSocket], [], [], 1.0)
|
||||
while not (stopEvent.is_set() and not failureEvent.is_set()):
|
||||
|
||||
if readable:
|
||||
clientSocket, _ = serverSocket.accept()
|
||||
data = clientSocket.recv(1024).decode('utf-8')
|
||||
readable, _, _ = select.select([serverSocket], [], [], 1.0)
|
||||
|
||||
if data:
|
||||
if readable:
|
||||
clientSocket, _ = serverSocket.accept()
|
||||
data = clientSocket.recv(1024).decode('utf-8')
|
||||
|
||||
dataBuffer.append(data)
|
||||
print(data)
|
||||
if data:
|
||||
|
||||
clientSocket.close()
|
||||
dataBuffer.append(data)
|
||||
print(data)
|
||||
|
||||
publishThread = threading.Thread(target=publishData, args=(stop_event,))
|
||||
serverThread = threading.Thread(target=server, args=(stop_event,))
|
||||
clientSocket.close()
|
||||
|
||||
except Exception:
|
||||
|
||||
failureEvent.set()
|
||||
|
||||
publishThread = threading.Thread(target=publishData, args=(stopEvent,))
|
||||
serverThread = threading.Thread(target=server, args=(stopEvent,))
|
||||
|
||||
publishThread.start()
|
||||
serverThread.start()
|
||||
|
@ -90,5 +108,9 @@ def main():
|
|||
publishThread.join()
|
||||
serverThread.join()
|
||||
|
||||
if failure_event.is_set():
|
||||
print("One of the threads failed. Terminating")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Loading…
Reference in a new issue