Merge pull request #2934 from bookwyrm-social/reduce-status-tasks

Only trigger add_status_task when status is first created
This commit is contained in:
Mouse Reeve 2023-08-01 08:30:43 -07:00 committed by GitHub
commit 211b60bba2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 7 deletions

View file

@ -329,10 +329,9 @@ def add_status_on_create(sender, instance, created, *args, **kwargs):
remove_status_task.delay(instance.id) remove_status_task.delay(instance.id)
return return
# To avoid creating a zillion unnecessary tasks caused by re-saving the model, # We don't want to create multiple add_status_tasks for each status, and because
# check if it's actually ready to send before we go. We're trusting this was # the transactions are atomic, on_commit won't run until the status is ready to add.
# set correctly by the inbox or view if not created:
if not instance.ready:
return return
# when creating new things, gotta wait on the transaction # when creating new things, gotta wait on the transaction

View file

@ -62,7 +62,7 @@ class StatusTransactions(TransactionTestCase):
with patch("bookwyrm.activitystreams.add_status_task.apply_async") as mock: with patch("bookwyrm.activitystreams.add_status_task.apply_async") as mock:
view(request, "comment") view(request, "comment")
self.assertEqual(mock.call_count, 2) self.assertEqual(mock.call_count, 1)
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay") @patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")

View file

@ -6,6 +6,7 @@ from urllib.parse import urlparse
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.validators import URLValidator from django.core.validators import URLValidator
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import transaction
from django.db.models import Q from django.db.models import Q
from django.http import HttpResponse, HttpResponseBadRequest, Http404 from django.http import HttpResponse, HttpResponseBadRequest, Http404
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
@ -56,6 +57,7 @@ class CreateStatus(View):
return TemplateResponse(request, "compose.html", data) return TemplateResponse(request, "compose.html", data)
# pylint: disable=too-many-branches # pylint: disable=too-many-branches
@transaction.atomic
def post(self, request, status_type, existing_status_id=None): def post(self, request, status_type, existing_status_id=None):
"""create status of whatever type""" """create status of whatever type"""
created = not existing_status_id created = not existing_status_id
@ -83,7 +85,6 @@ class CreateStatus(View):
return redirect_to_referer(request) return redirect_to_referer(request)
status = form.save(request, commit=False) status = form.save(request, commit=False)
status.ready = False
# save the plain, unformatted version of the status for future editing # save the plain, unformatted version of the status for future editing
status.raw_content = status.content status.raw_content = status.content
if hasattr(status, "quote"): if hasattr(status, "quote"):
@ -123,7 +124,6 @@ class CreateStatus(View):
if hasattr(status, "quote"): if hasattr(status, "quote"):
status.quote = to_markdown(status.quote) status.quote = to_markdown(status.quote)
status.ready = True
status.save(created=created) status.save(created=created)
# update a readthrough, if needed # update a readthrough, if needed