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:
Ilan
2015-07-27 12:31:38 +02:00
parent 82bd93d67f
commit ba58b4face
+7 -1
View File
@@ -31,6 +31,7 @@ except ImportError:
# Django
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django import db
# Local
import signing
@@ -328,6 +329,7 @@ def monitor(result_queue):
"""
name = current_process().name
logger.info(_("{} monitoring at {}").format(name, current_process().pid))
db.close_old_connections()
for task in iter(result_queue.get, 'STOP'):
save_task(task)
if task['success']:
@@ -346,6 +348,7 @@ def worker(task_queue, result_queue, timer, timeout=Conf.TIMEOUT):
"""
name = current_process().name
logger.info(_('{} ready for work at {}').format(name, current_process().pid))
db.close_old_connections()
task_count = 0
# Start reading the task queue
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']:
return
# SAVE LIMIT > 0: Prune database, SAVE_LIMIT 0: No pruning
try:
if task['success'] and 0 < Conf.SAVE_LIMIT < Success.objects.count():
Success.objects.last().delete()
try:
Task.objects.create(id=task['id'],
name=task['name'],
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
"""
try:
for s in Schedule.objects.exclude(repeats=0).filter(next_run__lt=timezone.now()):
args = ()
kwargs = {}
@@ -477,6 +481,8 @@ def scheduler(list_key=Conf.Q_LIST):
s.repeats = 0
# save the schedule
s.save()
except Exception as e:
logger.error(e)
def set_cpu_affinity(n, process_ids, actual=not Conf.TESTING):