Enhancement: better try to catch db errors before unlink

This commit is contained in:
shamoon 2025-06-23 09:19:28 -07:00
parent 37267f3f04
commit 0f6e9d4343
No known key found for this signature in database

View File

@ -530,24 +530,31 @@ class ConsumerPlugin(
# Don't save with the lock active. Saving will cause the file
# renaming logic to acquire the lock as well.
# This triggers things like file renaming
document.save()
try:
document.save()
success = True
finally:
if success:
# Delete the file only if it was successfully consumed
self.log.debug(f"Deleting file {self.working_copy}")
self.input_doc.original_file.unlink()
self.working_copy.unlink()
if self.unmodified_original is not None: # pragma: no cover
self.unmodified_original.unlink()
# Delete the file only if it was successfully consumed
self.log.debug(f"Deleting file {self.working_copy}")
self.input_doc.original_file.unlink()
self.working_copy.unlink()
if self.unmodified_original is not None: # pragma: no cover
self.unmodified_original.unlink()
# https://github.com/jonaswinkler/paperless-ng/discussions/1037
shadow_file = (
Path(self.input_doc.original_file).parent
/ f"._{Path(self.input_doc.original_file).name}"
)
# https://github.com/jonaswinkler/paperless-ng/discussions/1037
shadow_file = (
Path(self.input_doc.original_file).parent
/ f"._{Path(self.input_doc.original_file).name}"
)
if Path(shadow_file).is_file():
self.log.debug(f"Deleting file {shadow_file}")
Path(shadow_file).unlink()
if Path(shadow_file).is_file():
self.log.debug(f"Deleting file {shadow_file}")
Path(shadow_file).unlink()
else:
self.log.warning(
f"Document was not saved. Skipping file deletion for {self.filename}",
)
except Exception as e:
self._fail(