Commit 4ee3275b authored by Robert's avatar Robert

Refactor task execution code, remove (A)SYNC_EMAIL_BACKEND. Failed tasks can...

Refactor task execution code, remove (A)SYNC_EMAIL_BACKEND. Failed tasks can be repeated by calling management command repeat_tasks.
parent e20614fb
Pipeline #1746 passed with stage
in 24 minutes and 46 seconds
......@@ -256,8 +256,11 @@ CSP_STYLE_SRC = ("'self'", "'unsafe-inline'")
# grouprise Repeatable Tasks (grouprise.core)
GROUPRISE_REPEATABLE_TASKS = {
'content2.Content': 'grouprise.features.content.tasks.send_content_notifications',
'contributions.Contribution': 'grouprise.features.contributions.tasks.send_contribution_notifications',
'content2.Content':
'grouprise.features.content.notifications.send_content_notifications',
'contributions.Contribution':
'grouprise.features.contributions.notifications.send_contribution_notifications',
}
......
from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils.module_loading import import_string
from grouprise.core.models import RepeatableTask
from grouprise.core.tasks import auto_task
def repeat_tasks(task_queryset):
for task in task_queryset:
obj = task.task_parameter
label = obj._meta.label
method_name = settings.GROUPRISE_REPEATABLE_TASKS[label]
task_method = import_string(method_name)
try:
task_method(obj)
auto_task(task.task_parameter)
finally:
task.delete()
......
......@@ -8,7 +8,6 @@ from django.apps import apps
from django.conf import settings
from django.contrib.sites.models import Site
from django.core import mail
from django.core.mail import get_connection
from django.template import loader
from grouprise.core.models import PermissionToken
......@@ -194,11 +193,5 @@ class Notification:
for file_name in self.get_attachments():
message.attach_file(file_name)
# optional connection switch
if kwargs.get('use_sync_email_backend'):
message.connection = get_connection(settings.SYNC_EMAIL_BACKEND)
elif kwargs.get('use_async_email_backend'):
message.connection = get_connection(settings.ASYNC_EMAIL_BACKEND)
# we don't expect errors when sending mails because we just pass mails to django-mailer
message.send()
from django.conf import settings
from django.utils.module_loading import import_string
from huey.contrib.djhuey import db_task
from grouprise.core.models import RepeatableTask
@db_task()
def auto_task(model):
task = RepeatableTask.objects.create(
content_type=model.content_type, object_id=model.id)
model_class_label = model._meta.label
task_method_name = settings.GROUPRISE_REPEATABLE_TASKS[model_class_label]
task_method = import_string(task_method_name)
task_method(model)
task.delete()
......@@ -76,3 +76,7 @@ class ContentCreated(grouprise.core.notifications.Notification):
if self.association:
return reverse('content-permalink', args=(self.association.pk,))
return super().get_url()
def send_content_notifications(instance):
ContentCreated.send_all(instance)
......@@ -3,8 +3,7 @@ from datetime import timedelta
from django.dispatch import receiver, Signal
from django.utils.timezone import now
from grouprise.features.content.notifications import ContentCreated
from grouprise.features.content.tasks import send_content_notifications
from grouprise.core.tasks import auto_task
post_create = Signal(providing_args=['instance'])
......@@ -12,7 +11,4 @@ post_create = Signal(providing_args=['instance'])
@receiver(post_create)
def content_created(sender, instance, **kwargs):
if now() - instance.versions.last().time_created < timedelta(weeks=1):
# send mails synchronously via default EMAIL_BACKEND
ContentCreated.send_all(instance, use_sync_email_backend=True)
# send mails asynchronously (task queue) via ASYNC_EMAIL_BACKEND
send_content_notifications(instance)
auto_task(instance)
from huey.contrib.djhuey import db_task
from grouprise.core.models import RepeatableTask
from grouprise.features.content.notifications import ContentCreated
@db_task()
def send_content_notifications(instance):
# we save this task's parameter as long as the task runs
task = RepeatableTask.objects.create(
content_type=instance.content_type, object_id=instance.id)
# send notifications (the actual task)
ContentCreated.send_all(instance, use_async_email_backend=True)
# remove the saved parameter on success (no exception)
task.delete()
......@@ -110,3 +110,7 @@ class ContributionCreated(notifications.Notification):
def is_reply(self):
return not (self.object.container.is_conversation
and self.object.container.contributions.first() == self.object)
def send_contribution_notifications(instance):
ContributionCreated.send_all(instance)
import django.db.models.signals
from django.dispatch import receiver
from grouprise.features.contributions.notifications import ContributionCreated
from grouprise.features.contributions.tasks import send_contribution_notifications
from grouprise.core.tasks import auto_task
post_create = django.dispatch.Signal(providing_args=['instance'])
@receiver(post_create)
def contribution_created(sender, instance, **kwargs):
# send mails synchronously via default EMAIL_BACKEND
ContributionCreated.send_all(instance, use_sync_email_backend=True)
# send mails asynchronously (task queue) via ASYNC_EMAIL_BACKEND
send_contribution_notifications(instance)
auto_task(instance)
from huey.contrib.djhuey import db_task
from grouprise.core.models import RepeatableTask
from grouprise.features.contributions.notifications import ContributionCreated
@db_task()
def send_contribution_notifications(instance):
# we save this task's parameter as long as the task runs
task = RepeatableTask.objects.create(
content_type=instance.content_type, object_id=instance.id)
# send notifications (the actual task)
ContributionCreated.send_all(instance, use_async_email_backend=True)
# remove the saved parameter on success (no exception)
task.delete()
......@@ -9,8 +9,7 @@ DATABASES = {
DEBUG = True
EMAIL_BACKEND = SYNC_EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ASYNC_EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
GROUPRISE = {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment