Fix loop in starter/stopper/undeployer

This commit is contained in:
Stéphane Bidoul 2021-10-28 15:31:24 +02:00
parent e33ad90745
commit 28c109de6e
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92

View file

@ -75,9 +75,15 @@ class Controller:
settings.max_starting - self.starting, settings.max_starting - self.starting,
) )
if can_start <= 0: if can_start <= 0:
break # no capacity for now, back to sleep
to_start = self.db.to_start(limit=can_start)
if not to_start:
break break
for build in self.db.to_start(limit=can_start): _logger.info(f"Starting {len(to_start)} builds of up to {can_start}.")
for build in to_start:
await build.scale(1) await build.scale(1)
if len(to_start) < can_start:
break # back to sleep
async def stopper(self) -> None: async def stopper(self) -> None:
while True: while True:
@ -85,9 +91,15 @@ class Controller:
while True: while True:
can_stop = self.running - settings.max_running can_stop = self.running - settings.max_running
if can_stop <= 0: if can_stop <= 0:
break # nothing to top for now, back to sleep
to_stop = self.db.oldest_started(limit=can_stop)
if not to_stop:
break break
for build in self.db.oldest_started(limit=can_stop): _logger.info(f"Stopping {len(to_stop)} builds of up to {can_stop}.")
for build in to_stop:
await build.scale(0) await build.scale(0)
if len(to_stop) < can_stop:
break # back to sleep
async def undeployer(self) -> None: async def undeployer(self) -> None:
while True: while True:
@ -95,9 +107,17 @@ class Controller:
while True: while True:
can_undeploy = self.deployed - settings.max_deployed can_undeploy = self.deployed - settings.max_deployed
if can_undeploy <= 0: if can_undeploy <= 0:
break # nothing to undeploy for now, back to sleep
to_undeploy = self.db.oldest_stopped(limit=can_undeploy)
if not to_undeploy:
break break
for build in self.db.oldest_stopped(limit=can_undeploy): _logger.info(
f"Undeploying {len(to_undeploy)} builds of up to {can_undeploy}."
)
for build in to_undeploy:
await build.undeploy() await build.undeploy()
if len(to_undeploy) < can_undeploy:
break # back to sleep
async def start(self): async def start(self):
_logger.info("Starting controller tasks.") _logger.info("Starting controller tasks.")