diff --git a/wrolpi/downloader.py b/wrolpi/downloader.py index fb4f28eb..febcb834 100644 --- a/wrolpi/downloader.py +++ b/wrolpi/downloader.py @@ -714,26 +714,26 @@ class DownloadManager: downloads = query.all() return downloads - @optional_session - def renew_recurring_downloads(self, session: Session = None): + def renew_recurring_downloads(self): """Mark any recurring downloads that are due for download as "new". Start a download.""" now_ = now() - recurring = self.get_recurring_downloads(session) - renewed_count = 0 - for download in recurring: - # A new download may not have a `next_download`, create it if necessary. - download.next_download = download.next_download or self.calculate_next_download(download, session=session) - if download.next_download < now_ and download.status not in (DownloadStatus.new, DownloadStatus.pending): - download.renew() - renewed_count += 1 + with get_db_session() as session: + recurring = self.get_recurring_downloads(session) + renewed_count = 0 + for download in recurring: + # A new download may not have a `next_download`, create it if necessary. + download.next_download = download.next_download or self.calculate_next_download(download, session=session) + if download.next_download < now_ and download.status not in (DownloadStatus.new, DownloadStatus.pending): + download.renew() + renewed_count += 1 - if renewed_count: - self.log_debug(f'Renewed {renewed_count} recurring downloads') - session.commit() + if renewed_count: + self.log_debug(f'Renewed {renewed_count} recurring downloads') + session.commit() - # Save the config now that some Downloads renewed. - save_downloads_config.activate_switch() + # Save the config now that some Downloads renewed. + save_downloads_config.activate_switch() @staticmethod def get_downloads(session: Session) -> List[Download]: diff --git a/wrolpi/test/test_downloader.py b/wrolpi/test/test_downloader.py index 3cc58581..8cb63f8f 100644 --- a/wrolpi/test/test_downloader.py +++ b/wrolpi/test/test_downloader.py @@ -180,14 +180,14 @@ async def test_recurring_downloads(test_session, test_download_manager, fake_now assert download.last_successful_download == now_ # Download is not due for an hour. - test_download_manager.renew_recurring_downloads(test_session) + test_download_manager.renew_recurring_downloads() await test_download_manager.wait_for_all_downloads() assert list(test_download_manager.get_new_downloads(test_session)) == [] assert download.last_successful_download == now_ # Download is due an hour later. fake_now(datetime(2020, 1, 1, 2, 0, 1, tzinfo=pytz.UTC)) - test_download_manager.renew_recurring_downloads(test_session) + test_download_manager.renew_recurring_downloads() (download,) = list(test_download_manager.get_new_downloads(test_session)) # Download is "new" but has not been downloaded a second time. assert download.is_new, download.status_code @@ -211,7 +211,7 @@ async def test_recurring_downloads(test_session, test_download_manager, fake_now test_downloader.do_download.reset_mock() now_ = fake_now(datetime(2020, 1, 1, 4, 0, 1, tzinfo=pytz.UTC)) test_downloader.set_test_success() - test_download_manager.renew_recurring_downloads(test_session) + test_download_manager.renew_recurring_downloads() await test_download_manager.wait_for_all_downloads() test_downloader.do_download.assert_called_once() download = test_session.query(Download).one()