potiuk commented on a change in pull request #7257: [AIRFLOW-6636] Avoid 
exceptions when printing task instance
URL: https://github.com/apache/airflow/pull/7257#discussion_r370990274
 
 

 ##########
 File path: airflow/models/taskinstance.py
 ##########
 @@ -1137,18 +1143,30 @@ def handle_failure(self, error, test_mode=None, 
context=None, session=None):
                         'dag_id=%s, task_id=%s, execution_date=%s, 
start_date=%s, end_date=%s',
                         self.dag_id,
                         self.task_id,
-                        self.execution_date.strftime('%Y%m%dT%H%M%S'),
-                        self.start_date.strftime('%Y%m%dT%H%M%S'),
-                        self.end_date.strftime('%Y%m%dT%H%M%S'))
+                        self.execution_date.strftime('%Y%m%dT%H%M%S') if 
hasattr(
+                            self,
+                            'execution_date') and self.execution_date else '',
+                        self.start_date.strftime('%Y%m%dT%H%M%S') if hasattr(
+                            self,
+                            'start_date') and self.start_date else '',
+                        self.end_date.strftime('%Y%m%dT%H%M%S') if hasattr(
 
 Review comment:
   This is the error I originally had. The test was still passing and the ERROR 
sigterm logs were expected (on kill). But then apparently in signal processing 
path task instance was being printed to log and it was producing this 
misleading exceptions about NoneType not having strftime.  I looked at other 
places where task instance was printed and I simply aligned all the places to 
be the same (with getattr and checking for empty) - just to have a consistent 
approach.
   
   If you can modify all the places to not use getattra and maybe even extract 
taskinstance printing to separate method and trace down where the attributes 
could have been missing and make sure it is all tested for all cases, I am 
happy if you do it. 
   
   Note that you should do it for both master and v1-10-test because primary 
problem was in 1.10 branch.
   
   ```
   
tests/task/task_runner/test_standard_task_runner.py::TestStandardTaskRunner::test_on_kill
 ========================= AIRFLOW ==========================
   Airflow home /root/airflow
   Home of the user: /root
   Skipping initializing of the DB as it was initialized already.
   You can re-initialize the database by adding --with-db-init flag when 
running tests.
   [2020-01-25 11:38:56,950] {{__init__.py:51}} INFO - Using executor 
SequentialExecutor
   [2020-01-25 11:38:56,951] {{dagbag.py:403}} INFO - Filling up the DagBag 
from /opt/airflow/tests/dags
   [2020-01-25 11:38:57,050] {test_task_view_type_check.py:50} INFO - 
class_instance type: <class 
'unusual_prefix_5d280a9b385120fec3c40cfe5be04e2f41b6b5e8_test_task_view_type_check.CallableClass'>
   [2020-01-25 11:38:57,085] {{dagbag.py:271}} INFO - File 
/opt/airflow/tests/dags/test_zip.zip assumed to contain no DAGs. Skipping.
   [2020-01-25 11:38:57,144] {{standard_task_runner.py:52}} INFO - Started 
process 235 to run task
   [2020-01-25 11:38:57,196] {{dagbag.py:403}} INFO - Filling up the DagBag 
from /opt/airflow/tests/dags/test_on_kill.py
   Running %s on host %s <TaskInstance: test_on_kill.task1 
2016-01-01T00:00:00+00:00 [None]> 7f8368b91b67
   [2020-01-25 11:39:00,161] {{helpers.py:322}} INFO - Sending Signals.SIGTERM 
to GPID 235
   [2020-01-25 11:39:00,162] {{taskinstance.py:941}} ERROR - Received SIGTERM. 
Terminating subprocesses.
   [2020-01-25 11:39:00,164] {{test_on_kill.py:36}} INFO - Executing on_kill
   [2020-01-25 11:39:00,162] {{taskinstance.py:941}} ERROR - Received SIGTERM. 
Terminating subprocesses.
   [2020-01-25 11:39:00,165] {{test_on_kill.py:36}} INFO - Executing on_kill
   [2020-01-25 11:39:00,185] {{taskinstance.py:1122}} ERROR - Task received 
SIGTERM signal
   Traceback (most recent call last):
     File "/opt/airflow/airflow/models/taskinstance.py", line 966, in 
_run_raw_task
       result = task_copy.execute(context=context)
     File "/opt/airflow/tests/dags/test_on_kill.py", line 32, in execute
       os.system('sleep 10')
     File "/opt/airflow/airflow/models/taskinstance.py", line 943, in 
signal_handler
       raise AirflowException("Task received SIGTERM signal")
   airflow.exceptions.AirflowException: Task received SIGTERM signal
   [2020-01-25 11:39:00,185] {{taskinstance.py:1122}} ERROR - Task received 
SIGTERM signal
   Traceback (most recent call last):
     File "/opt/airflow/airflow/models/taskinstance.py", line 966, in 
_run_raw_task
       result = task_copy.execute(context=context)
     File "/opt/airflow/tests/dags/test_on_kill.py", line 33, in execute
       time.sleep(10)
     File "/opt/airflow/airflow/models/taskinstance.py", line 943, in 
signal_handler
       raise AirflowException("Task received SIGTERM signal")
   airflow.exceptions.AirflowException: Task received SIGTERM signal
   [2020-01-25 11:39:00,194] {{taskinstance.py:1171}} ERROR - Failed to send 
email to: None
   [2020-01-25 11:39:00,194] {{taskinstance.py:1171}} ERROR - Failed to send 
email to: None
   [2020-01-25 11:39:00,201] {{taskinstance.py:1172}} ERROR - 'NoneType' object 
has no attribute 'strftime'
   Traceback (most recent call last):
     File "/opt/airflow/airflow/models/taskinstance.py", line 966, in 
_run_raw_task
       result = task_copy.execute(context=context)
     File "/opt/airflow/tests/dags/test_on_kill.py", line 33, in execute
       time.sleep(10)
     File "/opt/airflow/airflow/models/taskinstance.py", line 943, in 
signal_handler
       raise AirflowException("Task received SIGTERM signal")
   airflow.exceptions.AirflowException: Task received SIGTERM signal
   
   During handling of the above exception, another exception occurred:
   
   Traceback (most recent call last):
     File "/opt/airflow/airflow/models/taskinstance.py", line 1166, in 
handle_failure
       self.start_date.strftime('%Y%m%dT%H%M%S'),
   AttributeError: 'NoneType' object has no attribute 'strftime'
   [2020-01-25 11:39:00,201] {{taskinstance.py:1172}} ERROR - 'NoneType' object 
has no attribute 'strftime'
   Traceback (most recent call last):
     File "/opt/airflow/airflow/models/taskinstance.py", line 966, in 
_run_raw_task
       result = task_copy.execute(context=context)
     File "/opt/airflow/tests/dags/test_on_kill.py", line 32, in execute
       os.system('sleep 10')
     File "/opt/airflow/airflow/models/taskinstance.py", line 943, in 
signal_handler
       raise AirflowException("Task received SIGTERM signal")
   airflow.exceptions.AirflowException: Task received SIGTERM signal
   
   During handling of the above exception, another exception occurred:
   
   Traceback (most recent call last):
     File "/opt/airflow/airflow/models/taskinstance.py", line 1166, in 
handle_failure
       self.start_date.strftime('%Y%m%dT%H%M%S'),
   AttributeError: 'NoneType' object has no attribute 'strftime'
   [2020-01-25 11:39:00,257] {{helpers.py:288}} INFO - Process 
psutil.Process(pid=235, status='terminated') (235) terminated with exit code 1
   [2020-01-25 11:39:00,258] {{helpers.py:288}} INFO - Process 
psutil.Process(pid=238, status='terminated') (238) terminated with exit code 
None
   [2020-01-25 11:39:00,259] {{helpers.py:288}} INFO - Process 
psutil.Process(pid=237, status='terminated') (237) terminated with exit code 
None
   [2020-01-25 11:39:00,288] {{helpers.py:288}} INFO - Process 
psutil.Process(pid=236, status='terminated') (236) terminated with exit code 
None
   PASSED
   ```

----------------------------------------------------------------
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]


With regards,
Apache Git Services

Reply via email to