23
23
24
24
BOSKOS_HOST = os .environ .get ("BOSKOS_HOST" , "boskos" )
25
25
BOSKOS_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
26
30
27
31
28
- def checkout_account (resource_type , user , input_state = "free" ):
32
+ def checkout_account (resource_type , user , input_state = "free" , tries = 1 ):
29
33
url = f'http://{ BOSKOS_HOST } /acquire?type={ resource_type } &state={ input_state } &dest=busy&owner={ user } '
30
34
31
35
r = requests .post (url )
@@ -37,7 +41,15 @@ def checkout_account(resource_type, user, input_state="free"):
37
41
print (f"export BOSKOS_RESOURCE_NAME={ result ['name' ]} " )
38
42
print (f"export AWS_ACCESS_KEY_ID={ result ['userdata' ]['access-key-id' ]} " )
39
43
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 )
41
53
else :
42
54
raise Exception (f"Got invalid response { r .status_code } : { r .reason } " )
43
55
0 commit comments