diff --git a/bookwyrm/activitystreams.py b/bookwyrm/activitystreams.py index 88a68a5a4..35c8757b4 100644 --- a/bookwyrm/activitystreams.py +++ b/bookwyrm/activitystreams.py @@ -4,6 +4,7 @@ from django.db.models import signals, Q from bookwyrm import models from bookwyrm.redis_store import RedisStore, r +from bookwyrm.tasks import app from bookwyrm.views.helpers import privacy_filter @@ -374,3 +375,11 @@ def remove_statuses_on_shelve(sender, instance, *args, **kwargs): return BooksStream().remove_book_statuses(instance.user, instance.book) + + +@app.task +def populate_stream_task(stream, user_id): + """background task for populating an empty activitystream""" + user = models.User.objects.get(id=user_id) + stream = streams[stream] + stream.populate_streams(user) diff --git a/bookwyrm/management/commands/populate_streams.py b/bookwyrm/management/commands/populate_streams.py index 37559f082..a04d7f5ad 100644 --- a/bookwyrm/management/commands/populate_streams.py +++ b/bookwyrm/management/commands/populate_streams.py @@ -3,22 +3,35 @@ from django.core.management.base import BaseCommand from bookwyrm import activitystreams, models -def populate_streams(): +def populate_streams(stream=None): """build all the streams for all the users""" + streams = [stream] if stream else activitystreams.streams.keys() + print("Populations streams", streams) users = models.User.objects.filter( local=True, is_active=True, - ).order_by("last_active_date") + ).order_by("-last_active_date") + print("This may take a long time! Please be patient.") for user in users: - for stream in activitystreams.streams.values(): - stream.populate_streams(user) + for stream_key in streams: + print(".", end="") + activitystreams.populate_stream_task.delay(stream_key, user.id) class Command(BaseCommand): """start all over with user streams""" help = "Populate streams for all users" + + def add_arguments(self, parser): + parser.add_argument( + "--stream", + default=None, + help="Specifies which time of stream to populate", + ) + # pylint: disable=no-self-use,unused-argument def handle(self, *args, **options): """run feed builder""" - populate_streams() + stream = options.get("stream") + populate_streams(stream=stream) diff --git a/bw-dev b/bw-dev index e5e46e5f3..a2f04bfb6 100755 --- a/bw-dev +++ b/bw-dev @@ -121,7 +121,7 @@ case "$CMD" in makeitblack ;; populate_streams) - runweb python manage.py populate_streams + runweb python manage.py populate_streams $@ ;; populate_suggestions) runweb python manage.py populate_suggestions @@ -169,7 +169,7 @@ case "$CMD" in echo " build" echo " clean" echo " black" - echo " populate_streams" + echo " populate_streams [--stream=]" echo " populate_suggestions" echo " generate_preview_images [--all]" echo " copy_media_to_s3" diff --git a/celerywyrm/celery.py b/celerywyrm/celery.py index 3ab338621..de5e56304 100644 --- a/celerywyrm/celery.py +++ b/celerywyrm/celery.py @@ -20,6 +20,7 @@ app.config_from_object("django.conf:settings", namespace="CELERY") # Load task modules from all registered Django app configs. app.autodiscover_tasks() app.autodiscover_tasks(["bookwyrm"], related_name="activitypub.base_activity") +app.autodiscover_tasks(["bookwyrm"], related_name="activitystreams") app.autodiscover_tasks(["bookwyrm"], related_name="broadcast") app.autodiscover_tasks(["bookwyrm"], related_name="connectors.abstract_connector") app.autodiscover_tasks(["bookwyrm"], related_name="emailing")