Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 12 hour cooldown for daily top lb user updates #62

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions osuchan/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import os
from datetime import timedelta

from celery.schedules import crontab
from pydantic_settings import BaseSettings
Expand Down Expand Up @@ -203,6 +204,10 @@ class EnvSettings(BaseSettings):
"update-top-global-members-every-day": {
"task": "profiles.tasks.dispatch_update_all_global_leaderboard_top_members",
"schedule": crontab(minute="0", hour="0"), # midnight UTC
"kwargs": {
"limit": 100,
"cooldown_seconds": timedelta(hours=12).total_seconds(),
},
},
"update-global-leaderboard-top-5-score-cache-every-hour": {
"task": "leaderboards.tasks.dispatch_update_global_leaderboard_top_5_score_cache",
Expand Down
8 changes: 6 additions & 2 deletions profiles/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def fetch_user(user_id=None, username=None, gamemode=Gamemode.STANDARD):

@transaction.atomic
def refresh_user_from_api(
user_id=None, username=None, gamemode: Gamemode = Gamemode.STANDARD
user_id=None,
username=None,
gamemode: Gamemode = Gamemode.STANDARD,
cooldown_seconds: int = 300,
):
"""
Fetch and add user with top 100 scores
Expand All @@ -65,7 +68,8 @@ def refresh_user_from_api(
user_stats = fetch_user(user_id=user_id, username=username, gamemode=gamemode)

if user_stats is not None and user_stats.last_updated > (
datetime.utcnow().replace(tzinfo=timezone.utc) - timedelta(minutes=5)
datetime.utcnow().replace(tzinfo=timezone.utc)
- timedelta(seconds=cooldown_seconds)
):
# User was last updated less than 5 minutes ago, so just return it
return user_stats
Expand Down
14 changes: 10 additions & 4 deletions profiles/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@


@shared_task
def dispatch_update_all_global_leaderboard_top_members(limit: int = 100):
def dispatch_update_all_global_leaderboard_top_members(
limit: int = 100, cooldown_seconds: int = 500
):
"""
Dispatches update_user tasks for the top members of all global leaderboards
"""
for leaderboard in Leaderboard.global_leaderboards.all():
members = leaderboard.memberships.order_by("-pp")[:limit].values("user_id")

for member in members:
update_user.delay(member["user_id"], leaderboard.gamemode)
update_user.delay(member["user_id"], leaderboard.gamemode, cooldown_seconds)


@shared_task
Expand All @@ -39,11 +41,15 @@ def dispatch_update_community_leaderboard_members(


@shared_task
def update_user(user_id: int, gamemode: int = Gamemode.STANDARD):
def update_user(
user_id: int, gamemode: int = Gamemode.STANDARD, cooldown_seconds: int = 500
):
"""
Runs an update for a given user
"""
user_stats = refresh_user_from_api(user_id=user_id, gamemode=Gamemode(gamemode))
user_stats = refresh_user_from_api(
user_id=user_id, gamemode=Gamemode(gamemode), cooldown_seconds=cooldown_seconds
)
if user_stats is not None:
update_memberships.delay(
user_id=user_stats.user_id, gamemode=user_stats.gamemode
Expand Down