mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-07 02:18:12 +08:00
#76 adds connection checked with timeout
Adds a check for old connections in both the workers and the monitor , every DB_TIMEOUT seconds
This commit is contained in:
@@ -319,10 +319,17 @@ def monitor(result_queue, broker=None):
|
||||
name = current_process().name
|
||||
logger.info(_("{} monitoring at {}").format(name, current_process().pid))
|
||||
db.close_old_connections()
|
||||
connection_timer = timezone.now()
|
||||
for task in iter(result_queue.get, 'STOP'):
|
||||
# check db connection timeout
|
||||
if (timezone.now() - connection_timer).total_seconds() >= Conf.DB_TIMEOUT:
|
||||
db.close_old_connections()
|
||||
connection_timer = timezone.now()
|
||||
# acknowledge
|
||||
ack_id = task.pop('ack_id', False)
|
||||
if ack_id:
|
||||
broker.acknowledge(ack_id)
|
||||
# save the result
|
||||
save_task(task)
|
||||
if task['success']:
|
||||
logger.info(_("Processed [{}]").format(task['name']))
|
||||
@@ -341,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()
|
||||
connection_timer = timezone.now()
|
||||
task_count = 0
|
||||
# Start reading the task queue
|
||||
for task in iter(task_queue.get, 'STOP'):
|
||||
@@ -360,6 +368,10 @@ def worker(task_queue, result_queue, timer, timeout=Conf.TIMEOUT):
|
||||
result = (e, False)
|
||||
# We're still going
|
||||
if not result:
|
||||
# check db connection timeout
|
||||
if (timezone.now() - connection_timer).total_seconds() >= Conf.DB_TIMEOUT:
|
||||
db.close_old_connections()
|
||||
connection_timer = timezone.now()
|
||||
# execute the payload
|
||||
timer.value = task['kwargs'].pop('timeout', timeout or 0) # Busy
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user