]> git.ipfire.org Git - pbs.git/commitdiff
builders: Replace asyncio.gather() with task groups
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 29 May 2023 14:23:34 +0000 (14:23 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 29 May 2023 14:23:34 +0000 (14:23 +0000)
This is easier to write/read and we can run multiple actions
concurrently.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/builders.py

index c2dcb8c8e7b6cc2e6a5438a573ac2d6ad7634e55..75eb15fcdfaa57e4877769015c32def704d0e670 100644 (file)
@@ -81,7 +81,11 @@ class Builders(base.Object):
                log.info("Syncing state with AWS")
 
                # Sync all builders
-               await asyncio.gather(*(builder.sync() for builder in self))
+               async with asyncio.TaskGroup() as tasks:
+                       for builder in self:
+                               tasks.create_task(
+                                       builder.sync(),
+                               )
 
        async def autoscale(self, wait=False):
                """
@@ -150,15 +154,18 @@ class Builders(base.Object):
                        builder for builder in builders if not queue[builder] and builder.is_idle()
                ]
 
-               # Start all builders that have been allocated at least one job
-               await asyncio.gather(
-                       *(builder.start(wait=wait) for builder in builders_to_be_launched),
-               )
+               async with asyncio.TaskGroup() as tasks:
+                       # Start all builders that have been allocated at least one job
+                       for builder in builders_to_be_launched:
+                               tasks.create_task(
+                                       builder.start(wait=wait),
+                               )
 
-               # Shutdown the rest
-               await asyncio.gather(
-                       *(builder.stop(wait=wait) for builder in builders_to_be_shut_down),
-               )
+                       # Shutdown the rest
+                       for builder in builders_to_be_shut_down:
+                               tasks.create_task(
+                                       builder.stop(wait=wait),
+                               )
 
        # Stats
 
@@ -239,9 +246,11 @@ class BuildersStats(base.Object):
                        return
 
                # Send the stats to all connections
-               await asyncio.gather(
-                       *(c.submit_stats(stats) for c in connections),
-               )
+               async with asyncio.TaskGroup() as tasks:
+                       for c in connections:
+                               tasks.create_task(
+                                       c.submit_stats(stats),
+                               )
 
 
 class Builder(base.DataObject):