fix(clock): dedup tasks lock bug removal
This commit is contained in:
@@ -40,7 +40,7 @@ class GameAdmin(admin.ModelAdmin):
|
||||
game_ids = list(
|
||||
queryset.filter(steam_app_id__isnull=False).values_list("id", flat=True)
|
||||
)
|
||||
refresh_steam_games_task.enqueue(game_ids=game_ids, reschedule=False)
|
||||
refresh_steam_games_task.enqueue(game_ids=game_ids)
|
||||
self.message_user(
|
||||
request, f"Queued Steam store refresh for {len(game_ids)} game(s)."
|
||||
)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from datetime import timedelta
|
||||
from uuid import uuid4
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
@@ -10,7 +11,9 @@ 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.
|
||||
# letting the next schedule_steam_refresh() start a new one. The guard holds the
|
||||
# loop id, so if it expires while the old loop is just late, the old one stops
|
||||
# at its next run (instead of 2 loops running side by side).
|
||||
STEAM_REFRESH_GUARD_KEY = "games:steam_refresh:scheduled"
|
||||
|
||||
|
||||
@@ -25,28 +28,34 @@ def _guard_timeout() -> int:
|
||||
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()):
|
||||
loop_id = uuid4().hex
|
||||
if not cache.add(STEAM_REFRESH_GUARD_KEY, loop_id, timeout=_guard_timeout()):
|
||||
return False
|
||||
|
||||
refresh_steam_games_task.enqueue()
|
||||
refresh_steam_games_task.enqueue(loop_id=loop_id)
|
||||
return True
|
||||
|
||||
|
||||
@task
|
||||
def refresh_steam_games_task(
|
||||
game_ids: list[int] | None = None, reschedule: bool = True
|
||||
game_ids: list[int] | None = None, loop_id: str | None = None
|
||||
):
|
||||
"""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."""
|
||||
Without loop_id it's a one-off run (e.g. the admin action) that leaves the
|
||||
periodic loop alone. With loop_id, it only continues while the guard still
|
||||
holds this loop_id."""
|
||||
if loop_id is not None and cache.get(STEAM_REFRESH_GUARD_KEY) != loop_id:
|
||||
# Another loop owns the guard (or it expired), stop this one
|
||||
return
|
||||
|
||||
try:
|
||||
refresh_steam_games(game_ids)
|
||||
finally:
|
||||
if reschedule:
|
||||
cache.set(STEAM_REFRESH_GUARD_KEY, 1, timeout=_guard_timeout())
|
||||
if loop_id is not None:
|
||||
cache.set(STEAM_REFRESH_GUARD_KEY, loop_id, timeout=_guard_timeout())
|
||||
refresh_steam_games_task.using(
|
||||
run_after=timezone.now() + steam_refresh_interval()
|
||||
).enqueue()
|
||||
).enqueue(loop_id=loop_id)
|
||||
|
||||
@@ -9,7 +9,11 @@ from events.models import Event
|
||||
|
||||
from .models import EventGame, Game
|
||||
from .services.steam_store import refresh_steam_games
|
||||
from .tasks import refresh_steam_games_task, schedule_steam_refresh
|
||||
from .tasks import (
|
||||
STEAM_REFRESH_GUARD_KEY,
|
||||
refresh_steam_games_task,
|
||||
schedule_steam_refresh,
|
||||
)
|
||||
|
||||
LOCMEM_CACHE = {"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}}
|
||||
|
||||
@@ -83,21 +87,32 @@ class ScheduleSteamRefreshTests(TestCase):
|
||||
def test_only_one_loop_is_scheduled(self, task):
|
||||
self.assertTrue(schedule_steam_refresh())
|
||||
self.assertFalse(schedule_steam_refresh())
|
||||
task.enqueue.assert_called_once_with()
|
||||
task.enqueue.assert_called_once_with(loop_id=cache.get(STEAM_REFRESH_GUARD_KEY))
|
||||
|
||||
@mock.patch("games.tasks.refresh_steam_games")
|
||||
def test_task_reschedules_itself_even_on_failure(self, refresh):
|
||||
cache.set(STEAM_REFRESH_GUARD_KEY, "loop")
|
||||
refresh.side_effect = RuntimeError("boom")
|
||||
with mock.patch("games.tasks.refresh_steam_games_task") as patched:
|
||||
with self.assertRaises(RuntimeError):
|
||||
refresh_steam_games_task.call()
|
||||
refresh_steam_games_task.call(loop_id="loop")
|
||||
|
||||
patched.using.return_value.enqueue.assert_called_once_with()
|
||||
patched.using.return_value.enqueue.assert_called_once_with(loop_id="loop")
|
||||
|
||||
@mock.patch("games.tasks.refresh_steam_games")
|
||||
def test_superseded_loop_stops(self, refresh):
|
||||
cache.set(STEAM_REFRESH_GUARD_KEY, "new-loop")
|
||||
with mock.patch("games.tasks.refresh_steam_games_task") as patched:
|
||||
refresh_steam_games_task.call(loop_id="old-loop")
|
||||
|
||||
refresh.assert_not_called()
|
||||
patched.using.assert_not_called()
|
||||
self.assertEqual(cache.get(STEAM_REFRESH_GUARD_KEY), "new-loop")
|
||||
|
||||
@mock.patch("games.tasks.refresh_steam_games")
|
||||
def test_one_off_run_does_not_reschedule(self, refresh):
|
||||
with mock.patch("games.tasks.refresh_steam_games_task") as patched:
|
||||
refresh_steam_games_task.call(game_ids=[1], reschedule=False)
|
||||
refresh_steam_games_task.call(game_ids=[1])
|
||||
|
||||
refresh.assert_called_once_with([1])
|
||||
patched.using.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user