Buffer events to avoid flooding

This commit is contained in:
Stéphane Bidoul 2021-11-30 18:26:52 +01:00
parent 2b27de3256
commit 825d8d2e4a
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92

View file

@ -10,6 +10,12 @@ from .settings import settings
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
# In some circumstances, on_build_event can be called very frequently (e.g. when the
# controller starts and discovers existing deployments). A small delay before the wakeup
# of the background tasks and the clearing of the wakeup avoids waking up the tasks
# too often.
EVENT_BUFFERING_DELAY = 1
class Controller: class Controller:
"""The controller monitors and manages the deployments. """The controller monitors and manages the deployments.
@ -45,7 +51,6 @@ class Controller:
self._wakeup_initializer.set() self._wakeup_initializer.set()
self._wakeup_stopper.set() self._wakeup_stopper.set()
self._wakeup_undeployer.set() self._wakeup_undeployer.set()
if event == BuildEvent.modified and build.status == BuildStatus.undeploying:
self._wakeup_cleaner.set() self._wakeup_cleaner.set()
@property @property
@ -184,6 +189,7 @@ class Controller:
async def cleaner(self) -> None: async def cleaner(self) -> None:
while True: while True:
await self._wakeup_cleaner.wait() await self._wakeup_cleaner.wait()
await asyncio.sleep(EVENT_BUFFERING_DELAY)
self._wakeup_cleaner.clear() self._wakeup_cleaner.clear()
for build in self.db.to_cleanup(): for build in self.db.to_cleanup():
await build.cleanup() await build.cleanup()
@ -191,6 +197,7 @@ class Controller:
async def initializer(self) -> None: async def initializer(self) -> None:
while True: while True:
await self._wakeup_initializer.wait() await self._wakeup_initializer.wait()
await asyncio.sleep(EVENT_BUFFERING_DELAY)
self._wakeup_initializer.clear() self._wakeup_initializer.clear()
can_initialize = self.max_initializing - self.initializing can_initialize = self.max_initializing - self.initializing
if can_initialize <= 0: if can_initialize <= 0:
@ -208,6 +215,7 @@ class Controller:
async def stopper(self) -> None: async def stopper(self) -> None:
while True: while True:
await self._wakeup_stopper.wait() await self._wakeup_stopper.wait()
await asyncio.sleep(EVENT_BUFFERING_DELAY)
self._wakeup_stopper.clear() self._wakeup_stopper.clear()
can_stop = self.started - self.max_started can_stop = self.started - self.max_started
if can_stop <= 0: if can_stop <= 0:
@ -225,6 +233,7 @@ class Controller:
async def undeployer(self) -> None: async def undeployer(self) -> None:
while True: while True:
await self._wakeup_undeployer.wait() await self._wakeup_undeployer.wait()
await asyncio.sleep(EVENT_BUFFERING_DELAY)
self._wakeup_undeployer.clear() self._wakeup_undeployer.clear()
can_undeploy = self.deployed - self.max_deployed can_undeploy = self.deployed - self.max_deployed
if can_undeploy <= 0: if can_undeploy <= 0: