From 6cf8749e9afb829045897cee41399fa6af64f072 Mon Sep 17 00:00:00 2001 From: zaihuaji Date: Mon, 25 Aug 2025 11:41:10 -0500 Subject: [PATCH] use /usr/sbin/sendmail on capsers --- pyproject.toml | 2 +- src/rda_python_common/PgDBI.py | 15 +++++++++------ src/rda_python_common/PgLOG.py | 4 +++- src/rda_python_common/PgOPT.py | 32 +++++++++++++++++++++----------- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 41f9ca1..b58a794 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "rda_python_common" -version = "1.0.27" +version = "1.0.28" authors = [ { name="Zaihua Ji", email="zji@ucar.edu" }, ] diff --git a/src/rda_python_common/PgDBI.py b/src/rda_python_common/PgDBI.py index ae4f416..c28817a 100644 --- a/src/rda_python_common/PgDBI.py +++ b/src/rda_python_common/PgDBI.py @@ -1845,9 +1845,9 @@ def get_specialist(dsid, logact = PGDBI['ERRLOG']): # def build_customized_email(table, field, condition, subject, logact = 0): + estat = PgLOG.FAILURE msg = PgLOG.get_email() - - if not msg: return PgLOG.FAILURE + if not msg: return estat sender = PgLOG.PGLOG['CURUID'] + "@ucar.edu" receiver = PgLOG.PGLOG['EMLADDR'] if PgLOG.PGLOG['EMLADDR'] else (PgLOG.PGLOG['CURUID'] + "@ucar.edu") @@ -1857,9 +1857,12 @@ def build_customized_email(table, field, condition, subject, logact = 0): if not subject: subject = "Message from {}-{}".format(PgLOG.PGLOG['HOSTNAME'], PgLOG.get_command()) ebuf += "Subject: {}!\n\n{}\n".format(subject, msg) - estat = cache_customized_email(table, field, condition, ebuf, logact) - if estat and logact: - PgLOG.pglog("Email {} cached to '{}.{}' for {}, Subject: {}".format(receiver, table, field, condition, subject), logact) + if PgLOG.PGLOG['EMLSEND']: + estat = PgLOG.send_customized_email(f"{table}.{condition}", ebuf, logact) + if estat != PgLOG.SUCCESS: + estat = cache_customized_email(table, field, condition, ebuf, 0) + if estat and logact: + PgLOG.pglog("Email {} cached to '{}.{}' for {}, Subject: {}".format(receiver, table, field, condition, subject), logact) return estat @@ -1924,7 +1927,7 @@ def cache_customized_email(table, field, condition, emlmsg, logact = 0): return PgLOG.SUCCESS else: msg = "cache email to '{}.{}' for {}".format(table, field, condition) - PgLOG.pglog("Error msg, try to send directly now", logact|PgLOG.ERRLOG) + PgLOG.pglog(f"Error {msg}, try to send directly now", logact|PgLOG.ERRLOG) return PgLOG.send_customized_email(msg, emlmsg, logact) # diff --git a/src/rda_python_common/PgLOG.py b/src/rda_python_common/PgLOG.py index 0c4afa0..dc86b00 100644 --- a/src/rda_python_common/PgLOG.py +++ b/src/rda_python_common/PgLOG.py @@ -119,6 +119,7 @@ 'PBSTIME' : 86400, # max runtime for SLURM bath job, (7x24x60x60 seconds) 'MSSGRP' : None, # set if set to different HPSS group 'RDAGRP' : "decs", + 'EMLSEND' : None, # path to sendmail, None if not exists 'DSCHECK' : None, # carry some cached dscheck information 'PGDBBUF' : None, # reference to a connected database object 'HPSSLMT' : 10, # up limit of HPSS streams @@ -1289,7 +1290,8 @@ def set_common_pglog(): SETPGLOG("LOGFILE", "pgdss.log") # log file name SETPGLOG("EMLFILE", "pgemail.log") # email log file name SETPGLOG("ERRFILE", '') # error file name - SETPGLOG("EMLSEND", "/usr/lib/sendmail -t") # send email command + sm = "/usr/sbin/sendmail" + if valid_command(sm): SETPGLOG("EMLSEND", f"{sm} -t") # send email command SETPGLOG("DBGLEVEL", '') # debug level SETPGLOG("DBGPATH", PGLOG['DSSDBHM']+"/log") # path to debug log file SETPGLOG("OBJCTBKT", "rda-data") # default Bucket on Object Store diff --git a/src/rda_python_common/PgOPT.py b/src/rda_python_common/PgOPT.py index a688fff..6d3801f 100644 --- a/src/rda_python_common/PgOPT.py +++ b/src/rda_python_common/PgOPT.py @@ -1684,6 +1684,7 @@ def send_request_email_notice(pgrqst, errmsg, fcount, rstat, readyfile = None, p for ekey in einfo: ebuf = re.sub(r'<{}>'.format(ekey), einfo[ekey], ebuf) + if PgLOG.PGLOG['DSCHECK'] and not pgpart: tbl = "dscheck" cnd = "cindex = {}".format(PgLOG.PGLOG['DSCHECK']['cindex']) @@ -1691,18 +1692,27 @@ def send_request_email_notice(pgrqst, errmsg, fcount, rstat, readyfile = None, p tbl = "dsrqst" cnd = "rindex = {}".format(pgrqst['rindex']) - if not PgDBI.cache_customized_email(tbl, "einfo", cnd, ebuf, 0): return 'E' - if errmsg: - PgLOG.pglog("Error Email {} cached to {}.einfo for {}:\n{}".format(einfo['SENDER'], tbl, cnd, errmsg), - PGOPT['errlog']) + if PgLOG.PGLOG['EMLSEND'] and PgLOG.send_customized_email(f"{tbl}.{cnd}", ebuf, 0): + if errmsg: + PgLOG.pglog("Error Email sent to {} for {}.{}:\n{}".format(einfo['SENDER'], tbl, cnd, errmsg), PGOPT['errlog']) + readyfile = None + else: + PgLOG.pglog("{}Email sent to {} for {}.{}\nSubset: {}".format(("Customized " if pgrqst['enotice'] else ""), einfo['RECEIVER'], tbl, cnd, einfo['SUBJECT']), + PGOPT['wrnlog']|PgLOG.FRCLOG) else: - PgLOG.pglog("{}Email {} cached to {}.einfo for {}\nSubset: {}".format(("Customized " if pgrqst['enotice'] else ""), einfo['RECEIVER'], tbl, cnd, einfo['SUBJECT']), - PGOPT['wrnlog']|PgLOG.FRCLOG) - if readyfile: - rf = open(readyfile, 'w') - rf.write(ebuf) - rf.close() - PgFile.set_local_mode(readyfile, 1, PgLOG.PGLOG['FILEMODE']) + if not PgDBI.cache_customized_email(tbl, "einfo", cnd, ebuf, 0): return 'E' + if errmsg: + PgLOG.pglog("Error Email {} cached to {}.einfo for {}:\n{}".format(einfo['SENDER'], tbl, cnd, errmsg), PGOPT['errlog']) + readyfile = None + else: + PgLOG.pglog("{}Email {} cached to {}.einfo for {}\nSubset: {}".format(("Customized " if pgrqst['enotice'] else ""), einfo['RECEIVER'], tbl, cnd, einfo['SUBJECT']), + PGOPT['wrnlog']|PgLOG.FRCLOG) + + if readyfile: + rf = open(readyfile, 'w') + rf.write(ebuf) + rf.close() + PgFile.set_local_mode(readyfile, 1, PgLOG.PGLOG['FILEMODE']) return rstat