Skip to content

Instantly share code, notes, and snippets.

@tardyp
Last active September 11, 2021 16:45
Show Gist options
  • Save tardyp/f5ba4591f1c65b5d823ef5ba1dc3f399 to your computer and use it in GitHub Desktop.
Save tardyp/f5ba4591f1c65b5d823ef5ba1dc3f399 to your computer and use it in GitHub Desktop.
a buildbot builders sorter which can prioritize builders that take longer
from datetime import datetime, timedelta
from dateutil.tz import tzutc
from twisted.internet import defer
# this stores bonuses for builders, which takes in account longer build time
builder_bonuses = {
'bldr1': timedelta(hours=1)
}
@defer.inlineCallbacks
def BuilderSorter(master, builders):
# perform an asynchronous schwarzian transform, transforming None
# into a really big date, so that any
# date set to 'None' will appear at the
# end of the list during comparisons.
max_time = datetime.max
# Need to set the timezone on the date, in order
# to perform comparisons with other dates which
# have the time zone set.
max_time = max_time.replace(tzinfo=tzutc())
@defer.inlineCallbacks
def transform(bldr):
time = yield bldr.getOldestRequestTime()
if time is None:
time = max_time
else:
if bldr.name in builder_bonuses:
time = time + builder_bonuses[bldr.name]
defer.returnValue((time, bldr))
transformed = yield defer.gatherResults(
[transform(bldr) for bldr in builders])
# sort the transformed list synchronously, comparing None to the end of
# the list
def transformedKey(a):
(date, builder) = a
return (date, builder.name)
transformed.sort(key=transformedKey)
# and reverse the transform
rv = [xf[1] for xf in transformed]
return rv
c['prioritizeBuilders'] = BuilderSorter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment