Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix for apache-airflow-providers-jenkins JenkinsJobTriggerOperator #22802

Merged

Conversation

SasanAhmadi
Copy link
Contributor

@SasanAhmadi SasanAhmadi commented Apr 6, 2022

This is to eliminate a bug in jenkins job trigger opertor. During polling for the newly created build, it is possible to get a failure and it is causing the task to fail and if task have retries then it will result in duplicate builds which could lead to problems.

here is the exception I'm receiving in the task logs:

[2022-04-05, 18:50:32 PDT] {jenkins_job_trigger.py:176} INFO - Triggering the job Segmentation/Production/Downstream Trigger - Production on the jenkins : JENKINS with the parameters : None
[2022-04-05, 18:50:32 PDT] {base.py:70} INFO - Using connection to: id: JENKINS. Host: server.com, Port: 443, Schema: , Login: ****, Password: ***, extra: True
[2022-04-05, 18:50:32 PDT] {jenkins.py:43} INFO - Trying to connect to [https://server.com:443](https://server.com/)
[2022-04-05, 18:50:32 PDT] {kerberos_.py:325} ERROR - handle_other(): Mutual authentication unavailable on 403 response
[2022-04-05, 18:50:33 PDT] {jenkins_job_trigger.py:154} INFO - Polling jenkins queue at the url https://server.com/queue/item/5870086//api/json
[2022-04-05, 18:50:33 PDT] {taskinstance.py:1700} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/opt/bitnami/airflow/venv/lib/python3.8/site-packages/jenkins/__init__.py", line 575, in jenkins_request
    return self._response_handler(
  File "/opt/bitnami/airflow/venv/lib/python3.8/site-packages/jenkins/__init__.py", line 536, in _response_handler
    response.raise_for_status()
  File "/opt/bitnami/airflow/venv/lib/python3.8/site-packages/requests/models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://server.com/queue/item/5870086//api/json

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/bitnami/airflow/venv/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1329, in _run_raw_task
    self._execute_task_with_callbacks(context)
  File "/opt/bitnami/airflow/venv/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1455, in _execute_task_with_callbacks
    result = self._execute_task(context, self.task)
  File "/opt/bitnami/airflow/venv/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1511, in _execute_task
    result = execute_callable(context=context)
  File "/opt/bitnami/airflow/dags/git_dags/jenkins_custom_lib/operators/jenkins_job_trigger.py", line 185, in execute
    build_number = self.poll_job_in_queue(jenkins_response['headers']['Location'], jenkins_server)
  File "/opt/bitnami/airflow/dags/git_dags/jenkins_custom_lib/operators/jenkins_job_trigger.py", line 156, in poll_job_in_queue
    location_answer = jenkins_request_with_headers(
  File "/opt/bitnami/airflow/dags/git_dags/jenkins_custom_lib/operators/jenkins_job_trigger.py", line 51, in jenkins_request_with_headers
    response = jenkins_server.jenkins_request(req)
  File "/opt/bitnami/airflow/venv/lib/python3.8/site-packages/jenkins/__init__.py", line 589, in jenkins_request
    raise NotFoundException('Requested item could not be found')
jenkins.NotFoundException: Requested item could not be found
[2022-04-05, 18:50:33 PDT] {taskinstance.py:1267} INFO - Marking task as UP_FOR_RETRY. dag_id=segmentation_v1, task_id=trigger_downstream_jenkins, execution_date=20220406T002500, start_date=20220406T015032, end_date=20220406T015033
[2022-04-05, 18:50:33 PDT] {standard_task_runner.py:89} ERROR - Failed to execute job 326683 for task trigger_downstream_jenkins
Traceback (most recent call last):
  File "/opt/bitnami/airflow/venv/lib/python3.8/site-packages/jenkins/__init__.py", line 575, in jenkins_request
    return self._response_handler(
  File "/opt/bitnami/airflow/venv/lib/python3.8/site-packages/jenkins/__init__.py", line 536, in _response_handler
    response.raise_for_status()
  File "/opt/bitnami/airflow/venv/lib/python3.8/site-packages/requests/models.py", line 953, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://server.com/queue/item/5870086//api/json

at the line 589 of jenkins_request in Jenkins library it returns a JobNotFoundException exception which leads to failure of jenkins_request_with_headers method. Because poll_job_in_queue doesn't account for exception handling it will fail and ultimately sets the whole task for retry (if configured!). This will lead to creating another build by going over the whole processes which is problematic.

Instead it should complete its polling retries and if failed then raise airflow exception and ultimately set the task for retry.

…it should not fail the task, instead it should continue polling until reaches the max allowed polling tries
@SasanAhmadi SasanAhmadi changed the title bugfix for when polling for the created job, if fail to get job info it should not fail the task, instead it should continue polling until reaches the max allowed polling tries Bug Fix for apache-airflow-providers-jenkins Apr 6, 2022
@SasanAhmadi SasanAhmadi changed the title Bug Fix for apache-airflow-providers-jenkins Bug Fix for apache-airflow-providers-jenkins JenkinsJobTriggerOperator Apr 6, 2022
try except accounts for only the risky part of the code
@github-actions github-actions bot added the okay to merge It's ok to merge this PR as it does not require more tests label Apr 7, 2022
@github-actions
Copy link

github-actions bot commented Apr 7, 2022

The PR is likely OK to be merged with just subset of tests for default Python and Database versions without running the full matrix of tests, because it does not modify the core of Airflow. If the committers decide that the full tests matrix is needed, they will add the label 'full tests needed'. Then you should rebase to the latest main or amend the last commit of the PR, and push it with --force-with-lease.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:providers okay to merge It's ok to merge this PR as it does not require more tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants