From a25b8373c99ef2a977a44880a9d8f164fc0db4c6 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 15 Oct 2024 08:59:14 -0600 Subject: [PATCH] Retry button resets attempts. --- wrolpi/downloader.py | 8 ++++++-- wrolpi/root_api.py | 2 +- wrolpi/test/test_root_api.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/wrolpi/downloader.py b/wrolpi/downloader.py index 2ab79c60..3cb5c7d9 100644 --- a/wrolpi/downloader.py +++ b/wrolpi/downloader.py @@ -121,6 +121,7 @@ class Download(ModelHelper, Base): # noqa def __json__(self) -> dict: d = dict( + attempts=self.attempts, channel_id=self.channel_id, downloader=self.downloader, frequency=self.frequency, @@ -654,10 +655,13 @@ class DownloadManager: raise TimeoutError('Downloads never finished!') @staticmethod - def retry_downloads(): + def retry_downloads(reset_attempts: bool = False): """Set any incomplete Downloads to `new` so they will be retried.""" with get_db_curs(commit=True) as curs: - curs.execute("UPDATE download SET status='new' WHERE status='pending' OR status='deferred'") + if reset_attempts: + curs.execute("UPDATE download SET status='new', attempts=0 WHERE status='pending' OR status='deferred'") + else: + curs.execute("UPDATE download SET status='new' WHERE status='pending' OR status='deferred'") save_downloads_config.activate_switch() diff --git a/wrolpi/root_api.py b/wrolpi/root_api.py index afae85e6..ef860704 100644 --- a/wrolpi/root_api.py +++ b/wrolpi/root_api.py @@ -355,7 +355,7 @@ async def clear_failed(_: Request): @api_bp.post('/download/retry_once') @openapi.description('Retry failed once-downloads') async def retry_once(_: Request): - download_manager.retry_downloads() + download_manager.retry_downloads(reset_attempts=True) return response.empty() diff --git a/wrolpi/test/test_root_api.py b/wrolpi/test/test_root_api.py index 37097b73..ae9a3036 100644 --- a/wrolpi/test/test_root_api.py +++ b/wrolpi/test/test_root_api.py @@ -375,6 +375,34 @@ async def test_clear_downloads(test_session, async_client, test_wrolpi_config, t assert get_download_manager_config().skip_urls == ['https://example.com/5', ] +@pytest.mark.asyncio +async def test_retry_downloads(test_session, async_client, test_wrolpi_config, assert_downloads): + """Downloads that are pending or deferred can be retried, and their attempts reset.""" + d1 = Download(url='https://example.com/1', status='complete', attempts=1) + d2 = Download(url='https://example.com/2', status='pending', attempts=1) + d3 = Download(url='https://example.com/3', status='deferred', attempts=1) + d4 = Download(url='https://example.com/4', status='new', attempts=1) + d5 = Download(url='https://example.com/5', status='failed', attempts=1) + d6 = Download(url='https://example.com/6', status='failed', frequency=60, attempts=1) + d7 = Download(url='https://example.com/7', status='complete', frequency=60, attempts=1) + test_session.add_all([d1, d2, d3, d4, d5, d6, d7]) + test_session.commit() + + # Retry all once-downloads. + request, response = await async_client.post('/api/download/retry_once') + assert response.status_code == HTTPStatus.NO_CONTENT + + assert_downloads([ + dict(url='https://example.com/1', status='complete', attempts=1), # complete is not retried. + dict(url='https://example.com/2', status='new', attempts=0), # pending is retried. + dict(url='https://example.com/3', status='new', attempts=0), # deferred is retried. + dict(url='https://example.com/4', status='new', attempts=1), # new does not need to be retried. + dict(url='https://example.com/5', status='failed', attempts=1), # failed has been given up on. + dict(url='https://example.com/6', status='failed', frequency=60, attempts=1,), # recurring is always retried. + dict(url='https://example.com/7', status='complete', frequency=60, attempts=1), # recurring is always retried. + ]) + + @pytest.mark.asyncio async def test_get_status(async_client, test_session): """Get the server status information."""