Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #30 from ds-wizard/release/3.13.0
Browse files Browse the repository at this point in the history
Release 3.13.0
  • Loading branch information
MarekSuchanek committed Jun 28, 2022
2 parents 32048a7 + 0ce79f9 commit 5e37db6
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: 3.9

Expand Down Expand Up @@ -42,7 +42,7 @@ jobs:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: 3.9

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ jobs:
- uses: actions/checkout@v3

# (1) -> Build Docker image
- name: Update build info
run: |
./scripts/build-info.sh
- name: Docker build
run: |
docker build -t ${PRIVATE_IMAGE#/}:$GITHUB_SHA .
Expand Down
8 changes: 8 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ mail:
count: 10
# SMTP connection timeout (default: 5 seconds)
timeout: 5

#sentry:
# enabled:
# workersDsn:

#general:
# environment: Test
# clientUrl: http://localhost:3001
57 changes: 56 additions & 1 deletion dsw_mailer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ def __init__(self, missing: List[str]):
self.missing = missing


class GeneralConfig:

def __init__(self, environment: str, client_url: str):
self.environment = environment
self.client_url = client_url

def __str__(self):
return f'GeneralConfig\n' \
f'- environment = {self.environment} ({type(self.environment)})\n' \
f'- client_url = {self.client_url} ({type(self.client_url)})\n'


class SentryConfig:

def __init__(self, enabled: bool, workers_dsn: Optional[str]):
self.enabled = enabled
self.workers_dsn = workers_dsn

def __str__(self):
return f'SentryConfig\n' \
f'- enabled = {self.enabled} ({type(self.enabled)})\n' \
f'- workers_dsn = {self.workers_dsn} ({type(self.workers_dsn)})\n'


class DatabaseConfig:

def __init__(self, connection_string: str, connection_timeout: int,
Expand Down Expand Up @@ -110,17 +134,22 @@ def __str__(self):
class MailerConfig:

def __init__(self, db: DatabaseConfig, log: LoggingConfig,
mail: MailConfig):
mail: MailConfig, sentry: SentryConfig,
general: GeneralConfig):
self.db = db
self.log = log
self.mail = mail
self.sentry = sentry
self.general = general

def __str__(self):
return f'MailerConfig\n' \
f'====================\n' \
f'{self.db}' \
f'{self.log}' \
f'{self.mail}' \
f'{self.sentry}' \
f'{self.general}' \
f'====================\n'


Expand All @@ -129,6 +158,8 @@ class MailerConfigParser:
DB_SECTION = 'database'
LOGGING_SECTION = 'logging'
MAIL_SECTION = 'mail'
SENTRY_SECTION = 'sentry'
GENERAL_SECTION = 'general'

DEFAULTS = {
DB_SECTION: {
Expand Down Expand Up @@ -160,6 +191,14 @@ class MailerConfigParser:
},
'timeout': 5,
},
SENTRY_SECTION: {
'enabled': False,
'workersDsn': None
},
GENERAL_SECTION: {
'environment': 'Production',
'clientUrl': 'http://localhost:8080',
},
}

REQUIRED = [
Expand Down Expand Up @@ -259,10 +298,26 @@ def mail(self):
timeout=int(self.get_or_default(self.MAIL_SECTION, 'timeout')),
)

@property
def sentry(self) -> SentryConfig:
return SentryConfig(
enabled=self.get_or_default(self.SENTRY_SECTION, 'enabled'),
workers_dsn=self.get_or_default(self.SENTRY_SECTION, 'workersDsn'),
)

@property
def general(self) -> GeneralConfig:
return GeneralConfig(
environment=self.get_or_default(self.GENERAL_SECTION, 'environment'),
client_url=self.get_or_default(self.GENERAL_SECTION, 'clientUrl'),
)

@property
def config(self) -> MailerConfig:
return MailerConfig(
db=self.db,
log=self.logging,
mail=self.mail,
sentry=self.sentry,
general=self.general,
)
3 changes: 2 additions & 1 deletion dsw_mailer/connection/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .command_queue import CommandWorker, CommandQueue
from .database import Database, PostgresConnection, PersistentCommand
from .sentry import SentryReporter
from .smtp import SMTPSender

__all__ = ['CommandQueue', 'CommandWorker', 'Database', 'PersistentCommand',
'PostgresConnection', 'SMTPSender']
'PostgresConnection', 'SentryReporter', 'SMTPSender']
25 changes: 25 additions & 0 deletions dsw_mailer/connection/sentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sentry_sdk

from ..consts import BUILD_INFO


class SentryReporter:

@classmethod
def initialize(cls, dsn: str, server_name: str, environment: str):
sentry_sdk.init(
dsn=dsn,
traces_sample_rate=1.0,
release=BUILD_INFO['version'],
server_name=server_name,
environment=environment,
)
sentry_sdk.set_tag('worker', BUILD_INFO['name'])

@staticmethod
def capture_exception(*args, **kwargs):
sentry_sdk.capture_exception(*args, **kwargs)

@staticmethod
def capture_message(*args, **kwargs):
sentry_sdk.capture_message(*args, **kwargs)
16 changes: 15 additions & 1 deletion dsw_mailer/consts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PROG_NAME = 'dsw-mailer'
VERSION = '3.12.0'
PACKAGE_VERSION = '3.13.0'

LOGGER_NAME = 'mailer'

Expand All @@ -10,6 +10,20 @@
CMD_COMPONENT = 'mailer'


_DEFAULT_BUILT_AT = 'BUILT_AT'
BUILT_AT = '--BUILT_AT--'
_DEFAULT_VERSION = 'VERSION'
VERSION = '--VERSION--'


BUILD_INFO = {
'name': PROG_NAME,
'packageVersion': PACKAGE_VERSION,
'version': VERSION if VERSION != f'--{_DEFAULT_VERSION}--' else 'unknown',
'builtAt': BUILT_AT if BUILT_AT != f'--{_DEFAULT_BUILT_AT}--' else 'unknown',
}


class CommandState:
NEW = 'NewPersistentCommandState'
DONE = 'DonePersistentCommandState'
Expand Down
9 changes: 8 additions & 1 deletion dsw_mailer/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .config import MailerConfig
from .connection import Database, SMTPSender, PersistentCommand, \
CommandWorker, CommandQueue
CommandWorker, CommandQueue, SentryReporter
from .connection.database import DBAppConfig
from .consts import Queries, CMD_COMPONENT
from .context import Context
Expand Down Expand Up @@ -38,6 +38,12 @@ def _init_context(self, workdir: pathlib.Path, mode: str):
sender=SMTPSender(cfg=self.cfg.mail),
mode=mode,
)
if self.cfg.sentry.enabled and self.cfg.sentry.workers_dsn is not None:
SentryReporter.initialize(
dsn=self.cfg.sentry.workers_dsn,
environment=self.cfg.general.environment,
server_name=self.cfg.general.client_url,
)

def _prepare_logging(self):
prepare_logging(cfg=self.cfg)
Expand All @@ -60,6 +66,7 @@ def work(self) -> bool:
self._process_command(cmd)
except Exception as e:
Context.logger.warning(f'Errored with exception: {str(e)}')
SentryReporter.capture_exception(e)
ctx.app.db.execute_query(
query=Queries.UPDATE_CMD_ERROR,
attempts=command.get('attempts', 0) + 1,
Expand Down
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ click==8.1.3
colorama==0.4.4
Jinja2==3.1.2
MarkupSafe==2.1.1
minio==7.1.8
minio==7.1.9
pathvalidate==2.5.0
psycopg2==2.9.3
PyYAML==6.0
sentry-sdk==1.5.12
tenacity==8.0.1
urllib3==1.26.9
25 changes: 25 additions & 0 deletions scripts/build-info.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
set -e

# File with build info
BUILD_INFO_FILE=dsw_mailer/consts.py

# Create version based on git tag or branch
branch=$(git rev-parse --abbrev-ref HEAD)
commit=$(git rev-parse --short HEAD)
version="$branch~$commit"
gittag=$(git tag -l --contains HEAD | head -n 1)
if test -n "$gittag"
then
version="$gittag~$commit"
fi

# Get build timestamp
builtAt=$(date +"%Y-%m-%d %TZ")

cat $BUILD_INFO_FILE
# Replace values
sed -i.bak "s#--BUILT_AT--#$version#" $BUILD_INFO_FILE && rm $BUILD_INFO_FILE".bak"
sed -i.bak "s#--VERSION--#$builtAt#" $BUILD_INFO_FILE && rm $BUILD_INFO_FILE".bak"

cat $BUILD_INFO_FILE
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='dsw_mailer',
version='3.12.0',
version='3.13.0',
description='Worker for sending email notifications',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 5e37db6

Please sign in to comment.