bnspopi opened a new pull request, #77391: URL: https://github.com/apache/airflow/pull/77391
In Airflow 2, `airflow tasks clear` also cleared the tasks the selected `ExternalTaskMarker`s point at, and their downstream tasks, in the other Dags. #75874 brought that back for task instance clears through the REST API and the UI only, so the CLI command clears the selected Dags and nothing else. That is #76779. The command now follows the same targets with `find_dependent_clears`, as the issue suggests: - The targets are looked up from a dry run before anything is cleared, because they are read from the selected task instances. - The confirmation prompt lists the task instances in the other Dags too. - After the selected Dags are cleared, each dependent run is cleared with the same `--only-failed`, `--only-running` and `--keep-task-state` options. - `--exclude-dependent-dags` keeps the clear inside the selected Dags. It takes the place of the old `--exclude-subdags`. - Targets nested deeper than the marker's `recursion_depth` make the command exit with the same message the REST API returns, before anything is cleared. Things reviewers may want to weigh: - The cascade is on by default, as it was in Airflow 2. The REST API defaults `include_dependent_dags` to `include_downstream`; the CLI has no single matching option (`--downstream` only applies with `--task-regex`), so I used an opt-out flag. Happy to change the name or the default. - The CLI has no user, so no per-Dag permission check is applied to the targets; `can_clear` always allows. - The CLI does not take the Dag run locks that the REST route holds between planning and clearing. `clear_dags` does not take them today either. - The command imports `find_dependent_clears` from `airflow.api_fastapi.core_api.services.public.task_instances`. If that helper should live somewhere shared first, I can move it. - `clear_dependent_runs` is not reused because it takes the REST request body; the CLI calls `dag.clear` for each dependent run with the plan from `find_dependent_clears`. Dependent runs are cleared with `run_on_latest_version` left at its default. - No newsfragment yet. I can add one if this counts as a significant change. Tests, run on Linux with SQLite: - New `TestTaskClearDependentDags`: the child Dag's tasks are cleared by default and left alone with `--exclude-dependent-dags`, the prompt counts the tasks in the other Dag, and too-deep targets exit without clearing anything. Without the source change, 4 of the 5 cases fail. - `test_task_clear_reports_nothing_to_clear` now passes `--exclude-dependent-dags`, because it mocks `clear_dags` to return a count instead of task instances. - `airflow-core/tests/unit/cli/commands/test_task_command.py`: 55 passed. Not tested: PostgreSQL and MySQL, and a real `ExternalTaskMarker` run end to end (the tests record the clear target the way the REST API tests do). related: #76779 --- ##### Was generative AI tooling used to co-author this PR? - [X] Yes (Claude Code) Generated-by: Claude Code following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions) 🤖 Generated with [Claude Code](https://claude.com/claude-code) ) -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
