diff --git a/modules/map/test/test_winds.py b/modules/map/test/test_winds.py index 7902cad0..627d7cd3 100644 --- a/modules/map/test/test_winds.py +++ b/modules/map/test/test_winds.py @@ -1,4 +1,5 @@ import json +from datetime import datetime, timedelta from http import HTTPStatus from unittest import mock @@ -320,6 +321,21 @@ async def test_weather_subscription(async_client, test_session, test_directory, assert response.status_code == HTTPStatus.BAD_REQUEST +@pytest.mark.parametrize('finished,expected', [ + ('2026-11-27T00:07:30+00:00', '2026-11-28T00:23:00+00:00'), + ('2026-11-27T14:00:11+00:00', '2026-11-28T14:23:00+00:00'), + ('2026-11-27T14:59:59+00:00', '2026-11-28T14:23:00+00:00'), +]) +def test_weather_downloader_avoids_the_hour(finished, expected): + """The next day's download is at WEATHER_MINUTE past an hour, when the forecast service is not + busy with requests made on the hour.""" + with mock.patch('modules.map.winds.now', lambda: datetime.fromisoformat(finished)): + result = winds.weather_downloader.finalize_download(None, None, winds.ExecutedWeather(points=1)) + assert result.success is True + assert datetime.fromisoformat(finished) + timedelta(seconds=result.retry_seconds) \ + == datetime.fromisoformat(expected) + + @pytest.mark.asyncio async def test_weather_downloader(test_session, test_directory, test_nuclear_config): """The downloader fetches a field around the saved targets, telling how far it has got.""" diff --git a/modules/map/winds.py b/modules/map/winds.py index ea822a2e..3d2fa914 100644 --- a/modules/map/winds.py +++ b/modules/map/winds.py @@ -20,6 +20,7 @@ a whole country takes a few minutes. import asyncio import json from dataclasses import dataclass +from datetime import timedelta import time import math from pathlib import Path @@ -299,6 +300,9 @@ _summary_cache = dict() # The weather subscription: one recurring Download of a field around the saved targets. WEATHER_URL = OPEN_METEO_URL WEATHER_DOWNLOADER = 'open_meteo' +# The minute past the hour the daily download is made: the forecast service is busy with requests +# made on the hour, sometimes too busy to answer a connection for minutes. +WEATHER_MINUTE = 23 def target_places() -> List[Tuple[float, float]]: @@ -403,7 +407,12 @@ class WeatherDownloader(Downloader): def finalize_download(self, session: Session, download: Download, executed: ExecutedWeather) -> DownloadResult: if executed.error: return DownloadResult(success=False, error=executed.error) - return DownloadResult(success=True, location='/map/manage') + # The next day's download is at WEATHER_MINUTE past the hour this one finished in, rather than in + # a daily slot (which are on the hour). + finished = now() + tomorrow = (finished + timedelta(days=1)).replace(minute=WEATHER_MINUTE, second=0, microsecond=0) + return DownloadResult(success=True, location='/map/manage', + retry_seconds=int((tomorrow - finished).total_seconds())) weather_downloader = WeatherDownloader()