Skip to content

Commit

Permalink
added proper django handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkrafka committed Aug 11, 2021
1 parent 1e8eb18 commit a64c608
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions process/management/commands/api_loader.py
Expand Up @@ -58,3 +58,4 @@ def process(self, connection, channel, delivery_tag, body):

# confirm message processing
self._ack(connection, channel, delivery_tag)
self._clean_thread_resources()
8 changes: 8 additions & 0 deletions process/management/commands/base/worker.py
Expand Up @@ -7,6 +7,7 @@
import pika
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import connection as django_db_connection
from django.utils.translation import gettext as t

from process.models import Collection, CollectionFile, CollectionNote, ProcessingStep
Expand Down Expand Up @@ -156,6 +157,13 @@ def _publish_async_callback(self, channel, message, routing_key):
)
)

def _clean_thread_resources(self):
"""
Cleans thread resources, which are not cleaned by default and automatically
i.e. django db connections.
"""
django_db_connection.close()

def _createStep(self, step_type=None, collection_id=None, collection_file_id=None, ocid=None):
"""Creates processing step"""
processing_step = ProcessingStep()
Expand Down
1 change: 1 addition & 0 deletions process/management/commands/checker.py
Expand Up @@ -68,3 +68,4 @@ def process(self, connection, channel, delivery_tag, body):
self._exception("Failed saving collection note")

self._ack(connection, channel, delivery_tag)
self._clean_thread_resources()
1 change: 1 addition & 0 deletions process/management/commands/compiler.py
Expand Up @@ -83,6 +83,7 @@ def process(self, connection, channel, delivery_tag, body):
)
except Exception:
self._exception("Failed saving collection note")
self._clean_thread_resources()

def _publish_releases(self, connection, channel, collection):
try:
Expand Down
2 changes: 2 additions & 0 deletions process/management/commands/file_worker.py
Expand Up @@ -74,3 +74,5 @@ def process(self, connection, channel, delivery_tag, body):

# confirm message processing
self._ack(connection, channel, delivery_tag)

self._clean_thread_resources()
2 changes: 2 additions & 0 deletions process/management/commands/finisher.py
Expand Up @@ -60,3 +60,5 @@ def process(self, connection, channel, delivery_tag, body):
self._exception("Failed saving collection note")

self._ack(connection, channel, delivery_tag)

self._clean_thread_resources()
1 change: 1 addition & 0 deletions process/management/commands/record_compiler.py
Expand Up @@ -68,3 +68,4 @@ def process(self, connection, channel, delivery_tag, body):
self._exception("Failed saving collection note")

self._ack(connection, channel, delivery_tag)
self._clean_thread_resources()
1 change: 1 addition & 0 deletions process/management/commands/release_compiler.py
Expand Up @@ -64,3 +64,4 @@ def process(self, connection, channel, delivery_tag, body):
self._exception("Failed saving collection note")

self._ack(connection, channel, delivery_tag)
self._clean_thread_resources()

0 comments on commit a64c608

Please sign in to comment.