Skip to content

Commit

Permalink
fix log for notifier(instance) without __name__ (apache#41591)
Browse files Browse the repository at this point in the history
Co-authored-by: obarisk <[email protected]>
Co-authored-by: Tzu-ping Chung <[email protected]>
  • Loading branch information
3 people authored Aug 23, 2024
1 parent 6c87886 commit 0cd4686
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
15 changes: 12 additions & 3 deletions airflow/models/taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,12 +1550,21 @@ def _run_finished_callback(
"""
if callbacks:
callbacks = callbacks if isinstance(callbacks, list) else [callbacks]
for callback in callbacks:
log.info("Executing %s callback", callback.__name__)

def get_callback_representation(callback: TaskStateChangeCallback) -> Any:
with contextlib.suppress(AttributeError):
return callback.__name__
with contextlib.suppress(AttributeError):
return callback.__class__.__name__
return callback

for idx, callback in enumerate(callbacks):
callback_repr = get_callback_representation(callback)
log.info("Executing callback at index %d: %s", idx, callback_repr)
try:
callback(context)
except Exception:
log.exception("Error when executing %s callback", callback.__name__) # type: ignore[attr-defined]
log.exception("Error in callback at index %d: %s", idx, callback_repr)


def _log_state(*, task_instance: TaskInstance | TaskInstancePydantic, lead_msg: str = "") -> None:
Expand Down
33 changes: 30 additions & 3 deletions tests/models/test_taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
from airflow.models.taskreschedule import TaskReschedule
from airflow.models.variable import Variable
from airflow.models.xcom import LazyXComSelectSequence, XCom
from airflow.notifications.basenotifier import BaseNotifier
from airflow.operators.bash import BashOperator
from airflow.operators.empty import EmptyOperator
from airflow.operators.python import PythonOperator
Expand Down Expand Up @@ -3421,7 +3422,9 @@ def on_execute_callable(context):
ti.refresh_from_db()
assert ti.state == State.SUCCESS

def test_finished_callbacks_handle_and_log_exception(self, caplog):
def test_finished_callbacks_callable_handle_and_log_exception(self, caplog):
called = completed = False

def on_finish_callable(context):
nonlocal called, completed
called = True
Expand All @@ -3437,8 +3440,32 @@ def on_finish_callable(context):
assert not completed
callback_name = callback_input[0] if isinstance(callback_input, list) else callback_input
callback_name = qualname(callback_name).split(".")[-1]
assert "Executing on_finish_callable callback" in caplog.text
assert "Error when executing on_finish_callable callback" in caplog.text
assert "Executing callback at index 0: on_finish_callable" in caplog.text
assert "Error in callback at index 0: on_finish_callable" in caplog.text

def test_finished_callbacks_notifier_handle_and_log_exception(self, caplog):
class OnFinishNotifier(BaseNotifier):
"""
error captured by BaseNotifier
"""

def __init__(self, error: bool):
super().__init__()
self.raise_error = error

def notify(self, context):
self.execute()

def execute(self) -> None:
if self.raise_error:
raise KeyError

caplog.clear()
callbacks = [OnFinishNotifier(error=False), OnFinishNotifier(error=True)]
_run_finished_callback(callbacks=callbacks, context={})
assert "Executing callback at index 0: OnFinishNotifier" in caplog.text
assert "Executing callback at index 1: OnFinishNotifier" in caplog.text
assert "KeyError" in caplog.text

@pytest.mark.skip_if_database_isolation_mode # Does not work in db isolation mode
@provide_session
Expand Down

0 comments on commit 0cd4686

Please sign in to comment.