Skip to content

Commit

Permalink
Refactor attempting loops in jenkins (#34057)
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro committed Sep 4, 2023
1 parent 2134875 commit a6a7a89
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions airflow/providers/jenkins/operators/jenkins_job_trigger.py
Expand Up @@ -149,12 +149,14 @@ def poll_job_in_queue(self, location: str, jenkins_server: Jenkins) -> int:
:param jenkins_server: The jenkins server to poll
:return: The build_number corresponding to the triggered job
"""
try_count = 0
location += "/api/json"
# TODO Use get_queue_info instead
# once it will be available in python-jenkins (v > 0.4.15)
self.log.info("Polling jenkins queue at the url %s", location)
while try_count < self.max_try_before_job_appears:
for attempt in range(self.max_try_before_job_appears):
if attempt:
time.sleep(self.sleep_time)

try:
location_answer = jenkins_request_with_headers(
jenkins_server, Request(method="POST", url=location)
Expand All @@ -163,26 +165,22 @@ def poll_job_in_queue(self, location: str, jenkins_server: Jenkins) -> int:
# until max_try_before_job_appears reached
except (HTTPError, JenkinsException):
self.log.warning("polling failed, retrying", exc_info=True)
try_count += 1
time.sleep(self.sleep_time)
continue

if location_answer is not None:
json_response = json.loads(location_answer["body"])
if (
"executable" in json_response
and json_response["executable"] is not None
and "number" in json_response["executable"]
):
build_number = json_response["executable"]["number"]
self.log.info("Job executed on Jenkins side with the build number %s", build_number)
return build_number
try_count += 1
time.sleep(self.sleep_time)

raise AirflowException(
f"The job hasn't been executed after polling the queue {self.max_try_before_job_appears} times"
)
else:
if location_answer is not None:
json_response = json.loads(location_answer["body"])
if (
"executable" in json_response
and json_response["executable"] is not None
and "number" in json_response["executable"]
):
build_number = json_response["executable"]["number"]
self.log.info("Job executed on Jenkins side with the build number %s", build_number)
return build_number
else:
raise AirflowException(
f"The job hasn't been executed after polling the queue "
f"{self.max_try_before_job_appears} times"
)

def get_hook(self) -> JenkinsHook:
"""Instantiate the Jenkins hook."""
Expand Down

0 comments on commit a6a7a89

Please sign in to comment.