feat(games): add steam pages with steam price/reduc
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.tasks import task
|
||||
from django.utils import timezone
|
||||
|
||||
from .services.steam_store import refresh_steam_games
|
||||
|
||||
# Set while a periodic refresh is queued, so there's only ever one loop running,
|
||||
# whatever the number of workers/containers trying to start it. It expires on
|
||||
# its own if the loop dies (worker killed mid-job, Redis queue flushed, ...),
|
||||
# letting the next schedule_steam_refresh() start a new one.
|
||||
STEAM_REFRESH_GUARD_KEY = "games:steam_refresh:scheduled"
|
||||
|
||||
|
||||
def steam_refresh_interval() -> timedelta:
|
||||
return timedelta(minutes=settings.STEAM_STORE_REFRESH_MINUTES)
|
||||
|
||||
|
||||
def _guard_timeout() -> int:
|
||||
return int(steam_refresh_interval().total_seconds() * 2)
|
||||
|
||||
|
||||
def schedule_steam_refresh() -> bool:
|
||||
"""Start the periodic refresh loop right away, unless one is already queued.
|
||||
Safe to call from anywhere, any number of times."""
|
||||
if not cache.add(STEAM_REFRESH_GUARD_KEY, 1, timeout=_guard_timeout()):
|
||||
return False
|
||||
|
||||
refresh_steam_games_task.enqueue()
|
||||
return True
|
||||
|
||||
|
||||
@task
|
||||
def refresh_steam_games_task(
|
||||
game_ids: list[int] | None = None, reschedule: bool = True
|
||||
):
|
||||
"""Refresh Steam store info, then re-enqueue itself every
|
||||
STEAM_STORE_REFRESH_MINUTES (same self-rescheduling idea as
|
||||
sync_event_game_scopes_task, no external scheduler needed).
|
||||
|
||||
reschedule=False is a one-off run (e.g. the admin action) that leaves the
|
||||
periodic loop alone."""
|
||||
try:
|
||||
refresh_steam_games(game_ids)
|
||||
finally:
|
||||
if reschedule:
|
||||
cache.set(STEAM_REFRESH_GUARD_KEY, 1, timeout=_guard_timeout())
|
||||
refresh_steam_games_task.using(
|
||||
run_after=timezone.now() + steam_refresh_interval()
|
||||
).enqueue()
|
||||
Reference in New Issue
Block a user