From ebbe58d3114c8bfb6847e494b8d81128093302cd Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Wed, 7 Dec 2016 12:55:22 -0500 Subject: [PATCH] replace prefix w/ canonical name list --- data/queue.py | 9 ++++++--- endpoints/building.py | 4 ++-- test/test_queue.py | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/data/queue.py b/data/queue.py index 07135b746..2f9321c92 100644 --- a/data/queue.py +++ b/data/queue.py @@ -77,13 +77,16 @@ class WorkQueue(object): ._available_jobs(now, name_match_query) .where(~(QueueItem.queue_name << running_query))) - def num_available_jobs_between(self, available_min_time, available_max_time, prefix): + def num_available_jobs_between(self, available_min_time, available_max_time, canonical_name_list): """ Returns the number of available queue items with a given prefix, between the two provided times. """ - prefix = prefix.lstrip('/') + def strip_slash(name): + return name.lstrip('/') + canonical_name_list = map(strip_slash, canonical_name_list) + available = self._available_jobs(available_max_time, - '/'.join([self._queue_name, prefix]) + '%') + '/'.join([self._queue_name] + canonical_name_list) + '%') return available.where(QueueItem.available_after >= available_min_time).count() diff --git a/endpoints/building.py b/endpoints/building.py index 83fd9e028..9ad61f8a1 100644 --- a/endpoints/building.py +++ b/endpoints/building.py @@ -29,13 +29,13 @@ class MaximumBuildsQueuedException(Exception): def start_build(repository, prepared_build, pull_robot_name=None): - queue_item_prefix = '%s/%s' % (repository.namespace_user.username, repository.name) if MAX_BUILD_QUEUE_RATE_ITEMS > 0 and MAX_BUILD_QUEUE_RATE_SECS > 0: + queue_item_canonical_name = [repository.namespace_user.username, repository.name] now = datetime.utcnow() available_min = now - timedelta(seconds=MAX_BUILD_QUEUE_RATE_SECS) available_builds = dockerfile_build_queue.num_available_jobs_between(available_min, now, - queue_item_prefix) + queue_item_canonical_name) if available_builds >= MAX_BUILD_QUEUE_RATE_ITEMS: raise MaximumBuildsQueuedException() diff --git a/test/test_queue.py b/test/test_queue.py index 0ac6cb633..4fd320964 100644 --- a/test/test_queue.py +++ b/test/test_queue.py @@ -189,11 +189,11 @@ class TestQueue(QueueTestCase): self.queue.put(['abc', 'ghi'], self.TEST_MESSAGE_2, available_after=-5) # Partial results - count = self.queue.num_available_jobs_between(now-timedelta(seconds=8), now, 'abc') + count = self.queue.num_available_jobs_between(now-timedelta(seconds=8), now, ['abc']) self.assertEqual(1, count) # All results - count = self.queue.num_available_jobs_between(now-timedelta(seconds=20), now, 'abc') + count = self.queue.num_available_jobs_between(now-timedelta(seconds=20), now, ['/abc']) self.assertEqual(2, count) # No results