Skip to content

Commit

Permalink
Proper Django DB handling when run using the process_queue mgtm. command
Browse files Browse the repository at this point in the history
  • Loading branch information
Schweigi committed Jan 17, 2019
1 parent d3928f7 commit cad568f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 16 additions & 0 deletions eb_sqs/worker/commons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import absolute_import, unicode_literals

from contextlib import contextmanager

from django.db import reset_queries, close_old_connections


@contextmanager
def django_db_management():
# type: () -> None
reset_queries()
close_old_connections()
try:
yield
finally:
close_old_connections()
10 changes: 7 additions & 3 deletions eb_sqs/worker/service.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import absolute_import, unicode_literals

import boto3
import logging
from datetime import timedelta

import boto3
from botocore.config import Config
from botocore.exceptions import ClientError
from datetime import timedelta
from django.utils import timezone

from eb_sqs import settings
from eb_sqs.worker.commons import django_db_management
from eb_sqs.worker.worker import Worker
from eb_sqs.worker.worker_exceptions import ExecutionFailedException
from eb_sqs.worker.worker_factory import WorkerFactory
Expand Down Expand Up @@ -116,7 +118,9 @@ def process_message(self, msg, worker):
receive_count, msg.message_id, msg.body
))

worker.execute(msg.body)
with django_db_management():
worker.execute(msg.body)

logger.debug('[django-eb-sqs] Processed message {}'.format(msg.message_id))
except ExecutionFailedException as exc:
logger.warning('[django-eb-sqs] Handling message {} got error: {}'.format(msg.message_id, repr(exc)))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='django-eb-sqs',
version='1.12',
version='1.13',
package_dir={'eb_sqs': 'eb_sqs'},
include_package_data=True,
packages=find_packages(),
Expand Down

0 comments on commit cad568f

Please sign in to comment.