Fixing upsert_file usage. Adding log details for error.

This commit is contained in:
Roland 2024-09-21 12:45:00 -06:00
parent eb017d21e7
commit 77ad52473a
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
5 changed files with 13 additions and 11 deletions

View file

@ -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,

View file

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

View file

@ -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."""

View file

@ -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,

View file

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