replace prefix w/ canonical name list

This commit is contained in:
Jimmy Zelinskie 2016-12-07 12:55:22 -05:00
parent c41de8ded6
commit ebbe58d311
3 changed files with 10 additions and 7 deletions

View file

@ -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()

View file

@ -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()

View file

@ -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