This commit is contained in:
2025-05-01 00:58:07 +02:00
parent cc7a136cf3
commit fd5429ee0d
23 changed files with 190 additions and 57 deletions

View File

@@ -1,79 +1,71 @@
#!/usr/bin/env python3
#!.venv/bin/python
import mysql.connector
import os
import sys
import logging
import importlib
import time
import threading
from utils.time import timestamp_fmt as ts
from utils.config import loader as setting
#from unit_tool_mod import g801_mums, g801_mux
from utils.database.connection import connetti_db
def conn_db(cfg):
"""Establishes a connection to the MySQL database.
logger = logging.getLogger(__name__)
Args:
cfg: The configuration object containing database connection details.
Returns:
A MySQL database connection object.
"""
def elab_csv(cfg, threads):
try:
conn = mysql.connector.connect(user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport)
conn.autocommit = True
return conn
except mysql.connector.Error as e:
print(f"Error: {e}")
logging.error(f'{e}')
return None
def elab_csv(cfg):
try:
with conn_db(cfg) as conn:
with connetti_db(cfg) as conn:
cur = conn.cursor()
cur.execute(f'select id, unit_name, unit_type, tool_name, tool_type, tool_data from {cfg.dbname}.{cfg.dbrectable} where locked = 0 and status = 0 limit 1')
id, unit_name, unit_type, tool_name, tool_type, tool_data = cur.fetchone()
cur.execute(f'update {cfg.dbname}.{cfg.dbrectable} set locked = 1 where id = {id}')
if id:
cur.execute(f'update {cfg.dbname}.{cfg.dbrectable} set locked = 1 where id = {id}')
module_name = f'utils.parsers.{unit_type.lower()}_{tool_type.lower()}'
modulo = importlib.import_module(module_name)
funzione = getattr(modulo, "main_loader")
# Chiamare la funzione
thread = threading.Thread(target = funzione, args=(cfg, id))
threads.append(thread)
thread.start()
return True
else:
time.sleep(20)
return False
except mysql.connector.Error as e:
print(f"Error: {e}")
logging.error(f'{e}')
module_name = f'unit_tool_mod.{unit_type.lower()}_{tool_type.lower()}'
modulo = importlib.import_module(module_name)
funzione = getattr(modulo, "chi_sono")
# Chiamare la funzione
risultato = funzione(unit_name, tool_name)
print(f'risultato: {risultato}')
logger.error(f'{e}')
def main():
# Load the configuration settings
cfg = setting.config()
cfg = setting.Config()
try:
# Configura la connessione al database PostgreSQL
# Configure logging
logging.basicConfig(
format="%(asctime)s %(message)s",
filename=cfg.elablog,
format="%(asctime)s - PID: %(process)d.%(name)s.%(levelname)s: %(message)s ",
filename=cfg.logfilename,
level=logging.INFO,
)
elab_csv(cfg)
threads = []
while True:
while len(threads) > cfg.max_threads:
for thread in threads:
if not thread.is_alive():
threads.remove(thread)
if elab_csv(cfg, threads):
logger.info(f"Threads in execution: {len(threads)}")
pass
except KeyboardInterrupt:
logging.info(
"Info: {}.".format("Shutdown requested...exiting")
)
logger.info("Info: Shutdown requested...exiting")
except Exception:
print(
"{} - PID {:>5} >> Error: {}.".format(
ts.timestamp("log"), os.getpid(), sys.exc_info()[1]
)
)
except Exception as e:
logger.info(f"Error: {e}.")
if __name__ == "__main__":
main()