From 63381c1d77a2b22c2e88e94e8327776fb0d485d1 Mon Sep 17 00:00:00 2001 From: Roland Date: Wed, 8 May 2024 17:02:27 -0600 Subject: [PATCH] Created download retry button. --- app/src/api.js | 14 ++++++++++ app/src/components/Common.js | 4 --- app/src/components/admin/Downloads.js | 38 +++++++++++---------------- main.py | 2 +- wrolpi/downloader.py | 4 ++- wrolpi/root_api.py | 7 +++++ wrolpi/test/test_flags.py | 6 ++--- wrolpi/test/test_root_api.py | 12 +++++++++ 8 files changed, 56 insertions(+), 31 deletions(-) diff --git a/app/src/api.js b/app/src/api.js index 98b0e428..2282bdfe 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -786,6 +786,20 @@ export async function deleteOnceDownloads() { } } +export async function retryOnceDownloads() { + let response = await apiPost(`${API_URI}/download/retry_once`); + if (response.status === 204) { + return null + } else { + toast({ + type: 'error', + title: 'Error!', + description: 'Could not retry once downloads! See server logs.', + time: 5000, + }); + } +} + export async function getStatistics() { let response = await apiGet(`${API_URI}/statistics`); if (response.status === 200) { diff --git a/app/src/components/Common.js b/app/src/components/Common.js index 536ad970..15779498 100644 --- a/app/src/components/Common.js +++ b/app/src/components/Common.js @@ -1537,10 +1537,6 @@ export function useAPIButton( } - button = <> - {button} - - return {button, ref} } diff --git a/app/src/components/admin/Downloads.js b/app/src/components/admin/Downloads.js index aba36f36..9df32aa3 100644 --- a/app/src/components/admin/Downloads.js +++ b/app/src/components/admin/Downloads.js @@ -1,11 +1,11 @@ import React from "react"; import { clearCompletedDownloads, - clearFailedDownloads, deleteDownload, deleteOnceDownloads, killDownload, - restartDownload + restartDownload, + retryOnceDownloads } from "../../api"; import {Link} from "react-router-dom"; import { @@ -33,7 +33,7 @@ import {Button, Header, Modal, ModalActions, ModalContent, ModalHeader, Placehol import {useDownloads} from "../../hooks/customHooks"; import {toast} from "react-semantic-toasts-2"; -function ClearCompleteDownloads({callback}) { +function ClearDownloadsButton({callback}) { async function localClearDownloads() { try { await clearCompletedDownloads(); @@ -49,14 +49,14 @@ function ClearCompleteDownloads({callback}) { onClick={localClearDownloads} color='violet' obeyWROLMode={true} - >Clear Completed + >Clear } -function ClearFailedDownloads({callback}) { - async function localDeleteFailed() { +function RetryDownloadsButton({callback}) { + async function localRetryOnce() { try { - await clearFailedDownloads(); + await retryOnceDownloads(); } finally { if (callback) { callback() @@ -65,17 +65,13 @@ function ClearFailedDownloads({callback}) { } return - Clear Failed - + >Retry } -function DeleteOnceDownloads({callback}) { +function DeleteOnceDownloadsButton({callback}) { async function localDeleteOnce() { try { await deleteOnceDownloads(); @@ -89,12 +85,10 @@ function DeleteOnceDownloads({callback}) { return - Delete All - + >Delete } class RecurringDownloadRow extends React.Component { @@ -333,9 +327,9 @@ export function OnceDownloadsTable({downloads, fetchDownloads}) { - - - + + + diff --git a/main.py b/main.py index 8880e8e2..96788a43 100755 --- a/main.py +++ b/main.py @@ -237,7 +237,7 @@ async def start_single_tasks(app: Sanic): if flags.refresh_complete.is_set(): # Set all downloads to new. - download_manager.reset_downloads() + download_manager.retry_downloads() # Hotspot/throttle are not supported in Docker containers. if not DOCKERIZED and get_wrolpi_config().hotspot_on_startup: diff --git a/wrolpi/downloader.py b/wrolpi/downloader.py index 38ea90d8..c8671aed 100644 --- a/wrolpi/downloader.py +++ b/wrolpi/downloader.py @@ -593,11 +593,13 @@ class DownloadManager: raise TimeoutError('Downloads never finished!') @staticmethod - def reset_downloads(): + def retry_downloads(): """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'") + api_app.add_task(save_downloads_config()) + DOWNLOAD_SORT = ( DownloadStatus.pending, DownloadStatus.failed, diff --git a/wrolpi/root_api.py b/wrolpi/root_api.py index acd30417..ad650a16 100644 --- a/wrolpi/root_api.py +++ b/wrolpi/root_api.py @@ -329,6 +329,13 @@ async def clear_failed(_: Request): return response.empty() +@api_bp.post('/download/retry_once') +@openapi.description('Retry failed once-downloads') +async def retry_once(_: Request): + download_manager.retry_downloads() + return response.empty() + + @api_bp.post('/download/delete_once') @openapi.description('Delete all once downloads') async def delete_once(_: Request): diff --git a/wrolpi/test/test_flags.py b/wrolpi/test/test_flags.py index 1931caaf..dc576d40 100644 --- a/wrolpi/test/test_flags.py +++ b/wrolpi/test/test_flags.py @@ -14,7 +14,7 @@ def assert_db(session: Session, name: str, expected: bool): assert getattr(f, name) is expected -def test_flags(test_session, flags_lock): +def test_flags(test_async_client, test_session, flags_lock): """DB flags can be saved.""" assert flags.refresh_complete.is_set() is False assert flags.get_flags() == [] @@ -28,7 +28,7 @@ def test_flags(test_session, flags_lock): assert flags.get_flags() == [] -def test_flags_with(flags_lock): +def test_flags_with(test_async_client, flags_lock): """A Flag can be used with `with` context.""" assert flags.refreshing.is_set() is False, 'Flag should not be set by default' assert flags.get_flags() == [] @@ -48,7 +48,7 @@ def test_flags_with(flags_lock): @pytest.mark.asyncio -async def test_flag_wait_for(flags_lock): +async def test_flag_wait_for(test_async_client, flags_lock): """Wait for throws an error when waiting exceeds timeout.""" with pytest.raises(TimeoutError): async with flags.refreshing.wait_for(timeout=1): diff --git a/wrolpi/test/test_root_api.py b/wrolpi/test/test_root_api.py index fbd382a3..4e23378b 100644 --- a/wrolpi/test/test_root_api.py +++ b/wrolpi/test/test_root_api.py @@ -348,6 +348,17 @@ async def test_clear_downloads(test_session, test_async_client, test_config, tes # Failed once-downloads will not be downloaded again. assert get_download_manager_config().skip_urls == ['https://example.com/5', ] + # Downloads can be retried. + request, response = await test_async_client.post('/api/download/retry_once') + assert response.status_code == HTTPStatus.NO_CONTENT + request, response = await test_async_client.get('/api/download') + once_downloads = [ + {'url': 'https://example.com/2', 'status': 'new'}, + {'url': 'https://example.com/3', 'status': 'new'}, + {'url': 'https://example.com/4', 'status': 'new'}, + ] + check_downloads(response, once_downloads, recurring_downloads, status_code=HTTPStatus.OK) + # "Delete All" button deletes all once-downloads. request, response = await test_async_client.post('/api/download/delete_once') assert response.status_code == HTTPStatus.NO_CONTENT @@ -592,6 +603,7 @@ async def test_search_file_estimates(test_session, test_async_client, archive_fa @pytest.mark.asyncio async def test_external_error(test_async_client): """A non-SanicException error is handled as an Internal Server Error.""" + @test_async_client.sanic_app.get('/error') async def error(_: Request): raise RuntimeError('Oh no!')