Ensures all old connections are closed in certain longer running locations (#4265)

This commit is contained in:
Trenton H 2023-09-25 12:52:22 -07:00 committed by GitHub
parent 70b566f746
commit 612e6341a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from time import monotonic
from time import sleep
from typing import Final
from django import db
from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.management.base import CommandError
@ -38,6 +39,7 @@ def _tags_from_path(filepath) -> set[Tag]:
Returns set of Tag models
"""
db.close_old_connections()
tag_ids = set()
path_parts = Path(filepath).relative_to(settings.CONSUMPTION_DIR).parent.parts
for part in path_parts:

View File

@ -14,6 +14,7 @@ from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.db import DatabaseError
from django.db import close_old_connections
from django.db import models
from django.db.models import Q
from django.dispatch import receiver
@ -529,6 +530,8 @@ def before_task_publish_handler(sender=None, headers=None, body=None, **kwargs):
return
try:
close_old_connections()
task_args = body[0]
input_doc, _ = task_args
@ -560,6 +563,7 @@ def task_prerun_handler(sender=None, task_id=None, task=None, **kwargs):
https://docs.celeryq.dev/en/stable/userguide/signals.html#task-prerun
"""
try:
close_old_connections()
task_instance = PaperlessTask.objects.filter(task_id=task_id).first()
if task_instance is not None:
@ -587,6 +591,7 @@ def task_postrun_handler(
https://docs.celeryq.dev/en/stable/userguide/signals.html#task-postrun
"""
try:
close_old_connections()
task_instance = PaperlessTask.objects.filter(task_id=task_id).first()
if task_instance is not None:
@ -615,6 +620,7 @@ def task_failure_handler(
https://docs.celeryq.dev/en/stable/userguide/signals.html#task-failure
"""
try:
close_old_connections()
task_instance = PaperlessTask.objects.filter(task_id=task_id).first()
if task_instance is not None and task_instance.result is None: