Skip to content

Commit 985e3b2

Browse files
committed
- Minor bugfixes
1 parent d12c7f9 commit 985e3b2

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

django-queue-manager/django_queue_manager/worker.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Based on the Worker class from Python in a nutshell, by Alex Martelli
2+
import django
23
import logging
34
import threading
45
import queue as Queue
@@ -7,9 +8,11 @@
78
import time
89
from django_queue_manager.task_manager import TaskManager
910
from django.conf import settings
11+
1012
SAVE_SUCCESS_TASKS = getattr(settings, "SAVE_SUCCESS_TASKS", True)
1113

1214
from django_queue_manager.utilities.loggers import get_default_logger
15+
1316
logger = get_default_logger(__name__)
1417

1518

@@ -74,9 +77,15 @@ def run(self):
7477
# The code above will run indefinitely except when a thread _stopevent.isSet(), it will try to empty
7578
# the queue and move the task into success/failed tasks in base of successful or not execution.
7679
self.logger.info('Worker Thread Starts')
80+
django.setup()
7781
while not self._stopevent.isSet():
82+
if not self.worker_queue.empty():
7883
try:
7984
task = self.worker_queue.get()
85+
if task is None:
86+
break
87+
88+
django.db.connection.close()
8089

8190
self.logger.info('Consuming Task Id: {db_id}'.format(
8291
name=task.task_function_name,
@@ -89,9 +98,6 @@ def run(self):
8998
self.logger.info(
9099
'Task Id {db_id} success!'.format(name=task.task_function_name,
91100
db_id=task.db_id))
92-
except Queue.Empty:
93-
continue
94-
95101
except Exception as e:
96102
# Save it on the failed table
97103
TaskManager.save_task_failed(task, e)
@@ -108,11 +114,7 @@ def run(self):
108114
# In any case, it will dequeue the task form the queued tasks
109115
self.dequeue_task(task=task)
110116

111-
self.worker_queue.task_done()
112-
113-
# Close the connection, in order to prevent (2006, 'MySQL server has gone away') timeout error
114-
from django.db import connection
115-
connection.close()
117+
django.db.connection.close()
116118

117119
self.worker_queue = None
118120
self.logger.warning('Worker Thread stopped, {0} tasks handled'.format(self.tasks_counter))

django-queue-manager/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name='django-queue-manager',
12-
version='1.3.4',
12+
version='1.3.5',
1313
packages=find_packages(),
1414
include_package_data=True,
1515
license='GNU GPLv3',

0 commit comments

Comments
 (0)