# XXX max queue length
threshold = datetime.timedelta(minutes=5)
- # Fetch all enabled builders and whether they are running or not
- builders = {
- builder : await builder.is_running() for builder in self if builder.enabled
- }
+ # Fetch all enabled builders
+ builders = [b for b in self if b.enabled]
+
+ # Fetch the priority for each builder
+ builders = dict(
+ zip(builders, await asyncio.gather(
+ *(b._autoscale_sort() for b in builders),
+ )),
+ )
- # Sort all running builders to the beginning of the list
- builders = sorted(builders, key=lambda b: (builders[b], b))
+ # Sort all builders after their priority
+ builders = sorted(builders, key=lambda b: (-builders[b], b))
# Store the length of the queue for each builder
queue = {
log.debug("%s has been stopped" % self)
+ async def _autoscale_sort(self):
+ """
+ This function returns a number that tells the autoscaling algorithm
+ in which order a builder should be considered.
+
+ The higher the number, the sooner the builder is considered.
+ """
+ # Return something neutral if this isn't an AWS instance
+ if not self.instance:
+ return 0
+
+ # Running builds should come first
+ if await self.is_running():
+ return 1
+
+ # Put builders that are shutting down at the end
+ elif await self.is_shutting_down():
+ return -1
+
+ # Otherwise return a neutral preference
+ return 0
+
# Stats
@lazy_property