From 804d8bcefd65875aa171c554352d4be0fbe79473 Mon Sep 17 00:00:00 2001 From: petitminion Date: Sat, 11 Jun 2022 06:34:11 +0000 Subject: [PATCH] Add task to refresh actor data in the cache (#1392) --- api/config/settings/common.py | 5 +++++ api/funkwhale_api/federation/tasks.py | 28 +++++++++++++++++++++++++++ changes/changelog.d/1392.enhancement | 1 + 3 files changed, 34 insertions(+) create mode 100644 changes/changelog.d/1392.enhancement diff --git a/api/config/settings/common.py b/api/config/settings/common.py index 82393fc5d..33fbceda2 100644 --- a/api/config/settings/common.py +++ b/api/config/settings/common.py @@ -841,6 +841,11 @@ CELERY_BEAT_SCHEDULE = { "schedule": crontab(day_of_week="1", minute="0", hour="2"), "options": {"expires": 60 * 60 * 24}, }, + "federation.refresh_actor_data": { + "task": "federation.refresh_actor_data", + "schedule": crontab(minute="0", hour="*/5"), + "options": {"expires": 60 * 60}, + }, } if env.bool("ADD_ALBUM_TAGS_FROM_TRACKS", default=True): diff --git a/api/funkwhale_api/federation/tasks.py b/api/funkwhale_api/federation/tasks.py index c040082aa..0d5832340 100644 --- a/api/funkwhale_api/federation/tasks.py +++ b/api/funkwhale_api/federation/tasks.py @@ -3,6 +3,7 @@ import json import logging import os import requests +from requests import HTTPError from django.conf import settings from django.db import transaction @@ -17,6 +18,7 @@ from funkwhale_api.common import preferences from funkwhale_api.common import models as common_models from funkwhale_api.common import session from funkwhale_api.common import utils as common_utils +from funkwhale_api.federation import actors as actors_utils from funkwhale_api.moderation import mrf from funkwhale_api.music import models as music_models from funkwhale_api.taskapp import celery @@ -625,3 +627,29 @@ def fetch_collection(url, max_pages, channel, is_page=False): results["errored"], ) return results + + +@celery.app.task(name="federation.refresh_actor_data") +def refresh_actor_data(): + actors = models.Actor.objects.all().prefetch_related() + for actor in actors: + try: + data = actors_utils.get_actor_data(actor.fid) + except HTTPError as e: + logger.info( + f"Actor couldn't be fetch because of the following exeption : {e!r}" + ) + if e.response.status_code == 410: + logger.info("Purging actor : {actor.fid!r}") + purge_actors([actor.id], [actor.domain]) + continue + continue + except Exception as e: + logger.info( + f"Actor couldn't be fetch because of the following exeption : {e!r}" + ) + continue + serializer = serializers.ActorSerializer(data=data) + serializer.is_valid(raise_exception=True) + serializer.save(last_fetch_date=timezone.now()) + return diff --git a/changes/changelog.d/1392.enhancement b/changes/changelog.d/1392.enhancement new file mode 100644 index 000000000..4b7d9be44 --- /dev/null +++ b/changes/changelog.d/1392.enhancement @@ -0,0 +1 @@ +Add task to refresh actor data in the cache (#1392)