Aug-19-2021, 07:23 AM
(This post was last modified: Aug-19-2021, 07:23 AM by wardancer84.)
good points, heres the full code with all your proposals implemented.
first run
#!/usr/bin/env python3 import threading import socketserver import os import sys import errno import pymysql as db import logging # # logger & PID setup # pid_file = "/tmp/registry_server_dev.pid" log_file = "/tmp/aix_registry_server_dev.log" logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.propagate = False formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') fh = logging.FileHandler(log_file, "w") fh.setLevel(logging.DEBUG) fh.setFormatter(formatter) logger.addHandler(fh) keep_fds = [fh.stream.fileno()] # # various config itmes # db_config = { 'user': 'aix_reg_dev', 'password': 'aix_reg_dev', 'db': 'aix_registry_dev', 'host': 'localhost', 'port': 3306 } UDPServerAddress = ("nimvie_a", 6790) class AIX_REGISTRY_UDPRequestHandler(socketserver.DatagramRequestHandler): def handle(self): self.r = self.open_db_connection(**db_config) self.c = self.r[0] self.cur = self.r[1] self.data = self.rfile.readline().strip() self.data = self.data.decode('utf-8') self.cur_thread = threading.current_thread() logger.debug("Thread: {} - Message:{}".format(self.cur_thread.name, self.data)) version, node_name, attrib_name, value = self.data.split("|") self.nodes = self.reload_nodes() self.attribs = self.reload_attribs() sql = "INSERT INTO entries VALUES (DEFAULT, %d, %d, '%s', DEFAULT)" % ( self.get_or_create_node_id(node_name), self.get_or_create_attrib_id(attrib_name), value, ) try: self.cur.execute(sql) self.c.commit() except db.MySQLError as e: logger.error("Something went wrong during db interaction: {}".format(e)) finally: if self.cur is not None: self.cur.close() if self.c is not None: self.c.close() def open_db_connection(self,**arg): """Connect to MySQL Database""" try: c = db.connect(**arg) cur = c.cursor() #logger.info("Connection to db opened successfully.") return [c, cur] except db.MySQLError as e: logger.error("Error while trying to connect to db {}".format(e)) def reload_nodes(self): sql = "SELECT name,id from nodes" row = self.cur.execute(sql) # cur should also be an instance variable instead of an argument self.nodes = dict(self.cur.fetchall()) return self.nodes def reload_attribs(self): sql = "SELECT name,id from attribs" row = self.cur.execute(sql) self.attribs = dict(self.cur.fetchall()) return self.attribs def get_or_create_node_id(self,node_name): if node_name not in self.nodes: print("creating new node entry {}".format(node_name)) sql = "INSERT INTO nodes VALUES (DEFAULT, '%s')" % (node_name) self.cur.execute(sql) # Should be an instance variable self.c.commit() # Should be an instance variable self.reload_nodes() return int(self.nodes[node_name]) def get_or_create_attrib_id(self,attrib_name): if attrib_name not in self.attribs: print("creating new attribs entry {}".format(attrib_name)) sql = "INSERT INTO attribs VALUES (DEFAULT, '%s')" % (attrib_name) self.cur.execute(sql) # Should be an instance variable self.c.commit() # Should be an instance variable self.reload_attribs() return int(self.attribs[attrib_name]) def main(): logger.info("Started aix_registry_server_dev with PID {}".format(os.getpid())) logger.info("PID file is: {} ".format(pid_file)) logger.info("LOG file is: {} ".format(log_file)) try: AIX_REGISTRY_UDPServer = socketserver.ThreadingUDPServer(UDPServerAddress, AIX_REGISTRY_UDPRequestHandler) AIX_REGISTRY_UDP_Thread = threading.Thread(target=AIX_REGISTRY_UDPServer.serve_forever) #AIX_REGISTRY_UDP_Thread.daemon = True AIX_REGISTRY_UDP_Thread.start() logger.info("UDP Listener on:{}':'{}".format(UDPServerAddress[0], UDPServerAddress[1])) logger.info("Server loop running in thread:{}".format(AIX_REGISTRY_UDP_Thread.name)) except Exception as e: logger.error("Error while starting up: {}".format(e)) logger.error("EXIT") sys.exit(1) if __name__ == '__main__': main() #daemon = Daemonize(app="aix_registry_server", pid=pid_file, action=main, foreground=False, keep_fds=keep_fds, auto_close_fds=False) #daemon.start()alas the multiplied entries remains...seems some kind of logic error in context to threading. with every run it stacks up even more. strange. i even do a manual delete in the db between the runs.
first run
MariaDB [aix_registry_dev]> select * from nodes where name like '%AIXTESTLPAR%'; +-----+---------------+ | id | name | +-----+---------------+ | 824 | AIXTESTLPAR01 | | 825 | AIXTESTLPAR01 | | 826 | AIXTESTLPAR01 | | 827 | AIXTESTLPAR01 | +-----+---------------+ 4 rows in set (0.001 sec)second run
MariaDB [aix_registry_dev]> select * from nodes where name like '%AIXTESTLPAR%'; +-----+---------------+ | id | name | +-----+---------------+ | 828 | AIXTESTLPAR01 | | 829 | AIXTESTLPAR01 | | 830 | AIXTESTLPAR01 | | 831 | AIXTESTLPAR01 | | 832 | AIXTESTLPAR01 | | 833 | AIXTESTLPAR01 | | 834 | AIXTESTLPAR01 | | 835 | AIXTESTLPAR01 | | 836 | AIXTESTLPAR01 | | 837 | AIXTESTLPAR01 | +-----+---------------+ 10 rows in set (0.002 sec)third run
MariaDB [aix_registry_dev]> select * from nodes where name like '%AIXTESTLPAR%'; +-----+---------------+ | id | name | +-----+---------------+ | 838 | AIXTESTLPAR01 | | 839 | AIXTESTLPAR01 | | 840 | AIXTESTLPAR01 | | 841 | AIXTESTLPAR01 | | 842 | AIXTESTLPAR01 | | 843 | AIXTESTLPAR01 | | 844 | AIXTESTLPAR01 | | 845 | AIXTESTLPAR01 | | 846 | AIXTESTLPAR01 | | 847 | AIXTESTLPAR01 | | 848 | AIXTESTLPAR01 | | 849 | AIXTESTLPAR01 | | 850 | AIXTESTLPAR01 | | 851 | AIXTESTLPAR01 | | 852 | AIXTESTLPAR01 | | 853 | AIXTESTLPAR01 | | 854 | AIXTESTLPAR01 | | 855 | AIXTESTLPAR01 | | 856 | AIXTESTLPAR01 | | 857 | AIXTESTLPAR01 | | 858 | AIXTESTLPAR01 | | 859 | AIXTESTLPAR01 | | 860 | AIXTESTLPAR01 | | 861 | AIXTESTLPAR01 | | 862 | AIXTESTLPAR01 | +-----+---------------+ 25 rows in set (0.001 sec)