Skip to content

Commit

Permalink
Add scheduled task to refresh lb score cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed Apr 14, 2024
1 parent 61130d9 commit 7210768
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
24 changes: 21 additions & 3 deletions leaderboards/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from celery import shared_task
from django.conf import settings
from django.core.cache import cache
from django.db import transaction

from common.discord_webhook_sender import DiscordWebhookSender
Expand Down Expand Up @@ -51,9 +52,11 @@ def update_memberships(user_id, gamemode=Gamemode.STANDARD):
)
elif leaderboard.score_set == ScoreSet.NEVER_CHOKE:
membership.pp = calculate_pp_total(
score.nochoke_performance_total
if score.result & ScoreResult.CHOKE
else score.performance_total
(
score.nochoke_performance_total
if score.result & ScoreResult.CHOKE
else score.performance_total
)
for score in scores
)
elif leaderboard.score_set == ScoreSet.ALWAYS_FULL_COMBO:
Expand Down Expand Up @@ -111,6 +114,21 @@ def send_notification(
return memberships


@shared_task
def 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,
900,
)


@shared_task
def send_leaderboard_top_score_notification(leaderboard_id: int, score_id: int):
# passing score_id instead of querying for top score in case it changes before the job is picked up
Expand Down
4 changes: 4 additions & 0 deletions osuchan/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ class EnvSettings(BaseSettings):
"task": "profiles.tasks.dispatch_update_all_global_leaderboard_top_members",
"schedule": crontab(minute=0, hour=0), # midnight UTC
},
"update-global-leaderboard-top-5-score-cache-every-10-minutes": {
"task": "leaderboards.tasks.update_global_leaderboard_top_5_score_cache",
"schedule": crontab(minute="*/10"),
},
}


Expand Down

0 comments on commit 7210768

Please sign in to comment.