-
Notifications
You must be signed in to change notification settings - Fork 17.4k
Call listeners for running task instance when a Dag Run state is manually set #69874
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
Open
kacpermuda
wants to merge
2
commits into
apache:main
Choose a base branch
from
kacpermuda:dag-run-ti-listeners
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ | |
| from airflow.timetables.simple import PartitionedAssetTimetable, PartitionedAtRuntime | ||
| from airflow.timetables.trigger import CronPartitionTimetable | ||
| from airflow.utils.session import provide_session | ||
| from airflow.utils.state import DagRunState, State | ||
| from airflow.utils.state import DagRunState, State, TaskInstanceState | ||
| from airflow.utils.types import DagRunTriggeredByType, DagRunType | ||
|
|
||
| from tests_common.test_utils.api_fastapi import _check_dag_run_note, _check_last_log | ||
|
|
@@ -1594,20 +1594,139 @@ def test_patch_dag_run_bad_request(self, test_client): | |
| assert body["detail"][0]["msg"] == "Input should be 'queued', 'success' or 'failed'" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("state", "listener_state"), | ||
| ("state", "expected_dagrun_state"), | ||
| [ | ||
| ("queued", []), | ||
| ("success", [DagRunState.SUCCESS]), | ||
| ("failed", [DagRunState.FAILED]), | ||
| ("queued", None), | ||
| ("success", DagRunState.SUCCESS), | ||
| ("failed", DagRunState.FAILED), | ||
| ], | ||
| ) | ||
| @pytest.mark.usefixtures("configure_git_connection_for_dag_bundle") | ||
| def test_patch_dag_run_notifies_listeners(self, test_client, state, listener_state, listener_manager): | ||
| def test_patch_dag_run_notifies_listeners( | ||
| self, test_client, state, expected_dagrun_state, listener_manager | ||
| ): | ||
| listener = ClassBasedListener() | ||
| listener_manager(listener) | ||
| response = test_client.patch(f"/dags/{DAG1_ID}/dagRuns/{DAG1_RUN1_ID}", json={"state": state}) | ||
| assert response.status_code == 200 | ||
| assert listener.state == listener_state | ||
| assert listener.state == ([expected_dagrun_state] if expected_dagrun_state else []) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("dag_run_state", "expected_ti_state"), | ||
| [ | ||
| ("success", TaskInstanceState.SUCCESS), | ||
| ("failed", TaskInstanceState.FAILED), | ||
| ], | ||
| ) | ||
| @pytest.mark.usefixtures("configure_git_connection_for_dag_bundle") | ||
| def test_patch_dag_run_notifies_ti_listeners_for_running_tasks( | ||
| self, | ||
| test_client, | ||
| dag_maker, | ||
| session, | ||
| listener_manager, | ||
| dag_run_state, | ||
| expected_ti_state, | ||
| ): | ||
|
Comment on lines
+1614
to
+1630
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also cover for teardown tasks, both here and in the other test.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a separate tests for teardown case |
||
| with dag_maker(dag_id="test_ti_listeners", schedule=None, serialized=True): | ||
| EmptyOperator(task_id="t1") | ||
|
|
||
| dr = dag_maker.create_dagrun(state=DagRunState.RUNNING) | ||
| ti = session.scalar( | ||
| select(TaskInstance).where( | ||
| TaskInstance.dag_id == dr.dag_id, | ||
| TaskInstance.run_id == dr.run_id, | ||
| TaskInstance.task_id == "t1", | ||
| ) | ||
| ) | ||
| ti.state = TaskInstanceState.RUNNING | ||
| dag_maker.sync_dagbag_to_db() | ||
| session.commit() | ||
|
|
||
| listener = ClassBasedListener() | ||
| listener_manager(listener) | ||
|
|
||
| response = test_client.patch( | ||
| f"/dags/test_ti_listeners/dagRuns/{dr.run_id}", json={"state": dag_run_state} | ||
| ) | ||
| assert response.status_code == 200 | ||
| # Use `is` for type-safe comparison between TaskInstanceState and DagRunState. | ||
| assert listener.state[0] is expected_ti_state | ||
| assert listener.state[1] is DagRunState(dag_run_state) | ||
| assert len(listener.state) == 2 | ||
|
|
||
| @pytest.mark.usefixtures("configure_git_connection_for_dag_bundle") | ||
| def test_patch_dag_run_does_not_notify_ti_listeners_for_non_running_tasks( | ||
| self, | ||
| test_client, | ||
| dag_maker, | ||
| session, | ||
| listener_manager, | ||
| ): | ||
| with dag_maker(dag_id="test_ti_listeners_queued", schedule=None, serialized=True): | ||
| EmptyOperator(task_id="t1") | ||
|
|
||
| dr = dag_maker.create_dagrun(state=DagRunState.RUNNING) | ||
| ti = session.scalar( | ||
| select(TaskInstance).where( | ||
| TaskInstance.dag_id == dr.dag_id, | ||
| TaskInstance.run_id == dr.run_id, | ||
| TaskInstance.task_id == "t1", | ||
| ) | ||
| ) | ||
| ti.state = TaskInstanceState.QUEUED | ||
| dag_maker.sync_dagbag_to_db() | ||
| session.commit() | ||
|
|
||
| listener = ClassBasedListener() | ||
| listener_manager(listener) | ||
|
|
||
| response = test_client.patch( | ||
| f"/dags/test_ti_listeners_queued/dagRuns/{dr.run_id}", json={"state": "success"} | ||
| ) | ||
| assert response.status_code == 200 | ||
| # Only the dagrun-level hook should have fired; no TI hooks for a non-running task. | ||
| # The list length check distinguishes "only dagrun fired" from "both fired". | ||
| assert listener.state == [DagRunState.SUCCESS] | ||
|
|
||
| @pytest.mark.usefixtures("configure_git_connection_for_dag_bundle") | ||
| def test_patch_dag_run_does_not_notify_ti_listeners_for_running_teardown_tasks( | ||
| self, | ||
| test_client, | ||
| dag_maker, | ||
| session, | ||
| listener_manager, | ||
| ): | ||
| with dag_maker(dag_id="test_ti_listeners_teardown", schedule=None, serialized=True): | ||
| normal = EmptyOperator(task_id="normal") | ||
| teardown = EmptyOperator(task_id="teardown").as_teardown(setups=normal) | ||
| normal >> teardown | ||
|
|
||
| dr = dag_maker.create_dagrun(state=DagRunState.RUNNING) | ||
| for task_id in ("normal", "teardown"): | ||
| ti = session.scalar( | ||
| select(TaskInstance).where( | ||
| TaskInstance.dag_id == dr.dag_id, | ||
| TaskInstance.run_id == dr.run_id, | ||
| TaskInstance.task_id == task_id, | ||
| ) | ||
| ) | ||
| ti.state = TaskInstanceState.RUNNING | ||
| dag_maker.sync_dagbag_to_db() | ||
| session.commit() | ||
|
|
||
| listener = ClassBasedListener() | ||
| listener_manager(listener) | ||
|
|
||
| response = test_client.patch( | ||
| f"/dags/test_ti_listeners_teardown/dagRuns/{dr.run_id}", json={"state": "success"} | ||
| ) | ||
| assert response.status_code == 200 | ||
| # Normal task was killed — its TI listener fires. Teardown task is intentionally skipped. | ||
| # Use `is` for type-safe comparison between TaskInstanceState and DagRunState. | ||
| assert len(listener.state) == 2 | ||
| assert listener.state[0] is TaskInstanceState.SUCCESS | ||
| assert listener.state[1] is DagRunState.SUCCESS | ||
|
|
||
|
|
||
| class TestDeleteDagRun: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about teardown task instances that are left running by design (so they can finish their own cleanup)? They are still included in the
running_tislist returned by_set_dag_run_terminal_state, so the API server will fire a false "success"/"failed" terminal event for a teardown task that is, in fact, still executing.This might've been an untested edge case even before this PR but still flagging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot ! Thanks, excluded teardown tasks now.