Skip to content

Commit dbeec89

Browse files
Refactor: SKIPPED shouldn't be logged again as SUCCESS. (#14822)
* `SKIPPED` shouldn't be logged again as `SUCCESS`. * `_safe_date` duplicates with `_date_or_empty`. * Borrowed advantage from `_safe_date`.
1 parent f2315bf commit dbeec89

File tree

1 file changed

+20
-45
lines changed

1 file changed

+20
-45
lines changed

airflow/models/taskinstance.py

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,12 +1087,23 @@ def check_and_change_state_before_execution( # pylint: disable=too-many-argumen
10871087
self.log.info("Executing %s on %s", self.task, self.execution_date)
10881088
return True
10891089

1090-
def _date_or_empty(self, attr):
1091-
if hasattr(self, attr):
1092-
date = getattr(self, attr)
1093-
if date:
1094-
return date.strftime('%Y%m%dT%H%M%S')
1095-
return ''
1090+
def _date_or_empty(self, attr: str):
1091+
result = getattr(self, attr, None) # type: datetime
1092+
return result.strftime('%Y%m%dT%H%M%S') if result else ''
1093+
1094+
def _log_state(self, lead_msg: str = ''):
1095+
self.log.info(
1096+
'%sMarking task as %s.'
1097+
+ ' dag_id=%s, task_id=%s,'
1098+
+ ' execution_date=%s, start_date=%s, end_date=%s',
1099+
lead_msg,
1100+
self.state.upper(),
1101+
self.dag_id,
1102+
self.task_id,
1103+
self._date_or_empty('execution_date'),
1104+
self._date_or_empty('start_date'),
1105+
self._date_or_empty('end_date'),
1106+
)
10961107

10971108
@provide_session
10981109
@Sentry.enrich_errors
@@ -1147,15 +1158,6 @@ def _run_raw_task(
11471158
self.log.info(e)
11481159
self.refresh_from_db(lock_for_update=True)
11491160
self.state = State.SKIPPED
1150-
self.log.info(
1151-
'Marking task as SKIPPED. '
1152-
'dag_id=%s, task_id=%s, execution_date=%s, start_date=%s, end_date=%s',
1153-
self.dag_id,
1154-
self.task_id,
1155-
self._date_or_empty('execution_date'),
1156-
self._date_or_empty('start_date'),
1157-
self._date_or_empty('end_date'),
1158-
)
11591161
except AirflowRescheduleException as reschedule_exception:
11601162
self.refresh_from_db()
11611163
self._handle_reschedule(actual_start_date, reschedule_exception, test_mode)
@@ -1181,17 +1183,9 @@ def _run_raw_task(
11811183
finally:
11821184
Stats.incr(f'ti.finish.{task.dag_id}.{task.task_id}.{self.state}')
11831185

1184-
# Recording SUCCESS
1186+
# Recording SKIPPED or SUCCESS
11851187
self.end_date = timezone.utcnow()
1186-
self.log.info(
1187-
'Marking task as SUCCESS. '
1188-
'dag_id=%s, task_id=%s, execution_date=%s, start_date=%s, end_date=%s',
1189-
self.dag_id,
1190-
self.task_id,
1191-
self._date_or_empty('execution_date'),
1192-
self._date_or_empty('start_date'),
1193-
self._date_or_empty('end_date'),
1194-
)
1188+
self._log_state()
11951189
self.set_duration()
11961190
if not test_mode:
11971191
session.add(Log(self.state, self))
@@ -1458,25 +1452,12 @@ def handle_failure(
14581452

14591453
if force_fail or not self.is_eligible_to_retry():
14601454
self.state = State.FAILED
1461-
if force_fail:
1462-
log_message = "Immediate failure requested. Marking task as FAILED."
1463-
else:
1464-
log_message = "Marking task as FAILED."
14651455
email_for_state = task.email_on_failure
14661456
else:
14671457
self.state = State.UP_FOR_RETRY
1468-
log_message = "Marking task as UP_FOR_RETRY."
14691458
email_for_state = task.email_on_retry
14701459

1471-
self.log.info(
1472-
'%s dag_id=%s, task_id=%s, execution_date=%s, start_date=%s, end_date=%s',
1473-
log_message,
1474-
self.dag_id,
1475-
self.task_id,
1476-
self._safe_date('execution_date', '%Y%m%dT%H%M%S'),
1477-
self._safe_date('start_date', '%Y%m%dT%H%M%S'),
1478-
self._safe_date('end_date', '%Y%m%dT%H%M%S'),
1479-
)
1460+
self._log_state('Immediate failure requested. ' if force_fail else '')
14801461
if email_for_state and task.email:
14811462
try:
14821463
self.email_alert(error)
@@ -1502,12 +1483,6 @@ def is_eligible_to_retry(self):
15021483
"""Is task instance is eligible for retry"""
15031484
return self.task.retries and self.try_number <= self.max_tries
15041485

1505-
def _safe_date(self, date_attr, fmt):
1506-
result = getattr(self, date_attr, None)
1507-
if result is not None:
1508-
return result.strftime(fmt)
1509-
return ''
1510-
15111486
@provide_session
15121487
def get_template_context(self, session=None) -> Context: # pylint: disable=too-many-locals
15131488
"""Return TI Context"""

0 commit comments

Comments
 (0)