Add configurable limits for number of builds allowed under a namespace

We also support that limit being increased automatically once a successful billing charge has gone through
This commit is contained in:
Joseph Schorr 2018-02-20 13:58:14 -05:00
parent 62971b7f20
commit 9a452ace11
10 changed files with 125 additions and 33 deletions

View file

@ -439,6 +439,8 @@ class User(BaseModel):
company = CharField(null=True)
location = CharField(null=True)
maximum_queued_builds_count = IntegerField(null=True)
def delete_instance(self, recursive=False, delete_nullable=False):
# If we are deleting a robot account, only execute the subset of queries necessary.
if self.robot:

View file

@ -0,0 +1,26 @@
"""Add maximum build queue count setting to user table
Revision ID: 152bb29a1bb3
Revises: 7367229b38d9
Create Date: 2018-02-20 13:34:34.902415
"""
# revision identifiers, used by Alembic.
revision = '152bb29a1bb3'
down_revision = 'cbc8177760d9'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
def upgrade(tables):
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('user', sa.Column('maximum_queued_builds_count', sa.Integer(), nullable=True))
# ### end Alembic commands ###
def downgrade(tables):
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('user', 'maximum_queued_builds_count')
# ### end Alembic commands ###

View file

@ -80,7 +80,9 @@ def create_user_noverify(username, email, email_required=True, prompts=tuple()):
try:
default_expr_s = _convert_to_s(config.app_config['DEFAULT_TAG_EXPIRATION'])
new_user = User.create(username=username, email=email, removed_tag_expiration_s=default_expr_s)
default_max_builds = config.app_config.get('DEFAULT_NAMESPACE_MAXIMUM_BUILD_COUNT')
new_user = User.create(username=username, email=email, removed_tag_expiration_s=default_expr_s,
maximum_queued_builds_count=default_max_builds)
for prompt in prompts:
create_user_prompt(new_user, prompt)
@ -88,6 +90,14 @@ def create_user_noverify(username, email, email_required=True, prompts=tuple()):
except Exception as ex:
raise DataModelException(ex.message)
def increase_maximum_build_count(user, maximum_queued_builds_count):
""" Increases the maximum number of allowed builds on the namespace, if greater than that
already present.
"""
if (user.maximum_queued_builds_count is not None and
maximum_queued_builds_count > user.maximum_queued_builds_count):
user.maximum_queued_builds_count = maximum_queued_builds_count
user.save()
def is_username_unique(test_username):
try:

View file

@ -74,6 +74,19 @@ class WorkQueue(object):
._available_jobs(now, name_match_query)
.where(~(QueueItem.queue_name << running_query)))
def num_available_jobs(self, canonical_name_list):
"""
Returns the number of available queue items with a given prefix.
"""
def strip_slash(name):
return name.lstrip('/')
canonical_name_list = map(strip_slash, canonical_name_list)
available = self._available_jobs(datetime.utcnow(),
'/'.join([self._queue_name] + canonical_name_list) + '%')
return available.count()
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.