Show max values in /status api

This commit is contained in:
Stéphane Bidoul 2021-10-28 17:07:42 +02:00
parent 28c109de6e
commit 8f7aa98224
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92
3 changed files with 22 additions and 7 deletions

View file

@ -14,8 +14,11 @@ router = APIRouter()
class Status(BaseModel):
deployed: int
max_deployed: int
running: int
max_running: int
starting: int
max_starting: int
class Config:
orm_mode = True

View file

@ -39,14 +39,26 @@ class Controller:
def running(self) -> int:
return self.db.count_by_statuses([BuildStatus.started, BuildStatus.starting])
@property
def max_running(self) -> int:
return settings.max_running
@property
def starting(self) -> int:
return self.db.count_by_statuses([BuildStatus.starting])
@property
def max_starting(self) -> int:
return settings.max_starting
@property
def deployed(self) -> int:
return self.db.count_all()
@property
def max_deployed(self) -> int:
return settings.max_deployed
def _wakeup(self) -> None:
self._wakeup_event.set()
self._wakeup_event.clear()
@ -71,8 +83,8 @@ class Controller:
await self._wakeup_event.wait()
while True:
can_start = max(
settings.max_running - self.running,
settings.max_starting - self.starting,
self.max_running - self.running,
self.max_starting - self.starting,
)
if can_start <= 0:
break # no capacity for now, back to sleep
@ -89,7 +101,7 @@ class Controller:
while True:
await self._wakeup_event.wait()
while True:
can_stop = self.running - settings.max_running
can_stop = self.running - self.max_running
if can_stop <= 0:
break # nothing to top for now, back to sleep
to_stop = self.db.oldest_started(limit=can_stop)
@ -105,7 +117,7 @@ class Controller:
while True:
await self._wakeup_event.wait()
while True:
can_undeploy = self.deployed - settings.max_deployed
can_undeploy = self.deployed - self.max_deployed
if can_undeploy <= 0:
break # nothing to undeploy for now, back to sleep
to_undeploy = self.db.oldest_stopped(limit=can_undeploy)

View file

@ -5,9 +5,9 @@ class Settings(BaseSettings):
admin_user: str
admin_passwd: str
supported_repos: set[str]
max_starting: int = 4
max_running: int = 10
max_deployed: int = 20
max_starting: int = 2
max_running: int = 4
max_deployed: int = 10
build_namespace: str
build_pghost: str
build_pgport: str