Skip to content

Commit

Permalink
[DSW-2011] Improve mailer versatility
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekSuchanek committed Jul 21, 2023
1 parent f2cdcda commit a0509b9
Show file tree
Hide file tree
Showing 41 changed files with 198 additions and 653 deletions.
26 changes: 25 additions & 1 deletion packages/dsw-database/dsw/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Database:
'WHERE document_template_id = %s AND app_uuid = %s;'
SELECT_TEMPLATE_ASSETS = 'SELECT * FROM document_template_asset ' \
'WHERE document_template_id = %s AND app_uuid = %s;'
CHECK_TABLE_EXISTS = 'SELECT EXISTS(SELECT * FROM information_schema.tables' \
' WHERE table_name = %(table_name)s)'
SELECT_MAIL_CONFIG = 'SELECT icm.* ' \
'FROM app_config ac JOIN instance_config_mail icm ' \
'ON ac.mail_config_uuid = icm.uuid ' \
Expand Down Expand Up @@ -89,6 +91,24 @@ def connect(self):
self.conn_query.connect()
self.conn_queue.connect()

@tenacity.retry(
reraise=True,
wait=tenacity.wait_exponential(multiplier=RETRY_QUERY_MULTIPLIER),
stop=tenacity.stop_after_attempt(RETRY_QUERY_TRIES),
before=tenacity.before_log(LOG, logging.DEBUG),
after=tenacity.after_log(LOG, logging.DEBUG),
)
def _check_table_exists(self, table_name: str) -> bool:
with self.conn_query.new_cursor() as cursor:
try:
cursor.execute(
query=self.CHECK_TABLE_EXISTS,
params={'table_name': table_name},
)
return cursor.fetchone()[0]
except Exception:
return False

@tenacity.retry(
reraise=True,
wait=tenacity.wait_exponential(multiplier=RETRY_QUERY_MULTIPLIER),
Expand Down Expand Up @@ -356,6 +376,8 @@ def get_app_config(self, app_uuid: str) -> Optional[DBAppConfig]:
)
def get_mail_config(self, app_uuid: str) -> Optional[DBInstanceConfigMail]:
with self.conn_query.new_cursor(use_dict=True) as cursor:
if not self._check_table_exists(table_name='instance_config_mail'):
return None
try:
cursor.execute(
query=self.SELECT_MAIL_CONFIG,
Expand All @@ -366,7 +388,7 @@ def get_mail_config(self, app_uuid: str) -> Optional[DBInstanceConfigMail]:
return None
return DBInstanceConfigMail.from_dict_row(data=result)
except Exception as e:
LOG.warning(f'Could not retrieve instance_mail_config for app'
LOG.warning(f'Could not retrieve instance_config_mail for app'
f' "{app_uuid}": {str(e)}')
return None

Expand All @@ -379,6 +401,8 @@ def get_mail_config(self, app_uuid: str) -> Optional[DBInstanceConfigMail]:
)
def update_component_info(self, name: str, version: str, built_at: datetime.datetime):
with self.conn_query.new_cursor(use_dict=True) as cursor:
if not self._check_table_exists(table_name='component'):
return None
ts_now = datetime.datetime.utcnow()
try:
cursor.execute(
Expand Down
1 change: 0 additions & 1 deletion packages/dsw-mailer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ FROM datastewardshipwizard/python-base:3.11-alpine

ENV APPLICATION_CONFIG_PATH /app/config/application.yml
ENV WORKDIR_PATH /home/user/templates
ENV MAILER_MODE wizard
ENV PATH "/home/user/.local/bin:$PATH"

# Setup non-root user
Expand Down
7 changes: 2 additions & 5 deletions packages/dsw-mailer/dsw/mailer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,11 @@ def extract_message_request(ctx, param, value: IO):
type=click.File('r', encoding='utf-8'))
@click.option('-w', '--workdir', envvar='WORKDIR_PATH',
type=click.Path(dir_okay=True, exists=True))
@click.option('-m', '--mode', envvar='MAILER_MODE',
type=click.Choice(['wizard', 'registry']),
default='wizard')
def cli(ctx, config: MailerConfig, workdir: str, mode: str):
def cli(ctx, config: MailerConfig, workdir: str):
path_workdir = pathlib.Path(workdir)
from .mailer import Mailer
config.log.apply()
ctx.obj['mailer'] = Mailer(config, path_workdir, mode)
ctx.obj['mailer'] = Mailer(config, path_workdir)


@cli.command(name='send', help='Send message(s) from given file directly.')
Expand Down
1 change: 1 addition & 0 deletions packages/dsw-mailer/dsw/mailer/consts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
COMPONENT_NAME = 'Mailer'
CMD_CHANNEL = 'mailer'
CMD_COMPONENT = 'mailer'
CMD_FUNCTION = 'sendMail'
DEFAULT_ENCODING = 'utf-8'
NULL_UUID = '00000000-0000-0000-0000-000000000000'
PROG_NAME = 'dsw-mailer'
Expand Down
20 changes: 2 additions & 18 deletions packages/dsw-mailer/dsw/mailer/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ def __init__(self, trace_id: str):
class _Context:

def __init__(self, app: AppContext, job: JobContext,
templates: TemplateRegistry, mode: str):
templates: TemplateRegistry):
self.app = app
self.job = job
self.templates = templates
self.mode = mode

def update_trace_id(self, trace_id: str):
self.app.cfg.log.set_logging_extra('traceId', trace_id)
Expand All @@ -59,15 +58,7 @@ def get(cls) -> _Context:
return cls._instance

@classmethod
def is_registry_mode(cls):
return cls.get().mode == 'registry'

@classmethod
def is_wizard_mode(cls):
return cls.get().mode == 'wizard'

@classmethod
def initialize(cls, db, config, sender, workdir, mode):
def initialize(cls, db, config, sender, workdir):
cls._instance = _Context(
app=AppContext(
db=db,
Expand All @@ -81,12 +72,5 @@ def initialize(cls, db, config, sender, workdir, mode):
templates=TemplateRegistry(
cfg=config,
workdir=workdir,
mode=mode,
),
mode=mode,
)
if cls.get().app.cfg.mail.name == '':
if cls.is_registry_mode():
cls.get().app.cfg.mail.name = 'DSW Registry'
elif cls.is_wizard_mode():
cls.get().app.cfg.mail.name = 'DSW'
Loading

0 comments on commit a0509b9

Please sign in to comment.