Compare commits

..

5 Commits

Author SHA1 Message Date
shamoon
056b314a89
Update consumer.py 2025-06-26 14:49:08 -07:00
shamoon
ba91bb154a
Update consumer.py 2025-06-26 14:41:39 -07:00
shamoon
3c4225979e
Update consumer.py 2025-06-26 14:40:29 -07:00
shamoon
84b5ef7c34
Update consumer.py 2025-06-26 14:34:44 -07:00
shamoon
d579caf1ea
Enhancement: better try to catch db errors before unlink 2025-06-26 14:24:45 -07:00
2 changed files with 22 additions and 32 deletions

View File

@ -468,7 +468,7 @@ class ConsumerPlugin(
# now that everything is done, we can start to store the document
# in the system. This will be a transaction and reasonably fast.
success = False
result = None
store_exception = None
try:
with transaction.atomic():
# store the document.
@ -535,18 +535,8 @@ class ConsumerPlugin(
document.save()
success = True
except Exception as e:
# save the exception for later
try:
self._fail(
str(e),
f"The following error occurred while storing document "
f"{self.filename} after parsing: {e}",
exc_info=True,
exception=e,
)
except Exception as fail_exc:
stored_exception = fail_exc
store_exception = fail_exc
finally:
if success:
# Delete the file only if it was successfully consumed
@ -581,19 +571,26 @@ class ConsumerPlugin(
# Return the most up to date fields
document.refresh_from_db()
result = f"Success. New document id {document.pk} created"
elif stored_exception:
raise stored_exception
document_parser.cleanup()
tempdir.cleanup()
return f"Success. New document id {document.pk} created"
else:
document_parser.cleanup()
tempdir.cleanup()
if store_exception:
self._fail(
str(store_exception),
f"The following error occurred while storing document "
f"{self.filename} after parsing: {store_exception}",
exc_info=True,
exception=store_exception,
)
else:
self._fail(
ConsumerStatusShortMessage.FAILED,
f"Error occurred while saving {self.filename}.",
)
document_parser.cleanup()
tempdir.cleanup()
return result
def _parse_title_placeholders(self, title: str) -> str:
local_added = timezone.localtime(timezone.now())

View File

@ -636,16 +636,9 @@ class TestConsumer(
@mock.patch("documents.consumer.ConsumerPlugin._store")
@mock.patch("documents.consumer.document_consumption_finished.send")
@mock.patch("documents.consumer.generate_unique_filename")
def test_post_consume_fails_silently(self, m_filename, m_signal, m_store):
"""
If _store() returns None but no exception is raised, _fail should still be called.
"""
def testSaveFailsStillCaught(self, m_filename, m_signal, m_store):
filename = self.get_test_file()
# Make _store() return None
m_store.return_value = None
# Cause crash in a predictable, testable place
m_filename.side_effect = AttributeError("BOOM")
with self.get_consumer(filename) as consumer: