mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-10 03:50:55 +08:00
closes db connection on worker and monitor spawn
This should prevent some problems with Postgresql where the workers would use stale db connections and cause errors.
This commit is contained in:
+7
-1
@@ -31,6 +31,7 @@ except ImportError:
|
|||||||
# Django
|
# Django
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from django import db
|
||||||
|
|
||||||
# Local
|
# Local
|
||||||
import signing
|
import signing
|
||||||
@@ -328,6 +329,7 @@ def monitor(result_queue):
|
|||||||
"""
|
"""
|
||||||
name = current_process().name
|
name = current_process().name
|
||||||
logger.info(_("{} monitoring at {}").format(name, current_process().pid))
|
logger.info(_("{} monitoring at {}").format(name, current_process().pid))
|
||||||
|
db.close_old_connections()
|
||||||
for task in iter(result_queue.get, 'STOP'):
|
for task in iter(result_queue.get, 'STOP'):
|
||||||
save_task(task)
|
save_task(task)
|
||||||
if task['success']:
|
if task['success']:
|
||||||
@@ -346,6 +348,7 @@ def worker(task_queue, result_queue, timer, timeout=Conf.TIMEOUT):
|
|||||||
"""
|
"""
|
||||||
name = current_process().name
|
name = current_process().name
|
||||||
logger.info(_('{} ready for work at {}').format(name, current_process().pid))
|
logger.info(_('{} ready for work at {}').format(name, current_process().pid))
|
||||||
|
db.close_old_connections()
|
||||||
task_count = 0
|
task_count = 0
|
||||||
# Start reading the task queue
|
# Start reading the task queue
|
||||||
for pack in iter(task_queue.get, 'STOP'):
|
for pack in iter(task_queue.get, 'STOP'):
|
||||||
@@ -399,9 +402,9 @@ def save_task(task):
|
|||||||
if not task.get('save', Conf.SAVE_LIMIT > 0) and task['success']:
|
if not task.get('save', Conf.SAVE_LIMIT > 0) and task['success']:
|
||||||
return
|
return
|
||||||
# SAVE LIMIT > 0: Prune database, SAVE_LIMIT 0: No pruning
|
# SAVE LIMIT > 0: Prune database, SAVE_LIMIT 0: No pruning
|
||||||
|
try:
|
||||||
if task['success'] and 0 < Conf.SAVE_LIMIT < Success.objects.count():
|
if task['success'] and 0 < Conf.SAVE_LIMIT < Success.objects.count():
|
||||||
Success.objects.last().delete()
|
Success.objects.last().delete()
|
||||||
try:
|
|
||||||
Task.objects.create(id=task['id'],
|
Task.objects.create(id=task['id'],
|
||||||
name=task['name'],
|
name=task['name'],
|
||||||
func=task['func'],
|
func=task['func'],
|
||||||
@@ -421,6 +424,7 @@ def scheduler(list_key=Conf.Q_LIST):
|
|||||||
"""
|
"""
|
||||||
Creates a task from a schedule at the scheduled time and schedules next run
|
Creates a task from a schedule at the scheduled time and schedules next run
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
for s in Schedule.objects.exclude(repeats=0).filter(next_run__lt=timezone.now()):
|
for s in Schedule.objects.exclude(repeats=0).filter(next_run__lt=timezone.now()):
|
||||||
args = ()
|
args = ()
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
@@ -477,6 +481,8 @@ def scheduler(list_key=Conf.Q_LIST):
|
|||||||
s.repeats = 0
|
s.repeats = 0
|
||||||
# save the schedule
|
# save the schedule
|
||||||
s.save()
|
s.save()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
|
||||||
|
|
||||||
def set_cpu_affinity(n, process_ids, actual=not Conf.TESTING):
|
def set_cpu_affinity(n, process_ids, actual=not Conf.TESTING):
|
||||||
|
|||||||
Reference in New Issue
Block a user