2323
2424BOSKOS_HOST = os .environ .get ("BOSKOS_HOST" , "boskos" )
2525BOSKOS_RESOURCE_NAME = os .environ .get ('BOSKOS_RESOURCE_NAME' )
26+ # Retry up to 3 times, with 10 seconds between tries. This is the same as defaults
27+ # on https://github.com/kubernetes-sigs/boskos/
28+ MAX_RETRIES = 3
29+ RETRY_WAIT = 10
2630
2731
28- def checkout_account (resource_type , user , input_state = "free" ):
32+ def checkout_account (resource_type , user , input_state = "free" , tries = 1 ):
2933 url = f'http://{ BOSKOS_HOST } /acquire?type={ resource_type } &state={ input_state } &dest=busy&owner={ user } '
3034
3135 r = requests .post (url )
@@ -37,7 +41,15 @@ def checkout_account(resource_type, user, input_state="free"):
3741 print (f"export BOSKOS_RESOURCE_NAME={ result ['name' ]} " )
3842 print (f"export AWS_ACCESS_KEY_ID={ result ['userdata' ]['access-key-id' ]} " )
3943 print (f"export AWS_SECRET_ACCESS_KEY={ result ['userdata' ]['secret-access-key' ]} " )
40-
44+ # The http API has two possible meanings of 404s - the named resource type cannot be found or there no available resources of the type.
45+ # For our purposes, we don't need to differentiate.
46+ elif r .status_code == 404 :
47+ print (f"could not find available host, retrying in { RETRY_WAIT } s" )
48+ if tries > MAX_RETRIES :
49+ raise Exception (f"could not allocate host after { MAX_RETRIES } tries" )
50+ tries = tries + 1
51+ time .sleep (RETRY_WAIT )
52+ return checkout_account (resource_type , user , input_state , tries )
4153 else :
4254 raise Exception (f"Got invalid response { r .status_code } : { r .reason } " )
4355
0 commit comments