Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chengshifan committed Feb 21, 2024
1 parent b0f589c commit 2b3b253
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/api/handlers/job_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def get_token_by_app_role(app_role_url, role_id, secret_id, retry_times=3):
app_role = {'role_id': role_id, 'secret_id': secret_id}
json_data = json.dumps(app_role)
attempt = 0
err_msg = 'Getting token from Vault error'
while retry_times > 0:
res = requests.post(url=app_role_url, data=json_data, verify=False)
attempt += 1
Expand All @@ -60,13 +61,15 @@ def get_token_by_app_role(app_role_url, role_id, secret_id, retry_times=3):
logger.warning("Vault Token returned code is not 200, will retry after %s seconds...", 10 * pow(2, attempt))
time.sleep(10 * pow(2, attempt))
else:
logger.error("Getting token from Vault error even though retried '%s' times, url is '%s', API response is '%s':'%s'", attempt, app_role_url, res.status_code, res.text)
err_msg = "Getting token from Vault error even though retried {} times, url is {}, API response is {}:{}".format(attempt, app_role_url, res.status_code, res.text)
logger.error(err_msg)
retry_times -= 1
abort(400, "Getting token from Vault error even though retried '%s' times, url is '%s', API response is '%s':'%s'", attempt, app_role_url, res.status_code, res.text)
abort(400, err_msg)


def get_value_from_vault(url, token, secret_key, verify, retry_times=3):
attempt = 0
err_msg = 'Getting value from Vault error'
while retry_times > 0:
response = requests.get(url=url, headers={'X-Vault-Token': token}, verify=verify)
attempt += 1
Expand All @@ -82,9 +85,10 @@ def get_value_from_vault(url, token, secret_key, verify, retry_times=3):
logger.warning("Vault value returned code is not 200, will retry after %s seconds...", 10 * pow(2, attempt))
time.sleep(10 * pow(2, attempt))
else:
logger.error("Getting value from Vault error even though retried %s times, url is '%s', API response is '%s':'%s'", attempt, url, response.status_code, response.text)
err_msg = "Getting value from Vault error even though retried {} times, url is {}, API response is {}:{}".format(attempt, url, response.status_code, response.text)
logger.error(err_msg)
retry_times -= 1
abort(400, "Getting value from Vault error even though retried %s times, url is '%s', API response is '%s':'%s'", attempt, url, response.status_code, response.text)
abort(400, err_msg)


@api.route("/api/job/job", doc=False)
Expand Down

0 comments on commit 2b3b253

Please sign in to comment.