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 2019/05/01 08:47:05 UTC

[GitHub] [airflow] ashb commented on a change in pull request #5118: [AIRFLOW-4315] Add monitoring API's to airflow

ashb commented on a change in pull request #5118: [AIRFLOW-4315] Add monitoring API's to airflow
URL: https://github.com/apache/airflow/pull/5118#discussion_r280025480
 
 

 ##########
 File path: airflow/api/common/experimental/get_task_logs.py
 ##########
 @@ -0,0 +1,91 @@
+# -*- coding: utf-8 -*-
+#
+# 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 airflow.exceptions import (DagNotFound, TaskNotFound,
+                                DagRunNotFound, TaskInstanceNotFound)
+from airflow.models import DagBag
+from airflow.models import TaskInstance
+from airflow.utils import timezone
+import logging
+from io import BytesIO
+from airflow import configuration as conf
+from airflow.utils.helpers import render_log_filename
+from flask import send_file
+
+
+def get_task_logs(dag_id, task_id, execution_date):
+    """Return the logs for the task instance identified by the given dag_id, execution_date and task_id."""
+
+    execution_date = timezone.parse(execution_date)
+
+    dagbag = DagBag()
+
+    # Check DAG exists.
+    if dag_id not in dagbag.dags:
+        error_message = "Dag id {} not found".format(dag_id)
+        raise DagNotFound(error_message)
+
+    # Get DAG object and check Task Exists
+    dag = dagbag.get_dag(dag_id)
+    if not dag.has_task(task_id):
+        error_message = 'Task {} not found in dag {}'.format(task_id, dag_id)
+        raise TaskNotFound(error_message)
+
+    # Get DagRun object and check that it exists
+    dagrun = dag.get_dagrun(execution_date=execution_date)
+    if not dagrun:
+        error_message = ('Dag Run for date {} not found in dag {}'
+                         .format(execution_date, dag_id))
+        raise DagRunNotFound(error_message)
+
+    # Get task instance object and check that it exists
+    task_instance = dagrun.get_task_instance(task_id)
+    if not task_instance:
+        error_message = ('Task {} instance for date {} not found'
+                         .format(task_id, execution_date))
+        raise TaskInstanceNotFound(error_message)
+
+    ti = TaskInstance.find(dag_id=dag_id, task_id=task_id, execution_date=execution_date)[0]
 
 Review comment:
   Everything in the fn up to this point should be replaced with a call to `get_task_instance(dag_id, task_id, executition_date)` from airflow.api.common.experimental.get_task_instance

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services