Skip to content

Commit

Permalink
Handle http status 503 and 504
Browse files Browse the repository at this point in the history
  • Loading branch information
droberts2013 committed Jul 10, 2023
1 parent b36d4b5 commit 91991a7
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/ansibletower/launchAndWait.wait_for_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def formatted_print(message):

api_url = '/api/v2/%s/%s/' % (status_path_component, job_id)

unavailable_status_recommendations = """
The Ansible server did not return a timely response. The server or network may be overloaded.\n" +
Recommendations:
Use a global variable for the wait interval and increase its value to lighten the load on the server.
Use a global variable for max tries and adjust it inversely to accommodate expected execution durations.
"""

response = request.get(api_url, contentType='application/json',headers=headers)
num_tries += 1
if response.isSuccessful():
Expand All @@ -68,6 +75,24 @@ def formatted_print(message):
if status in ["failed"]:
formatted_print(">>> Job failed after " + str(num_tries) + " tries")
raise Exception("Error: job failed")
elif response.getStatus() == 503:
task.setStatusLine("Job id %s status unavailable (503)")
formatted_print(">>> Job status after " + str(num_tries) + " tries is unavailable: HTTP status 503, Service Unavailable." + unavailable_status_recommendations)
if max_retries:
if num_tries > max_retries:
job_output=request.get(api_url+'stdout/', contentType='text/plain',headers=headers).response
formatted_print(job_output)
raise Exception("Error: maximum number of tries reached")
task.schedule("ansibletower/launchAndWait.wait_for_completion.py", int(wait_interval))
elif response.getStatus() == 504:
task.setStatusLine("Job id %s status unavailable (504)")
formatted_print(">>> Job status after " + str(num_tries) + " tries is unavailable: HTTP status 504, Gateway Timeout." + unavailable_status_recommendations)
if max_retries:
if num_tries > max_retries:
job_output=request.get(api_url+'stdout/', contentType='text/plain',headers=headers).response
formatted_print(job_output)
raise Exception("Error: maximum number of tries reached")
task.schedule("ansibletower/launchAndWait.wait_for_completion.py", int(wait_interval))
else:
formatted_print(">>> Task failed after " + str(num_tries) + " tries. Job status was not retrieved.")
raise Exception("Error: server returned [%s], with content [%s]" % (response.status, response.response))

0 comments on commit 91991a7

Please sign in to comment.