Switch build queue limiter query to use total number of alive jobs

This is slightly more accurate and, not being based on time, will work better under MySQL
This commit is contained in:
Joseph Schorr 2018-02-21 14:04:40 -05:00
parent d77aa9228f
commit e446eb5757
2 changed files with 10 additions and 8 deletions

View file

@ -74,18 +74,20 @@ class WorkQueue(object):
._available_jobs(now, name_match_query) ._available_jobs(now, name_match_query)
.where(~(QueueItem.queue_name << running_query))) .where(~(QueueItem.queue_name << running_query)))
def num_available_jobs(self, canonical_name_list): def num_alive_jobs(self, canonical_name_list):
""" """
Returns the number of available queue items with a given prefix. Returns the number of alive queue items with a given prefix.
""" """
def strip_slash(name): def strip_slash(name):
return name.lstrip('/') return name.lstrip('/')
canonical_name_list = map(strip_slash, canonical_name_list) canonical_name_list = map(strip_slash, canonical_name_list)
canonical_name_query = '/'.join([self._queue_name] + canonical_name_list) + '%'
available = self._available_jobs(datetime.utcnow(), return (QueueItem
'/'.join([self._queue_name] + canonical_name_list) + '%') .select()
.where(QueueItem.queue_name ** canonical_name_query)
return available.count() .where(QueueItem.retries_remaining > 0)
.count())
def num_available_jobs_between(self, available_min_time, available_max_time, canonical_name_list): def num_available_jobs_between(self, available_min_time, available_max_time, canonical_name_list):
""" """

View file

@ -31,8 +31,8 @@ def start_build(repository, prepared_build, pull_robot_name=None):
if repository.namespace_user.maximum_queued_builds_count is not None: if repository.namespace_user.maximum_queued_builds_count is not None:
queue_item_canonical_name = [repository.namespace_user.username] queue_item_canonical_name = [repository.namespace_user.username]
available_builds = dockerfile_build_queue.num_available_jobs(queue_item_canonical_name) alive_builds = dockerfile_build_queue.num_alive_jobs(queue_item_canonical_name)
if available_builds >= repository.namespace_user.maximum_queued_builds_count: if alive_builds >= repository.namespace_user.maximum_queued_builds_count:
logger.debug('Prevented queueing of build under namespace %s due to reaching max: %s', logger.debug('Prevented queueing of build under namespace %s due to reaching max: %s',
repository.namespace_user.username, repository.namespace_user.username,
repository.namespace_user.maximum_queued_builds_count) repository.namespace_user.maximum_queued_builds_count)