Skip to content

Commit

Permalink
fix(mail): use deferred mailer to get rid of synchronous exceptions d…
Browse files Browse the repository at this point in the history
…uring SMTP timeout
  • Loading branch information
thejoeejoee committed Jan 5, 2024
1 parent db9a883 commit 4d316ec
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 6 deletions.
7 changes: 7 additions & 0 deletions charts/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ Create the name of the service account to use
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

{{/*
Component label
*/}}
{{- define "fiesta.componentLabels" -}}
component: {{ . }}
{{- end }}
3 changes: 3 additions & 0 deletions charts/templates/web-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ metadata:
name: web
labels:
{{- include "fiesta.labels" . | nindent 4 }}
{{- include "fiesta.componentLabels" "web" | nindent 4 }}
spec:
replicas: 1
selector:
matchLabels:
{{- include "fiesta.selectorLabels" . | nindent 6 }}
{{- include "fiesta.componentLabels" "web" | nindent 6 }}
template:
metadata:
labels:
app: web
{{- include "fiesta.selectorLabels" . | nindent 8 }}
{{- include "fiesta.componentLabels" "web" | nindent 8 }}
annotations:
kubectl.kubernetes.io/default-logs-container: web
spec:
Expand Down
65 changes: 65 additions & 0 deletions charts/templates/web-mailer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-mailer
labels:
{{- include "fiesta.labels" . | nindent 4 }}
{{- include "fiesta.componentLabels" "mailer" | nindent 4 }}
spec:
replicas: 1
selector:
matchLabels:
{{- include "fiesta.selectorLabels" . | nindent 6 }}
{{- include "fiesta.componentLabels" "mailer" | nindent 6 }}
template:
metadata:
labels:
{{- include "fiesta.selectorLabels" . | nindent 8 }}
{{- include "fiesta.componentLabels" "mailer" | nindent 8 }}
annotations:
kubectl.kubernetes.io/default-logs-container: mailer
spec:
containers:
- name: mailer
image: "{{ .Values.web.repository }}:{{ .Values.web.tag | default .Chart.AppVersion }}"
command:
- /bin/sh
- -c
- ./manage.py runmailer_pg
envFrom:
- secretRef:
name: {{ .Values.web.secretName }}
- configMapRef:
name: {{ .Values.web.configName }}

---
apiVersion: batch/v1
kind: CronJob
metadata:
name: web-mailer-retry
labels:
{{- include "fiesta.labels" . | nindent 4 }}
{{- include "fiesta.componentLabels" "mailer-retry" | nindent 4 }}
spec:
schedule: "0,15,30,45 * * * *" # Run every 15 minutes
jobTemplate:
spec:
template:
metadata:
labels:
{{- include "fiesta.selectorLabels" . | nindent 12 }}
{{- include "fiesta.componentLabels" "mailer-retry" | nindent 12 }}
spec:
containers:
- name: mailer-retry
image: "{{ .Values.web.repository }}:{{ .Values.web.tag | default .Chart.AppVersion }}"
command:
- /bin/sh
- -c
- ./manage.py retry_deferred
envFrom:
- secretRef:
name: {{ .Values.web.secretName }}
- configMapRef:
name: {{ .Values.web.configName }}
restartPolicy: OnFailure
4 changes: 2 additions & 2 deletions fiesta/fiesta/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .db import DatabaseConfigMixin
from .files import FilesConfigMixin, S3ConfigMixin
from .logging import LoggingConfigMixin, SentryConfigMixin
from .notifications import SmtpMailerConfigMixin
from .notifications import DatabaseSmtpMailerConfigMixin
from .project import ProjectConfigMixin
from .security import SecurityConfigMixin
from .templates import TemplatesConfigMixin
Expand Down Expand Up @@ -58,7 +58,7 @@ class LocalProduction(Base):


class Production(
SmtpMailerConfigMixin,
DatabaseSmtpMailerConfigMixin,
S3ConfigMixin,
SentryConfigMixin,
Base,
Expand Down
11 changes: 9 additions & 2 deletions fiesta/fiesta/settings/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ._utils import BaseConfigurationProtocol


class SmtpMailerConfigMixin(BaseConfigurationProtocol):
class DatabaseSmtpMailerConfigMixin(BaseConfigurationProtocol):
MAILER_PRIMARY_BACKEND = Value(default="django.core.mail.backends.smtp.EmailBackend")
MAILER_PRIMARY_TIMEOUT = PositiveIntegerValue(default=10, cast=int)
MAILER_PRIMARY_HOST_USE_TLS = BooleanValue(default=True)
Expand All @@ -14,8 +14,15 @@ class SmtpMailerConfigMixin(BaseConfigurationProtocol):
MAILER_PRIMARY_HOST_PASSWORD = SecretValue()
MAILER_PRIMARY_HOST_USER = SecretValue()

# cannot use file, because pods
MAILER_USE_FILE_LOCK = BooleanValue(default=False)

# backend used by django itself
EMAIL_BACKEND = Value(default="mailer.backend.DbBackend")

# mailer used by db mailer to actually send emails
@property
def EMAIL_BACKEND(self):
def MAILER_EMAIL_BACKEND(self):
return self.MAILER_PRIMARY_BACKEND

@property
Expand Down
5 changes: 4 additions & 1 deletion fiesta/fiesta/settings/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def ALLOWED_HOSTS(self):
return [f".{self.ROOT_DOMAIN}"]

# overwritten by production mixins
EMAIL_BACKEND = Value(default="django.core.mail.backends.console.EmailBackend")
EMAIL_BACKEND = Value(default="mailer.backend.DbBackend")
MAILER_EMAIL_BACKEND = Value(default="django.core.mail.backends.console.EmailBackend")

def DEFAULT_FROM_EMAIL(self):
return f"Fiesta+ <noreply@{self.ROOT_DOMAIN}>"
Expand Down Expand Up @@ -108,6 +109,8 @@ def DEFAULT_FROM_EMAIL(self):
# health checks
"health_check",
"health_check.contrib.migrations",
# django-mailer
"mailer",
]

MIDDLEWARE = [
Expand Down
30 changes: 29 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ sentry-sdk = {extras = ["django"], version = "^1.35.0"}
django-admin-env-notice = "^1.0"
cryptography = "41.0.7"
django-admin-relation-links = "^0.2.5"
django-mailer = "^2.3.1"

[tool.poetry.dev-dependencies]
pre-commit = "^2.17.0"
Expand Down

0 comments on commit 4d316ec

Please sign in to comment.