From 4ca9ad1e5b16d4045978be46b8d1881aeca40260 Mon Sep 17 00:00:00 2001 From: Samuel Cattini-Schultz Date: Mon, 15 Apr 2024 21:18:38 +1000 Subject: [PATCH] Split lb score cache task and implement dispatcher --- leaderboards/tasks.py | 21 ++++++++++++++------- osuchan/settings.py | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/leaderboards/tasks.py b/leaderboards/tasks.py index 6183756..aac1271 100644 --- a/leaderboards/tasks.py +++ b/leaderboards/tasks.py @@ -115,18 +115,25 @@ def send_notification( @shared_task -def update_global_leaderboard_top_5_score_cache(): +def dispatch_update_global_leaderboard_top_5_score_cache(): for gamemode in Gamemode: leaderboards = Leaderboard.objects.filter( access_type=LeaderboardAccessType.GLOBAL, gamemode=gamemode ) for leaderboard in leaderboards: - scores = leaderboard.get_top_scores(limit=5) - cache.set( - f"leaderboards::global_leaderboard_top_5_scores::{leaderboard.id}", - scores, - 1800, - ) + update_global_leaderboard_top_5_score_cache.delay(leaderboard.id) + + +@shared_task +def update_global_leaderboard_top_5_score_cache(leaderboard_id: int): + leaderboard = Leaderboard.objects.get(id=leaderboard_id) + scores = leaderboard.get_top_scores(limit=5) + cache.set( + f"leaderboards::global_leaderboard_top_5_scores::{leaderboard.id}", + scores, + 1800, + ) + return scores @shared_task diff --git a/osuchan/settings.py b/osuchan/settings.py index 1914ab7..f6fdfc2 100644 --- a/osuchan/settings.py +++ b/osuchan/settings.py @@ -201,7 +201,7 @@ class EnvSettings(BaseSettings): "schedule": crontab(minute=0, hour=0), # midnight UTC }, "update-global-leaderboard-top-5-score-cache-every-20-minutes": { - "task": "leaderboards.tasks.update_global_leaderboard_top_5_score_cache", + "task": "leaderboards.tasks.dispatch_update_global_leaderboard_top_5_score_cache", "schedule": crontab(minute="*/20"), }, }