Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Fix: avoid unnecessary close_old_connections in Celery task dispatch (#12701)

Trenton H5010f37174fb
diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py
index 65ce3c785..a557b9de9 100644
--- a/src/documents/bulk_edit.py
+++ b/src/documents/bulk_edit.py
@@ -237,44 +237,40 @@ def modify_tags(
expanded_remove_tags.add(int(t.id))
expanded_remove_tags.update(int(pk) for pk in t.get_descendants_pks())
- try:
- with transaction.atomic():
- if expanded_remove_tags:
+ with transaction.atomic():
+ if expanded_remove_tags:
+ DocumentTagRelationship.objects.filter(
+ document_id__in=affected_docs,
+ tag_id__in=expanded_remove_tags,
+ ).delete()
+
+ to_create = []
+ if expanded_add_tags:
+ existing_pairs = set(
DocumentTagRelationship.objects.filter(
document_id__in=affected_docs,
- tag_id__in=expanded_remove_tags,
- ).delete()
-
- to_create = []
- if expanded_add_tags:
- existing_pairs = set(
- DocumentTagRelationship.objects.filter(
- document_id__in=affected_docs,
- tag_id__in=expanded_add_tags,
- ).values_list("document_id", "tag_id"),
- )
-
- to_create = [
- DocumentTagRelationship(document_id=doc, tag_id=tag)
- for doc in affected_docs
- for tag in expanded_add_tags
- if (doc, tag) not in existing_pairs
- ]
-
- if to_create:
- DocumentTagRelationship.objects.bulk_create(
- to_create,
- ignore_conflicts=True,
- )
+ tag_id__in=expanded_add_tags,
+ ).values_list("document_id", "tag_id"),
+ )
- if affected_docs:
- bulk_update_documents.apply_async(
- kwargs={"document_ids": affected_docs},
- headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM},
+ to_create = [
+ DocumentTagRelationship(document_id=doc, tag_id=tag)
+ for doc in affected_docs
+ for tag in expanded_add_tags
+ if (doc, tag) not in existing_pairs
+ ]
+
+ if to_create:
+ DocumentTagRelationship.objects.bulk_create(
+ to_create,
+ ignore_conflicts=True,
)
- except Exception as e:
- logger.error(f"Error modifying tags: {e}")
- return "ERROR"
+
+ if affected_docs:
+ bulk_update_documents.apply_async(
+ kwargs={"document_ids": affected_docs},
+ headers={"trigger_source": PaperlessTask.TriggerSource.SYSTEM},
+ )
return "OK"
diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py
index 5b481b4ef..22b28ac98 100644
--- a/src/documents/signals/handlers.py
+++ b/src/documents/signals/handlers.py
@@ -1122,7 +1122,6 @@ def before_task_publish_handler(
return
try:
- close_old_connections()
_, task_kwargs, _ = body
task_id = headers["id"]