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 56 additions and 24 deletions

View File

@ -467,6 +467,8 @@ class ConsumerPlugin(
) )
# now that everything is done, we can start to store the document # now that everything is done, we can start to store the document
# in the system. This will be a transaction and reasonably fast. # in the system. This will be a transaction and reasonably fast.
success = False
store_exception = None
try: try:
with transaction.atomic(): with transaction.atomic():
# store the document. # store the document.
@ -531,7 +533,12 @@ class ConsumerPlugin(
# renaming logic to acquire the lock as well. # renaming logic to acquire the lock as well.
# This triggers things like file renaming # This triggers things like file renaming
document.save() document.save()
success = True
except Exception as fail_exc:
store_exception = fail_exc
finally:
if success:
# Delete the file only if it was successfully consumed # Delete the file only if it was successfully consumed
self.log.debug(f"Deleting file {self.working_copy}") self.log.debug(f"Deleting file {self.working_copy}")
self.input_doc.original_file.unlink() self.input_doc.original_file.unlink()
@ -549,34 +556,40 @@ class ConsumerPlugin(
self.log.debug(f"Deleting file {shadow_file}") self.log.debug(f"Deleting file {shadow_file}")
Path(shadow_file).unlink() Path(shadow_file).unlink()
except Exception as e: self.run_post_consume_script(document)
self._fail(
str(e),
f"The following error occurred while storing document "
f"{self.filename} after parsing: {e}",
exc_info=True,
exception=e,
)
finally:
document_parser.cleanup()
tempdir.cleanup()
self.run_post_consume_script(document) self.log.info(f"Document {document} consumption finished")
self.log.info(f"Document {document} consumption finished") self._send_progress(
100,
100,
ProgressStatusOptions.SUCCESS,
ConsumerStatusShortMessage.FINISHED,
document.id,
)
self._send_progress( # Return the most up to date fields
100, document.refresh_from_db()
100,
ProgressStatusOptions.SUCCESS,
ConsumerStatusShortMessage.FINISHED,
document.id,
)
# Return the most up to date fields document_parser.cleanup()
document.refresh_from_db() tempdir.cleanup()
return f"Success. New document id {document.pk} created"
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}.",
)
def _parse_title_placeholders(self, title: str) -> str: def _parse_title_placeholders(self, title: str) -> str:
local_added = timezone.localtime(timezone.now()) local_added = timezone.localtime(timezone.now())

View File

@ -633,6 +633,25 @@ class TestConsumer(
# Database empty # Database empty
self.assertEqual(Document.objects.all().count(), 0) self.assertEqual(Document.objects.all().count(), 0)
@mock.patch("documents.consumer.ConsumerPlugin._store")
@mock.patch("documents.consumer.document_consumption_finished.send")
@mock.patch("documents.consumer.generate_unique_filename")
def testSaveFailsStillCaught(self, m_filename, m_signal, m_store):
filename = self.get_test_file()
m_store.return_value = None
m_filename.side_effect = AttributeError("BOOM")
with self.get_consumer(filename) as consumer:
with self.assertRaisesMessage(
ConsumerError,
"sample.pdf: The following error occurred while storing document sample.pdf after parsing: BOOM",
):
consumer.run()
self._assert_first_last_send_progress(last_status="FAILED")
self.assertIsFile(filename)
self.assertEqual(Document.objects.count(), 0)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}") @override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def testFilenameHandling(self): def testFilenameHandling(self):
with self.get_consumer( with self.get_consumer(