YingboWang commented on a change in pull request #5499:
URL: https://github.com/apache/airflow/pull/5499#discussion_r468792189



##########
File path: airflow/sensors/smart_sensor_operator.py
##########
@@ -0,0 +1,730 @@
+#
+# 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.
+
+
+import datetime
+import json
+import logging
+import time
+import traceback
+from logging.config import DictConfigurator  # type: ignore
+from time import sleep
+
+from sqlalchemy import and_, or_, tuple_
+
+from airflow.exceptions import AirflowException, AirflowTaskTimeout
+from airflow.models import BaseOperator, SensorInstance, SkipMixin, 
TaskInstance
+from airflow.settings import LOGGING_CLASS_PATH
+from airflow.stats import Stats
+from airflow.utils import helpers, timezone
+from airflow.utils.decorators import apply_defaults
+from airflow.utils.email import send_email
+from airflow.utils.log.logging_mixin import set_context
+from airflow.utils.module_loading import import_string
+from airflow.utils.net import get_hostname
+from airflow.utils.session import provide_session
+from airflow.utils.state import PokeState, State
+from airflow.utils.timeout import timeout
+
+config = import_string(LOGGING_CLASS_PATH)
+handler_config = config['handlers']['task']
+try:
+    formatter_config = config['formatters'][handler_config['formatter']]
+except Exception as err:  # pylint: disable=broad-except
+    formatter_config = None
+    print(err)
+dictConfigurator = DictConfigurator(config)
+
+
+class SensorWork:
+    """
+    This class stores a sensor work with decoded context value. It is only used
+    inside of smart sensor.
+    """
+    def __init__(self, ti):
+        self.dag_id = ti.dag_id
+        self.task_id = ti.task_id
+        self.execution_date = ti.execution_date
+        self.try_number = ti.try_number
+
+        self.poke_context = json.loads(ti.poke_context) if ti.poke_context 
else {}
+        self.execution_context = json.loads(ti.execution_context) if 
ti.execution_context else {}
+        try:
+            self.log = self._get_sensor_logger(ti)
+        except Exception as e:  # pylint: disable=broad-except
+            self.log = None
+            print(e)
+        self.hashcode = ti.hashcode
+        self.start_date = ti.start_date
+        self.operator = ti.operator
+        self.op_classpath = ti.op_classpath
+        self.encoded_poke_context = ti.poke_context
+
+    def __eq__(self, other):
+        if not isinstance(other, SensorWork):
+            return NotImplemented
+
+        return self.dag_id == other.dag_id and \
+            self.task_id == other.task_id and \
+            self.execution_date == other.execution_date and \
+            self.try_number == other.try_number
+
+    @staticmethod
+    def create_new_task_handler():
+        """
+        Create task log handler for a sensor work.
+        :return: log handler
+        """
+        handler_config_copy = {k: handler_config[k] for k in handler_config}
+        formatter_config_copy = {k: formatter_config[k] for k in 
formatter_config}
+        handler = dictConfigurator.configure_handler(handler_config_copy)
+        formatter = dictConfigurator.configure_formatter(formatter_config_copy)
+        handler.setFormatter(formatter)
+        return handler
+
+    def _get_sensor_logger(self, ti):
+        # TODO: should be somewhere else, but not this file, has to use 
LOG_ID_TEMPLATE from es
+        # but how about other log file handler?
+        ti.raw = False  # Otherwise set_context will fail
+        log_id = "-".join([ti.dag_id,
+                           ti.task_id,
+                           ti.execution_date.strftime("%Y_%m_%dT%H_%M_%S_%f"),

Review comment:
       Should not fail. The log_id is used as an internal key for fetching 
these in memory log handler only in smart sensor service. The output path was 
configure in the `set_context` function which should always be consistent with 
other tasks. 




----------------------------------------------------------------
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:
[email protected]


Reply via email to