ini e email

This commit is contained in:
2025-08-21 19:03:23 +02:00
parent d3f7e9090a
commit f33ae140fc
10 changed files with 83 additions and 22 deletions

21
env/email.ini vendored
View File

@@ -1,12 +1,19 @@
[smtp]
address = smtp.aseltd.eu
port = 587
user = alert@aseltd.eu
password = Ase#2013!20@bat
[address]
from = ASE Alert System<alert@aseltd.eu>
to = andrea.carri@aseltd.eu,alessandro.battilani@gmail.com,alessandro.valletta@aseltd.eu,alberto.sillani@aseltd.eu,majd.saidani@aseltd.eu
cc =
to1 = andrea.carri@aseltd.eu,alessandro.battilani@gmail.com,alessandro.valletta@aseltd.eu,alberto.sillani@aseltd.eu,majd.saidani@aseltd.eu
to = alessandro.battilani@aseltd.eu
cc = alessandro.battilani@gmail.com
bcc =
[msg]
subject = ASE Alert System
body = <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> \
body = <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
@@ -29,22 +36,22 @@
</tr>
<tr>
<td align="center">
<h3 style="margin: 5px;">Matlab function $matlab_cmd failed on unit => $unit - tool => $tool</h3>
<h3 style="margin: 5px;">Matlab function {matlab_cmd} failed on unit => {unit} - tool => {tool}</h3>
</td>
</tr>
<tr>
<td align="center">
<h4 style="margin: 5px;">$matlab_error</h4>
<h4 style="margin: 5px;">{matlab_error}</h4>
</td>
</tr>
<tr>
<td style="padding: 20px; padding-bottom: 0px; color: red">
$MatlabErrors
{MatlabErrors}
</td>
</tr>
<tr>
<td style="padding: 20px;">
$MatlabWarnings
{MatlabWarnings}
</td>
</tr>
</table>

View File

@@ -11,6 +11,7 @@ from utils.database.action_query import get_tool_info
from utils.csv.loaders import get_next_csv_atomic
from utils.orchestrator_utils import run_orchestrator, worker_context
from utils.database.loader_action import update_status, unlock
from utils.connect.send_email import send_error_email
# Initialize the logger for this module
logger = logging.getLogger()
@@ -44,7 +45,7 @@ async def worker(worker_id: int, cfg: object, pool: object) -> None:
record = await get_next_csv_atomic(pool, cfg.dbrectable, WorkflowFlags.DATA_LOADED, WorkflowFlags.DATA_ELABORATED)
if record:
id, unit_type, tool_type, unit_name, tool_name = [x.lower().replace(" ", "_") if isinstance(x, str) else x for x in record]
if tool_type.lower() != "gd": # i tool GD non devono essere elaborati
if tool_type.lower() != "gd": # i tool GD non devono essere elaborati ???
tool_elab_info = await get_tool_info(WorkflowFlags.DATA_ELABORATED, unit_name.upper(), tool_name.upper(), pool)
if tool_elab_info:
if tool_elab_info['statustools'].lower() in cfg.elab_status:
@@ -65,6 +66,20 @@ async def worker(worker_id: int, cfg: object, pool: object) -> None:
logger.error(stderr.decode().strip())
with open(f"{cfg.matlab_error_path}{unit_name}{tool_name}_output_error.txt", "w") as f:
f.write(stderr.decode().strip())
if proc.returncode == 124:
error_type = f"Matlab elab excessive duration: killed after {cfg.matlab_timeout} seconds."
else:
error_type = f"Matlab elab failed: {proc.returncode}."
"""
da verificare i log degli errori come sono creati... dal perl:
"cd $MatlabErrorPath; cat _${unit}_${tool}*_\*_output_error.txt > ${unit}${tool}_output_error.txt";
"""
errors = [line for line in stderr.decode().strip() if line.startswith("Error")]
warnings = [line for line in stderr.decode().strip() if line.startswith("Warning")]
await send_error_email(unit_name.upper(), tool_name.upper(), tool_elab_info['matcall'], error_type, errors, warnings) # da verificare
else:
logger.info(stdout.decode().strip())
await update_status(cfg, id, WorkflowFlags.DATA_ELABORATED, pool)

View File

@@ -1,3 +1,3 @@
"""Config ini setting"""
from pathlib import Path
ROOT_PATH = Path(__file__).resolve().parent.parent.parent.parent
ENV_PARENT_PATH = Path(__file__).resolve().parent.parent.parent.parent

View File

@@ -2,18 +2,24 @@
"""
from configparser import ConfigParser
from . import ROOT_PATH
from . import ENV_PARENT_PATH
class Config:
def __init__(self):
c = ConfigParser()
c.read([f"{ROOT_PATH}/env/email.ini"])
print(c.items)
c.read([f"{ENV_PARENT_PATH}/env/email.ini"])
# email setting
self.from_addr = c.get("address", "from")
self.to_addr = c.get("address", "to")
self.cc_addr = c.get("address", "cc")
self.bcc_addr = c.get("address", "bcc")
self.subject = c.get("msg", "subject")
self.body = c.get("msg", "body")
self.smtp_addr = c.get("smtp", "address")
self.smtp_port = c.getint("smtp", "port")
self.smtp_user = c.get("smtp", "user")
self.smtp_passwd = c.get("smtp", "password")

View File

@@ -2,7 +2,7 @@
"""
from configparser import ConfigParser
from . import ROOT_PATH
from . import ENV_PARENT_PATH
class Config:
def __init__(self):
@@ -12,7 +12,7 @@ class Config:
"""
c = ConfigParser()
c.read([f"{ROOT_PATH}/env/ftp.ini", f"{ROOT_PATH}/env/db.ini"])
c.read([f"{ENV_PARENT_PATH}/env/ftp.ini", f"{ENV_PARENT_PATH}/env/db.ini"])
# FTP setting
self.service_port = c.getint("ftpserver", "service_port")

View File

@@ -2,7 +2,7 @@
"""
from configparser import ConfigParser
from . import ROOT_PATH
from . import ENV_PARENT_PATH
class Config:
def __init__(self):
@@ -12,7 +12,7 @@ class Config:
"""
c = ConfigParser()
c.read([f"{ROOT_PATH}/env/load.ini", f"{ROOT_PATH}/env/db.ini"])
c.read([f"{ENV_PARENT_PATH}/env/load.ini", f"{ENV_PARENT_PATH}/env/db.ini"])
# LOG setting
self.logfilename = c.get("logging", "logFilename")

View File

@@ -2,7 +2,7 @@
"""
from configparser import ConfigParser
from . import ROOT_PATH
from . import ENV_PARENT_PATH
class Config:
def __init__(self):
@@ -12,7 +12,7 @@ class Config:
"""
c = ConfigParser()
c.read([f"{ROOT_PATH}/env/elab.ini", f"{ROOT_PATH}/env/db.ini"])
c.read([f"{ENV_PARENT_PATH}/env/elab.ini", f"{ENV_PARENT_PATH}/env/db.ini"])
# LOG setting
self.logfilename = c.get("logging", "logFilename")

View File

@@ -2,7 +2,7 @@
"""
from configparser import ConfigParser
from . import ROOT_PATH
from . import ENV_PARENT_PATH
class Config:
def __init__(self):
@@ -12,7 +12,7 @@ class Config:
"""
c = ConfigParser()
c.read([f"{ROOT_PATH}/env/send.ini", f"{ROOT_PATH}/env/db.ini"])
c.read([f"{ENV_PARENT_PATH}/env/send.ini", f"{ENV_PARENT_PATH}/env/db.ini"])
# LOG setting
self.logfilename = c.get("logging", "logFilename")

View File

@@ -2,7 +2,7 @@
"""
from configparser import ConfigParser
from . import ROOT_PATH
from . import ENV_PARENT_PATH
class Config:
"""
@@ -11,7 +11,7 @@ class Config:
def __init__(self):
c = ConfigParser()
c.read([f"{ROOT_PATH}/env/db.ini"])
c.read([f"{ENV_PARENT_PATH}/env/db.ini"])
# DB setting
self.dbhost = c.get("db", "hostname")

View File

@@ -0,0 +1,33 @@
import smtplib
import logging
from email.message import EmailMessage
from utils.config import loader_email as setting
cfg = setting.Config()
logger = logging.getLogger(__name__)
async def send_error_email(unit_name: str, tool_name: str, matlab_cmd: str, matlab_error: str, errors: list, warnings: list) -> None:
# Creazione dell'oggetto messaggio
msg = EmailMessage()
msg['Subject'] = cfg.subject
msg['From'] = cfg.from_addr
msg['To'] = cfg.to_addr
msg['Cc'] = cfg.cc_addr
msg['Bcc'] = cfg.bcc_addr
MatlabErrors = "<br/>".join(errors)
MatlabWarnings = "<br/>".join(dict.fromkeys(warnings))
# Imposta il contenuto del messaggio come HTML
msg.add_alternative(cfg.body.format(unit=unit_name, tool=tool_name, matlab_cmd=matlab_cmd, matlab_error=matlab_error,
MatlabErrors=MatlabErrors, MatlabWarnings=MatlabWarnings), subtype='html')
try:
# Connessione al server SMTP
with smtplib.SMTP(cfg.smtp_addr, cfg.smtp_port) as server:
server.starttls() # Avvia la crittografia TLS per una connessione sicura
server.login(cfg.smtp_user, cfg.smtp_passwd) # Autenticazione con il server
server.send_message(msg) # Invio dell'email
logger.info("Email inviata con successo!")
except Exception as e:
logger.error(f"Errore durante l'invio dell'email: {e}")