From ae19236366cefeab3fde03e4281e585618fd243f Mon Sep 17 00:00:00 2001 From: "Langhammer, Jens" Date: Fri, 11 Oct 2019 14:24:58 +0200 Subject: [PATCH] factors/email(minor): fix default timeout being 0, which breaks task --- passbook/core/tasks.py | 1 - passbook/factors/email/factor.py | 2 +- .../migrations/0002_auto_20191011_1224.py | 18 ++++++++++++++ passbook/factors/email/models.py | 2 +- passbook/factors/email/tasks.py | 4 ++++ passbook/root/celery.py | 24 ++++++++++--------- 6 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 passbook/factors/email/migrations/0002_auto_20191011_1224.py diff --git a/passbook/core/tasks.py b/passbook/core/tasks.py index 5e2c3ab8d..2966d4ee0 100644 --- a/passbook/core/tasks.py +++ b/passbook/core/tasks.py @@ -1,6 +1,5 @@ """passbook core tasks""" from django.utils.timezone import now - from structlog import get_logger from passbook.core.models import Nonce diff --git a/passbook/factors/email/factor.py b/passbook/factors/email/factor.py index 7085b841b..dbd70e0e6 100644 --- a/passbook/factors/email/factor.py +++ b/passbook/factors/email/factor.py @@ -23,11 +23,11 @@ class EmailFactorView(AuthenticationFactor): def get(self, request, *args, **kwargs): nonce = Nonce.objects.create(user=self.pending_user) - LOGGER.debug("DEBUG %s", str(nonce.uuid)) # Send mail to user message = TemplateEmailMessage( subject=_('Forgotten password'), template_name='email/account_password_reset.html', + to=[self.pending_user.email], template_context={ 'url': self.request.build_absolute_uri( reverse('passbook_core:auth-password-reset', diff --git a/passbook/factors/email/migrations/0002_auto_20191011_1224.py b/passbook/factors/email/migrations/0002_auto_20191011_1224.py new file mode 100644 index 000000000..7b2e96cc3 --- /dev/null +++ b/passbook/factors/email/migrations/0002_auto_20191011_1224.py @@ -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), + ), + ] diff --git a/passbook/factors/email/models.py b/passbook/factors/email/models.py index 6becb241f..c02a257dd 100644 --- a/passbook/factors/email/models.py +++ b/passbook/factors/email/models.py @@ -15,7 +15,7 @@ class EmailFactor(Factor): password = models.TextField(default='', blank=True) use_tls = 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_certfile = models.TextField(default=None, blank=True, null=True) diff --git a/passbook/factors/email/tasks.py b/passbook/factors/email/tasks.py index 7b03d3584..5ec02de79 100644 --- a/passbook/factors/email/tasks.py +++ b/passbook/factors/email/tasks.py @@ -4,10 +4,13 @@ from typing import Any, Dict, List from celery import group from django.core.mail import EmailMessage +from structlog import get_logger from passbook.factors.email.models import EmailFactor from passbook.root.celery import CELERY_APP +LOGGER = get_logger() + def send_mails(factor: EmailFactor, *messages: List[EmailMessage]): """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(): setattr(message_object, key, value) message_object.from_email = factor.from_address + LOGGER.debug("Sending mail", to=message_object.to) try: num_sent = factor.backend.send_messages([message_object]) except SMTPException as exc: diff --git a/passbook/root/celery.py b/passbook/root/celery.py index d87531ea0..2b7c61286 100644 --- a/passbook/root/celery.py +++ b/passbook/root/celery.py @@ -2,7 +2,9 @@ import os 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 structlog import get_logger @@ -10,39 +12,39 @@ from structlog import get_logger os.environ.setdefault("DJANGO_SETTINGS_MODULE", "passbook.root.settings") LOGGER = get_logger() - - CELERY_APP = Celery('passbook') # pylint: disable=unused-argument -@signals.setup_logging.connect +@setup_logging.connect def config_loggers(*args, **kwags): """Apply logging settings from settings.py to celery""" dictConfig(settings.LOGGING) # pylint: disable=unused-argument -@signals.after_task_publish.connect +@after_task_publish.connect def after_task_publish(sender=None, headers=None, body=None, **kwargs): """Log task_id after it was published""" 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 -@signals.task_prerun.connect +@task_prerun.connect def task_prerun(task_id, task, *args, **kwargs): """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 -@signals.task_postrun.connect +@task_postrun.connect def task_postrun(task_id, task, *args, retval=None, state=None, **kwargs): """Log task_id on worker""" - LOGGER.debug('%-40s finished (name=%s, state=%s)', - task_id, task.__name__, state) + LOGGER.debug('Task finished', + task_id=task_id, + task_name=task.__name__, + state=state) # Using a string here means the worker doesn't have to serialize