From 23e498879e73eb5f0b21bdcdfc7b65613f170a38 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 4 Jan 2022 14:17:14 -0800 Subject: [PATCH] Fixes account create tasks --- bookwyrm/activitystreams.py | 8 +++++++- bookwyrm/lists_stream.py | 16 ++++++++++------ bookwyrm/suggested_users.py | 8 +++++++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/bookwyrm/activitystreams.py b/bookwyrm/activitystreams.py index 4cba9939..f2dd43fb 100644 --- a/bookwyrm/activitystreams.py +++ b/bookwyrm/activitystreams.py @@ -397,9 +397,15 @@ def populate_streams_on_account_create(sender, instance, created, *args, **kwarg """build a user's feeds when they join""" if not created or not instance.local: return + transaction.on_commit( + lambda: populate_streams_on_account_create_command(instance.id) + ) + +def populate_streams_on_account_create_command(instance_id): + """wait for the transaction to complete""" for stream in streams: - populate_stream_task.delay(stream, instance.id) + populate_stream_task.delay(stream, instance_id) @receiver(signals.pre_save, sender=models.ShelfBook) diff --git a/bookwyrm/lists_stream.py b/bookwyrm/lists_stream.py index bb0ae1c8..79fabe43 100644 --- a/bookwyrm/lists_stream.py +++ b/bookwyrm/lists_stream.py @@ -52,7 +52,7 @@ class ListsStream(RedisStore): .distinct() ) - def populate_streams(self, user): + def populate_lists(self, user): """go from zero to a timeline""" self.populate_store(self.stream_id(user)) @@ -116,7 +116,7 @@ def add_list_on_create(sender, instance, created, *args, **kwargs): if not created: return # when creating new things, gotta wait on the transaction - transaction.on_commit(lambda: add_list_on_create_command(instance)) + transaction.on_commit(lambda: add_list_on_create_command(instance.id)) @receiver(signals.pre_delete, sender=models.List) @@ -126,9 +126,9 @@ def remove_list_on_delete(sender, instance, *args, **kwargs): remove_list_task.delay(instance.id) -def add_list_on_create_command(instance): +def add_list_on_create_command(instance_id): """runs this code only after the database commit completes""" - add_list_task.delay(instance.id) + add_list_task.delay(instance_id) @receiver(signals.post_save, sender=models.UserFollows) @@ -197,8 +197,12 @@ def populate_lists_on_account_create(sender, instance, created, *args, **kwargs) """build a user's feeds when they join""" if not created or not instance.local: return + transaction.on_commit(lambda: add_list_on_account_create_command(instance.id)) - populate_lists_task.delay(instance.id) + +def add_list_on_account_create_command(user_id): + """wait for the transaction to complete""" + populate_lists_task.delay(user_id) # ---- TASKS @@ -206,7 +210,7 @@ def populate_lists_on_account_create(sender, instance, created, *args, **kwargs) def populate_lists_task(user_id): """background task for populating an empty list stream""" user = models.User.objects.get(id=user_id) - ListsStream().populate_streams(user) + ListsStream().populate_lists(user) @app.task(queue=MEDIUM) diff --git a/bookwyrm/suggested_users.py b/bookwyrm/suggested_users.py index 86c181a2..a4b7ca65 100644 --- a/bookwyrm/suggested_users.py +++ b/bookwyrm/suggested_users.py @@ -2,6 +2,7 @@ import math import logging from django.dispatch import receiver +from django.db import transaction from django.db.models import signals, Count, Q from bookwyrm import models @@ -197,7 +198,7 @@ def update_user(sender, instance, created, update_fields=None, **kwargs): """an updated user, neat""" # a new user is found, create suggestions for them if created and instance.local: - rerank_suggestions_task.delay(instance.id) + transaction.on_commit(lambda: update_new_user_command(instance.id)) # we know what fields were updated and discoverability didn't change if not instance.bookwyrm_user or ( @@ -217,6 +218,11 @@ def update_user(sender, instance, created, update_fields=None, **kwargs): remove_user_task.delay(instance.id) +def update_new_user_command(instance_id): + """wait for transaction to complete""" + rerank_suggestions_task.delay(instance_id) + + @receiver(signals.post_save, sender=models.FederatedServer) def domain_level_update(sender, instance, created, update_fields=None, **kwargs): """remove users on a domain block"""