Skip to content

Commit

Permalink
Split lb score cache task and implement dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed Apr 15, 2024
1 parent fc8a711 commit 4ca9ad1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions leaderboards/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion osuchan/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
}
Expand Down

0 comments on commit 4ca9ad1

Please sign in to comment.