Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix log for notifier(instance) without __name__ (#41591) #41699

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3416,7 +3417,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 @@ -3432,8 +3435,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