Skip to content
This repository was archived by the owner on Feb 2, 2022. It is now read-only.

Commit 52e7ba4

Browse files
stishkinstas
andauthored
add retry logic to CLI when service is temporarily unavailable (#129)
Co-authored-by: stas <statis@microsoft.com>
1 parent b0046a8 commit 52e7ba4

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

cli/raft_sdk/raft_common.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import atexit
77
import string
88
from pathlib import Path
9+
import time
910

1011
script_dir = os.path.dirname(os.path.abspath(__file__))
1112
cache_dir = os.path.join(str(Path.home()), '.cache', 'raft')
@@ -80,6 +81,8 @@ def __init__(self, endpoint, client_id, tenant_id, secret):
8081
self.tenant_id = tenant_id
8182
self.secret = secret
8283

84+
self.retry_status_code = [503]
85+
8386
def auth_header(self):
8487
token = get_auth_token(self.client_id, self.tenant_id, self.secret)
8588
if 'error_description' in token:
@@ -88,27 +91,51 @@ def auth_header(self):
8891
'Authorization': f"{token['token_type']} {token['access_token']}"
8992
}
9093

91-
def post(self, relative_url, json_data):
92-
return requests.post(
94+
def post(self, relative_url, json_data, seconds_to_wait=10):
95+
response = requests.post(
9396
self.endpoint + relative_url,
9497
json=json_data,
9598
headers=self.auth_header())
99+
if (response.status_code in self.retry_status_code and
100+
seconds_to_wait > 0.0):
101+
time.sleep(2.0)
102+
return self.post(relative_url, json_data, seconds_to_wait-2.0)
103+
else:
104+
return response
96105

97-
def put(self, relative_url, json_data):
98-
return requests.put(
106+
def put(self, relative_url, json_data, seconds_to_wait=10):
107+
response = requests.put(
99108
self.endpoint + relative_url,
100109
json=json_data,
101110
headers=self.auth_header())
111+
if (response.status_code in self.retry_status_code and
112+
seconds_to_wait > 0.0):
113+
time.sleep(2.0)
114+
return self.put(relative_url, json_data, seconds_to_wait-2.0)
115+
else:
116+
return response
102117

103-
def delete(self, relative_url):
104-
return requests.delete(
118+
def delete(self, relative_url, seconds_to_wait=10):
119+
response = requests.delete(
105120
self.endpoint + relative_url,
106121
headers=self.auth_header())
122+
if (response.status_code in self.retry_status_code and
123+
seconds_to_wait > 0.0):
124+
time.sleep(2.0)
125+
return self.delete(relative_url, seconds_to_wait-2.0)
126+
else:
127+
return response
107128

108-
def get(self, relative_url):
109-
return requests.get(
129+
def get(self, relative_url, seconds_to_wait=10):
130+
response = requests.get(
110131
self.endpoint + relative_url,
111132
headers=self.auth_header())
133+
if (response.status_code in self.retry_status_code and
134+
seconds_to_wait > 0.0):
135+
time.sleep(2.0)
136+
return self.get(relative_url, seconds_to_wait-2.0)
137+
else:
138+
return response
112139

113140

114141
class RaftDefinitions():

cli/raft_sdk/raft_deploy.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -584,17 +584,17 @@ def init_service_principal(
584584
f' --api-permissions "{user_read_permission}=Scope"')
585585
except RaftAzCliException as ex:
586586
if (ex.error_message.startswith(
587-
'Invoking "az ad app permission grant'
588-
f' --id {app_id}'
589-
' --api'
590-
' 00000003-0000-0000-c000-000000000000"'
591-
' is needed to make the change effective') or
587+
'Invoking "az ad app permission grant'
588+
f' --id {app_id}'
589+
' --api'
590+
' 00000003-0000-0000-c000-000000000000"'
591+
' is needed to make the change effective') or
592592
ex.error_message.startswith(
593-
'WARNING: Invoking "az ad app permission grant'
594-
f' --id {app_id}'
595-
' --api'
596-
' 00000003-0000-0000-c000-000000000000"'
597-
' is needed to make the change effective')):
593+
'WARNING: Invoking "az ad app permission grant'
594+
f' --id {app_id}'
595+
' --api'
596+
' 00000003-0000-0000-c000-000000000000"'
597+
' is needed to make the change effective')):
598598

599599
pass
600600
else:

src/Agent/AzureAuth/Auth.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ let main argv =
114114
| Some uri ->
115115
authBuilder.WithAuthority(uri, auth.tenant, true).Build()
116116

117-
let! r = cred.AcquireTokenForClient(scopes).ExecuteAsync() |> Async.AwaitTask
117+
let! r = cred.AcquireTokenForClient(scopes).ExecuteAsync() |> Async.AwaitTask
118118

119119
match config.PrependLine with
120120
| Some h -> printfn "%s" h

0 commit comments

Comments
 (0)