factors/email(minor): fix default timeout being 0, which breaks task

This commit is contained in:
Langhammer, Jens 2019-10-11 14:24:58 +02:00
parent f9babe7089
commit ae19236366
6 changed files with 37 additions and 14 deletions

View File

@ -1,6 +1,5 @@
"""passbook core tasks""" """passbook core tasks"""
from django.utils.timezone import now from django.utils.timezone import now
from structlog import get_logger from structlog import get_logger
from passbook.core.models import Nonce from passbook.core.models import Nonce

View File

@ -23,11 +23,11 @@ class EmailFactorView(AuthenticationFactor):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
nonce = Nonce.objects.create(user=self.pending_user) nonce = Nonce.objects.create(user=self.pending_user)
LOGGER.debug("DEBUG %s", str(nonce.uuid))
# Send mail to user # Send mail to user
message = TemplateEmailMessage( message = TemplateEmailMessage(
subject=_('Forgotten password'), subject=_('Forgotten password'),
template_name='email/account_password_reset.html', template_name='email/account_password_reset.html',
to=[self.pending_user.email],
template_context={ template_context={
'url': self.request.build_absolute_uri( 'url': self.request.build_absolute_uri(
reverse('passbook_core:auth-password-reset', reverse('passbook_core:auth-password-reset',

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2.6 on 2019-10-11 12:24
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('passbook_factors_email', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='emailfactor',
name='timeout',
field=models.IntegerField(default=10),
),
]

View File

@ -15,7 +15,7 @@ class EmailFactor(Factor):
password = models.TextField(default='', blank=True) password = models.TextField(default='', blank=True)
use_tls = models.BooleanField(default=False) use_tls = models.BooleanField(default=False)
use_ssl = models.BooleanField(default=False) use_ssl = models.BooleanField(default=False)
timeout = models.IntegerField(default=0) timeout = models.IntegerField(default=10)
ssl_keyfile = models.TextField(default=None, blank=True, null=True) ssl_keyfile = models.TextField(default=None, blank=True, null=True)
ssl_certfile = models.TextField(default=None, blank=True, null=True) ssl_certfile = models.TextField(default=None, blank=True, null=True)

View File

@ -4,10 +4,13 @@ from typing import Any, Dict, List
from celery import group from celery import group
from django.core.mail import EmailMessage from django.core.mail import EmailMessage
from structlog import get_logger
from passbook.factors.email.models import EmailFactor from passbook.factors.email.models import EmailFactor
from passbook.root.celery import CELERY_APP from passbook.root.celery import CELERY_APP
LOGGER = get_logger()
def send_mails(factor: EmailFactor, *messages: List[EmailMessage]): def send_mails(factor: EmailFactor, *messages: List[EmailMessage]):
"""Wrapper to convert EmailMessage to dict and send it from worker""" """Wrapper to convert EmailMessage to dict and send it from worker"""
@ -31,6 +34,7 @@ def _send_mail_task(self, email_factor_pk: int, message: Dict[Any, Any]):
for key, value in message.items(): for key, value in message.items():
setattr(message_object, key, value) setattr(message_object, key, value)
message_object.from_email = factor.from_address message_object.from_email = factor.from_address
LOGGER.debug("Sending mail", to=message_object.to)
try: try:
num_sent = factor.backend.send_messages([message_object]) num_sent = factor.backend.send_messages([message_object])
except SMTPException as exc: except SMTPException as exc:

View File

@ -2,7 +2,9 @@
import os import os
from logging.config import dictConfig from logging.config import dictConfig
from celery import Celery, signals from celery import Celery
from celery.signals import (after_task_publish, setup_logging, task_postrun,
task_prerun)
from django.conf import settings from django.conf import settings
from structlog import get_logger from structlog import get_logger
@ -10,39 +12,39 @@ from structlog import get_logger
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "passbook.root.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "passbook.root.settings")
LOGGER = get_logger() LOGGER = get_logger()
CELERY_APP = Celery('passbook') CELERY_APP = Celery('passbook')
# pylint: disable=unused-argument # pylint: disable=unused-argument
@signals.setup_logging.connect @setup_logging.connect
def config_loggers(*args, **kwags): def config_loggers(*args, **kwags):
"""Apply logging settings from settings.py to celery""" """Apply logging settings from settings.py to celery"""
dictConfig(settings.LOGGING) dictConfig(settings.LOGGING)
# pylint: disable=unused-argument # pylint: disable=unused-argument
@signals.after_task_publish.connect @after_task_publish.connect
def after_task_publish(sender=None, headers=None, body=None, **kwargs): def after_task_publish(sender=None, headers=None, body=None, **kwargs):
"""Log task_id after it was published""" """Log task_id after it was published"""
info = headers if 'task' in headers else body info = headers if 'task' in headers else body
LOGGER.debug('%-40s published (name=%s)', info.get('id'), info.get('task')) LOGGER.debug('Task published', task_id=info.get('id', ''), task_name=info.get('task', ''))
# pylint: disable=unused-argument # pylint: disable=unused-argument
@signals.task_prerun.connect @task_prerun.connect
def task_prerun(task_id, task, *args, **kwargs): def task_prerun(task_id, task, *args, **kwargs):
"""Log task_id on worker""" """Log task_id on worker"""
LOGGER.debug('%-40s started (name=%s)', task_id, task.__name__) LOGGER.debug('Task started', task_id=task_id, task_name=task.__name__)
# pylint: disable=unused-argument # pylint: disable=unused-argument
@signals.task_postrun.connect @task_postrun.connect
def task_postrun(task_id, task, *args, retval=None, state=None, **kwargs): def task_postrun(task_id, task, *args, retval=None, state=None, **kwargs):
"""Log task_id on worker""" """Log task_id on worker"""
LOGGER.debug('%-40s finished (name=%s, state=%s)', LOGGER.debug('Task finished',
task_id, task.__name__, state) task_id=task_id,
task_name=task.__name__,
state=state)
# Using a string here means the worker doesn't have to serialize # Using a string here means the worker doesn't have to serialize