Print + better handling of locks

This commit is contained in:
Stranck 2024-05-31 17:00:27 +02:00
parent 1efc0ac030
commit 0529d3fba0
2 changed files with 44 additions and 10 deletions

View File

@ -15,30 +15,44 @@ def killSmptClient():
global sslLock
global sslTimer
global smptSender
logger.info(f"[SMPT] killSmptClient: Lock status: {self.updating.locked()}")
sslTimer.cancel()
sslLock.acquire()
exp = None
if(smptSender is not None):
logger.debug('[SMPT] Closing smpt client')
try:
smptSender.quit() # it calls close() inside
except Exception as e:
exp = e
smptSender = None
sslLock.release()
if(exp != None):
raise exp
async def openSmptClient():
global sslLock
global sslTimer
global sslContext
global smptSender
logger.info(f"[SMPT] openSmptClient: Lock status: {self.updating.locked()}")
sslTimer.cancel()
sslLock.acquire()
exp = None
try:
if(smptSender is None):
logger.debug('[SMPT] Opening smpt client')
client : smtplib.SMTP = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
client.starttls(context=sslContext)
client.login(SMTP_USER, SMTP_PASSWORD)
smptSender = client
except Exception as e:
exp = e
sslLock.release()
sslTimer = createTimer()
sslTimer.start()
if(exp != None):
raise exp
def createTimer():
return Timer(SMPT_CLIENT_CLOSE_TIMEOUT, killSmptClient)
@ -51,9 +65,16 @@ async def sendEmail(message : MIMEMultipart):
message['From'] = f'{EMAIL_SENDER_NAME} <{EMAIL_SENDER_MAIL}>'
await openSmptClient()
logger.debug(f"[SMPT] Sending mail {message['From']} -> {message['to']} '{message['Subject']}'")
logger.info(f"[SMPT] sendEmail: Lock status: {self.updating.locked()}")
exp = None
sslLock.acquire()
try:
smptSender.sendmail(message['From'], message['to'], message.as_string())
except Exception as e:
exp = e
sslLock.release()
if(exp != None):
raise exp
def render_email_template(title = "", body = ""):
tpl = Environment(loader=FileSystemLoader("tpl"), autoescape=False).get_template('email/comunication.html')

17
ext.py
View File

@ -367,9 +367,24 @@ class OrderManager:
del cache[code]
orderList.remove(code)
async def fill_cache(self, check_itemsQuestions=False) -> bool:
# Check cache lock
logger.info(f"[CACHE] Lock status: {self.updating.locked()}")
self.updating.acquire()
ret = False
exp = None
try:
ret = await self.fill_cache_INTERNAL(check_itemsQuestions=check_itemsQuestions)
except Exception as e:
exp = e
self.updating.release()
logger.info(f"[CACHE] Ret status: {ret}. Exp: {exp}")
if(exp != None):
raise exp
return ret
async def fill_cache_INTERNAL(self, check_itemsQuestions=False) -> bool:
start_time = time()
logger.info("[CACHE] Filling cache...")
# Index item's ids
@ -412,8 +427,6 @@ class OrderManager:
except Exception:
logger.error(f"[CACHE] Error while refreshing cache.\n{traceback.format_exc()}")
success = False
finally:
self.updating.release()
# Apply new cache if there were no errors
if(success):