Merge pull request #673 from openreplay/api-v1.7.0_hotfix

Api v1.7.0 hotfix
This commit is contained in:
Kraiem Taha Yassine 2022-08-11 17:36:48 +01:00 committed by GitHub
commit fef1c05044
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 17 deletions

View file

@ -139,9 +139,9 @@ def send_by_email(notification, destination):
def send_by_email_batch(notifications_list): def send_by_email_batch(notifications_list):
if not helper.has_smtp(): if not helper.has_smtp():
print("no SMTP configuration for email notifications") logging.info("no SMTP configuration for email notifications")
if notifications_list is None or len(notifications_list) == 0: if notifications_list is None or len(notifications_list) == 0:
print("no email notifications") logging.info("no email notifications")
return return
for n in notifications_list: for n in notifications_list:
send_by_email(notification=n.get("notification"), destination=n.get("destination")) send_by_email(notification=n.get("notification"), destination=n.get("destination"))

View file

@ -1,13 +1,15 @@
import base64 import base64
import logging
import re import re
from email.header import Header from email.header import Header
from email.mime.image import MIMEImage from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from chalicelib.utils import helper, smtp
from decouple import config from decouple import config
from chalicelib.utils import smtp
def __get_subject(subject): def __get_subject(subject):
return subject return subject
@ -64,11 +66,11 @@ def send_html(BODY_HTML, SUBJECT, recipient, bcc=None):
if bcc is not None and len(bcc) > 0: if bcc is not None and len(bcc) > 0:
r += [bcc] r += [bcc]
try: try:
print(f"Email sending to: {r}") logging.info(f"Email sending to: {r}")
s.sendmail(msg['FROM'], r, msg.as_string().encode('ascii')) s.sendmail(msg['FROM'], r, msg.as_string().encode('ascii'))
except Exception as e: except Exception as e:
print("!!! Email error!") logging.error("!!! Email error!")
print(e) logging.error(e)
def send_text(recipients, text, subject): def send_text(recipients, text, subject):
@ -82,8 +84,8 @@ def send_text(recipients, text, subject):
try: try:
s.sendmail(msg['FROM'], recipients, msg.as_string().encode('ascii')) s.sendmail(msg['FROM'], recipients, msg.as_string().encode('ascii'))
except Exception as e: except Exception as e:
print("!! Text-email failed: " + subject), logging.error("!! Text-email failed: " + subject),
print(e) logging.error(e)
def __escape_text_html(text): def __escape_text_html(text):

View file

@ -1,5 +1,5 @@
from chalicelib.utils.TimeUTC import TimeUTC from chalicelib.utils.TimeUTC import TimeUTC
from chalicelib.utils.email_handler import __get_html_from_file, send_html, __escape_text_html from chalicelib.utils.email_handler import __get_html_from_file, send_html
def send_team_invitation(recipient, client_id, sender_name, invitation_link): def send_team_invitation(recipient, client_id, sender_name, invitation_link):

View file

@ -1,3 +1,4 @@
import logging
import time import time
from threading import Semaphore from threading import Semaphore
@ -52,18 +53,18 @@ def make_pool():
try: try:
postgreSQL_pool.closeall() postgreSQL_pool.closeall()
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
print("Error while closing all connexions to PostgreSQL", error) logging.error("Error while closing all connexions to PostgreSQL", error)
try: try:
postgreSQL_pool = ORThreadedConnectionPool(config("pg_minconn", cast=int, default=20), postgreSQL_pool = ORThreadedConnectionPool(config("pg_minconn", cast=int, default=20),
config("pg_maxconn", cast=int, default=80), config("pg_maxconn", cast=int, default=80),
**PG_CONFIG) **PG_CONFIG)
if (postgreSQL_pool): if (postgreSQL_pool):
print("Connection pool created successfully") logging.info("Connection pool created successfully")
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
print("Error while connecting to PostgreSQL", error) logging.error("Error while connecting to PostgreSQL", error)
if RETRY < RETRY_MAX: if RETRY < RETRY_MAX:
RETRY += 1 RETRY += 1
print(f"waiting for {RETRY_INTERVAL}s before retry n°{RETRY}") logging.info(f"waiting for {RETRY_INTERVAL}s before retry n°{RETRY}")
time.sleep(RETRY_INTERVAL) time.sleep(RETRY_INTERVAL)
make_pool() make_pool()
else: else:
@ -113,12 +114,12 @@ class PostgresClient:
if self.long_query or self.unlimited_query: if self.long_query or self.unlimited_query:
self.connection.close() self.connection.close()
except Exception as error: except Exception as error:
print("Error while committing/closing PG-connection", error) logging.error("Error while committing/closing PG-connection", error)
if str(error) == "connection already closed" \ if str(error) == "connection already closed" \
and not self.long_query \ and not self.long_query \
and not self.unlimited_query \ and not self.unlimited_query \
and config('PG_POOL', cast=bool, default=True): and config('PG_POOL', cast=bool, default=True):
print("Recreating the connexion pool") logging.info("Recreating the connexion pool")
make_pool() make_pool()
else: else:
raise error raise error

View file

@ -1,10 +1,14 @@
import logging
import smtplib import smtplib
from smtplib import SMTPAuthenticationError
from decouple import config from decouple import config
from starlette.exceptions import HTTPException
class EmptySMTP: class EmptySMTP:
def sendmail(self, from_addr, to_addrs, msg, mail_options=(), rcpt_options=()): def sendmail(self, from_addr, to_addrs, msg, mail_options=(), rcpt_options=()):
print("!! CANNOT SEND EMAIL, NO VALID SMTP CONFIGURATION FOUND") logging.error("!! CANNOT SEND EMAIL, NO VALID SMTP CONFIGURATION FOUND")
class SMTPClient: class SMTPClient:
@ -31,7 +35,10 @@ class SMTPClient:
# stmplib docs recommend calling ehlo() before & after starttls() # stmplib docs recommend calling ehlo() before & after starttls()
self.server.ehlo() self.server.ehlo()
if len(config("EMAIL_USER", default="")) > 0 and len(config("EMAIL_PASSWORD", default="")) > 0: if len(config("EMAIL_USER", default="")) > 0 and len(config("EMAIL_PASSWORD", default="")) > 0:
self.server.login(user=config("EMAIL_USER"), password=config("EMAIL_PASSWORD")) try:
self.server.login(user=config("EMAIL_USER"), password=config("EMAIL_PASSWORD"))
except SMTPAuthenticationError:
raise HTTPException(401, "SMTP Authentication Error")
return self.server return self.server
def __exit__(self, *args): def __exit__(self, *args):