You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/03/22 14:13:39 UTC
[GitHub] [airflow] eladkal commented on a change in pull request #22421: Jenkins sensor
eladkal commented on a change in pull request #22421:
URL: https://github.com/apache/airflow/pull/22421#discussion_r832232224
##########
File path: airflow/providers/jenkins/sensors/jenkins.py
##########
@@ -0,0 +1,86 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from typing import Any, Dict, Optional
+from urllib.error import HTTPError, URLError
+
+import jenkins
+from jenkins import JenkinsException
+
+from airflow.exceptions import AirflowException
+from airflow.providers.jenkins.hooks.jenkins import JenkinsHook
+from airflow.sensors.base import BaseSensorOperator
+
+class JenkinsSensor(BaseSensorOperator):
+ """
+ Monitor a jenkins job and pass when it is finished building. This is regardless of the build outcome.
+ This sensor depend on python-jenkins library,
+ version >= 0.4.15 to communicate with jenkins server.
+ You'll also need to configure a Jenkins connection in the connections screen.
Review comment:
Minimum lib version is 1.0.0 so no need to point that
https://github.com/apache/airflow/blob/fa655fe53dcf34a7b581fe3e881d770921dfc7a0/setup.py#L410
As for the 2nd point also - all sensors work with connections so no reason for special mention of this.
##########
File path: airflow/providers/jenkins/sensors/jenkins.py
##########
@@ -0,0 +1,86 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from typing import Any, Dict, Optional
+from urllib.error import HTTPError, URLError
+
+import jenkins
+from jenkins import JenkinsException
+
+from airflow.exceptions import AirflowException
+from airflow.providers.jenkins.hooks.jenkins import JenkinsHook
+from airflow.sensors.base import BaseSensorOperator
+
+class JenkinsSensor(BaseSensorOperator):
+ """
+ Monitor a jenkins job and pass when it is finished building. This is regardless of the build outcome.
+ This sensor depend on python-jenkins library,
+ version >= 0.4.15 to communicate with jenkins server.
+ You'll also need to configure a Jenkins connection in the connections screen.
+
+ :param jenkins_connection_id: The jenkins connection to use for this job
+ :param job_name: The name of the job to check
+ :param build_number: Build number to check - if None, the latest build will be used
+ """
+
+ def __init__(
+ self,
+ *,
+ jenkins_connection_id: str,
+ job_name: str,
+ build_number: Optional[int] = None,
+ **kwargs,
+ ):
+ super().__init__(**kwargs)
+ self.job_name = job_name
+ self.build_number = build_number
+ self.jenkins_connection_id = jenkins_connection_id
+
+ def get_hook(self) -> JenkinsHook:
+ """Instantiate jenkins hook"""
+ return JenkinsHook(self.jenkins_connection_id)
+
+ def poke(self, context: 'Context') -> bool:
+ try:
+ self.log.info(f"Poking jenkins job {self.job_name}")
+
+ jenkins_server = self.get_hook().get_jenkins_server()
+ if not self.build_number:
+ job_info = jenkins_server.get_job_info(self.job_name)
+ build_number_to_check = job_info['lastBuild']['number']
+ self.log.info(f"Build number not specified, getting latest build info from Jenkins")
+ else:
+ build_number_to_check = self.build_number
+
+ self.log.info(f"Checking build number: #{build_number_to_check}")
+
+ build_info = jenkins_server.get_build_info(self.job_name, build_number_to_check)
+ if build_info['building']:
+ self.log.info(f"Build #{build_number_to_check} is still building")
+ return False
+ else:
+ self.log.info(f"Build #{build_number_to_check} is finished")
+ return True
Review comment:
this logic should not be in the sensor code.
The hook should have a general function that returns a build status.
Then the sensor code should be something like:
```
def poke(self, context: 'Context') -> bool:
if get_build_status(...)= 'building'
return False
else:
return True
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org