Fixes account create tasks

This commit is contained in:
Mouse Reeve 2022-01-04 14:17:14 -08:00
parent 3dd7847d7b
commit 23e498879e
3 changed files with 24 additions and 8 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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"""