From 77ad52473a62d05472e0e4835f3520e3500f2c57 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 21 Sep 2024 12:45:00 -0600 Subject: [PATCH] Fixing upsert_file usage. Adding log details for error. --- modules/videos/downloader.py | 4 ++-- modules/videos/models.py | 3 +-- wrolpi/downloader.py | 11 +++++++---- wrolpi/files/downloader.py | 5 ++--- wrolpi/files/lib.py | 1 + 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/modules/videos/downloader.py b/modules/videos/downloader.py index c77b0eab..110e3ac7 100755 --- a/modules/videos/downloader.py +++ b/modules/videos/downloader.py @@ -137,11 +137,11 @@ class ChannelDownloader(Downloader, ABC): downloads=downloads, settings=settings, ) - except Exception: + except Exception as e: if PYTEST: raise kind = 'playlist' if is_a_playlist else 'channel' - logger.warning(f'Failed to update catalog of {kind} {download.url}') + logger.error(f'Failed to update catalog of {kind} {download.url}', exc_info=e) return DownloadResult( success=False, location=location, diff --git a/modules/videos/models.py b/modules/videos/models.py index 05c02200..21cffbd3 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -436,8 +436,7 @@ class Video(ModelHelper, Base): def delete_video(video_: Video): """Delete a Video with any of its tags.""" for tag_name_ in video_.file_group.tag_names: - tag_ = Tag.get_by_name(tag_name_) - video_.file_group.untag(tag_, session) + video_.file_group.untag(tag_name_, session) video_.delete() changes = False diff --git a/wrolpi/downloader.py b/wrolpi/downloader.py index 20f666fa..ee0d991a 100644 --- a/wrolpi/downloader.py +++ b/wrolpi/downloader.py @@ -245,8 +245,7 @@ class Download(ModelHelper, Base): # noqa def get_all_by_destination(cls, destination: str | pathlib.Path, session: Session = None) -> List['Download']: destination = str(destination) downloads = session.query(Download) \ - .filter(Download.settings.contains({'destination': destination})) \ - .all() + .filter(Download.settings.contains({'destination': destination})).all() # noqa return downloads @@ -595,7 +594,7 @@ class DownloadManager: ).order_by( Download.frequency.is_(None), Download.frequency, - Download.id)) # noqa + Download.id)) count = 0 for download in new_downloads: domain = download.domain @@ -727,7 +726,7 @@ class DownloadManager: 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_: + if download.next_download < now_ and download.status not in (DownloadStatus.new, DownloadStatus.pending): download.renew() renewed_count += 1 @@ -823,6 +822,7 @@ class DownloadManager: """Delete all once-downloads that have expired. Do not delete downloads that are new, or should be tried again.""" + count = 0 with get_db_session(commit=True) as session: downloads = self.get_once_downloads(session) one_month = now() - timedelta(days=30) @@ -830,6 +830,9 @@ class DownloadManager: if download.status in self.FINISHED_STATUSES and download.last_successful_download and \ download.last_successful_download < one_month: session.delete(download) + count += 1 + if count: + self.log_debug(f'Deleted {count} once downloads') def list_downloaders(self) -> List[Downloader]: """Return a list of the Downloaders available on this Download Manager.""" diff --git a/wrolpi/files/downloader.py b/wrolpi/files/downloader.py index 0e64ffd4..40d2fab6 100644 --- a/wrolpi/files/downloader.py +++ b/wrolpi/files/downloader.py @@ -52,9 +52,8 @@ class FileDownloader(Downloader, ABC): try: await download_file(download.url, output_path, info) - with get_db_session(commit=True) as session: - fg = await upsert_file(output_path, session=session, tag_names=download.settings.get('tag_names')) - location = fg.location + fg = await upsert_file(output_path, tag_names=download.settings.get('tag_names')) + location = fg.location return DownloadResult( success=True, diff --git a/wrolpi/files/lib.py b/wrolpi/files/lib.py index 2209b05d..271ee0c3 100644 --- a/wrolpi/files/lib.py +++ b/wrolpi/files/lib.py @@ -1570,4 +1570,5 @@ async def upsert_file(file: pathlib.Path | str, tag_names: List[str] = None) -> file_group.add_tag(tag.id) session.commit() + file_group.flush() return file_group