Getting session inside renew_recurring_downloads method to avoid rare error.

This commit is contained in:
Roland 2024-10-20 15:37:51 -06:00
parent 8909d28a6b
commit 055a359f34
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 18 additions and 18 deletions

View file

@ -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]:

View file

@ -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()