From 98d6262a7f988dbbf413157219de1a4c65c4e5d0 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Mon, 21 Sep 2015 12:21:39 -0400 Subject: [PATCH] close connections after getting queue metrics --- data/queue.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/data/queue.py b/data/queue.py index b787d22be..e99c98e68 100644 --- a/data/queue.py +++ b/data/queue.py @@ -1,6 +1,7 @@ from datetime import datetime, timedelta -from data.database import QueueItem, db, db_for_update +from app import app +from data.database import QueueItem, db, db_for_update, UseThenDisconnect from util.morecollections import AttrDict @@ -68,23 +69,24 @@ class WorkQueue(object): return db_for_update(QueueItem.select().where(QueueItem.id == queue_id)).get() def get_metrics(self, require_transaction=True): - guard = self._transaction_factory(db) if require_transaction else NoopWith() - with guard: - now = datetime.utcnow() - name_match_query = self._name_match_query() + with UseThenDisconnect(app.config): + guard = self._transaction_factory(db) if require_transaction else NoopWith() + with guard: + now = datetime.utcnow() + name_match_query = self._name_match_query() - running_query = self._running_jobs(now, name_match_query) - running_count = running_query.distinct().count() + running_query = self._running_jobs(now, name_match_query) + running_count = running_query.distinct().count() - available_query = self._available_jobs(now, name_match_query) - available_count = available_query.select(QueueItem.queue_name).distinct().count() + available_query = self._available_jobs(now, name_match_query) + available_count = available_query.select(QueueItem.queue_name).distinct().count() - available_not_running_query = self._available_jobs_not_running(now, name_match_query, - running_query) - available_not_running_count = (available_not_running_query.select(QueueItem.queue_name) - .distinct().count()) + available_not_running_query = self._available_jobs_not_running(now, name_match_query, + running_query) + available_not_running_count = (available_not_running_query.select(QueueItem.queue_name) + .distinct().count()) - return (running_count, available_not_running_count, available_count) + return (running_count, available_not_running_count, available_count) def update_metrics(self): if self._reporter is None and self._metric_queue is None: