Skip to content

Commit

Permalink
fix(free-space): Continue next items after failure
Browse files Browse the repository at this point in the history
  • Loading branch information
rpatterson committed Aug 19, 2024
1 parent ea4d259 commit 0b02f61
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
53 changes: 34 additions & 19 deletions src/prunerr/downloadclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import re
import shutil
import urllib.parse
import bdb
import logging

import requests
Expand Down Expand Up @@ -228,6 +229,9 @@ def delete_files(self, item):
Delete all files and directories for the given path and stat or download item.
First remove from the download client if given a download item.
:param item: A `pathlib.Path()` filesystem path or a download item to be
deleted.
"""
# Handle actual items recognized by the download client
if isinstance(item, prunerr.downloaditem.PrunerrDownloadItem):
Expand All @@ -253,25 +257,10 @@ def delete_files(self, item):
# When freeing disk space it's important not to get hung up waiting for a
# heavily loaded client. Be very defensive and proceed directly to deleting
# the data:
try:
self.client.remove_torrent(
[item.hashString],
timeout=transmission_rpc.constants.DEFAULT_TIMEOUT,
)
except transmission_rpc.error.TransmissionTimeoutError: # pragma: no cover
logger.debug(
"Expected short timeout to promptly free space: %r",
item,
exc_info=True,
)
except (
Exception # pylint: disable=broad-exception-caught
): # pragma: no cover
logger.exception(
"Unexpected exception removing item, freeing space anyways: %r",
item,
)

self.client.remove_torrent(
[item.hashString],
timeout=transmission_rpc.constants.DEFAULT_TIMEOUT,
)
self.items.remove(item)
path = item.files_parent

Expand Down Expand Up @@ -318,6 +307,32 @@ def delete_files(self, item):

return size

def try_delete_files(self, item):
"""
Attempt to delete a path or a download item, but tolerate and log failures.
:param item: A `pathlib.Path()` filesystem path or a download item to be
deleted.
"""
try:
return self.delete_files(item)
except transmission_rpc.error.TransmissionTimeoutError: # pragma: no cover
logger.debug(
"Expected short timeout to promptly free space: %r",
item,
exc_info=True,
)
except (
Exception # pylint: disable=broad-exception-caught
) as exc_value: # pragma: no cover
if isinstance(exc_value, (KeyboardInterrupt, bdb.BdbQuit)):
raise
logger.exception(
"Unexpected exception removing item, freeing space anyways: %r",
item,
)
return 0 # pragma: no cover

def free_space_check(self):
"""
Determine if there's sufficient free disk space.
Expand Down
4 changes: 2 additions & 2 deletions src/prunerr/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def free_space(self) -> typing.Optional[dict]:
)
for orphan_download_clients, file_path, file_stat in self.find_orphans():
first_download_client = next(iter(orphan_download_clients.values()))
first_download_client.delete_files((file_path, file_stat))
first_download_client.try_delete_files((file_path, file_stat))
results.setdefault(
first_download_client.config["url"],
[],
Expand Down Expand Up @@ -477,7 +477,7 @@ def free_space_remove_items(
download_client,
download_client_method,
)():
removed_size = download_client.delete_files(download_item)
removed_size = download_client.try_delete_files(download_item)
results.setdefault(
download_client_url,
[],
Expand Down

0 comments on commit 0b02f61

Please sign in to comment.