You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "henry3260 (via GitHub)" <gi...@apache.org> on 2026/05/01 09:45:43 UTC

Re: [PR] airflowctl add tasks clear command [airflow]

henry3260 commented on code in PR #66179:
URL: https://github.com/apache/airflow/pull/66179#discussion_r3172756831


##########
airflow-ctl/src/airflowctl/ctl/commands/task_command.py:
##########
@@ -0,0 +1,60 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+import sys
+
+import rich
+
+from airflowctl.api.client import NEW_API_CLIENT, ClientKind, ServerResponseError, provide_api_client
+from airflowctl.api.datamodels.generated import ClearTaskInstancesBody, TaskInstanceCollectionResponse
+from airflowctl.ctl.console_formatting import AirflowConsole
+
+
+@provide_api_client(kind=ClientKind.CLI)
+def clear(args, api_client=NEW_API_CLIENT) -> None:
+    """
+    Clear task instances for a DAG run via POST ``/api/v2/dags/{dag_id}/clearTaskInstances``.
+
+    The DAG run is selected with ``dag_run_id`` in the JSON body (not the URL path).
+    """
+    body = ClearTaskInstancesBody(
+        dag_run_id=args.dag_run_id,
+        dry_run=args.dry_run,
+        only_failed=args.only_failed,
+        only_running=args.only_running,
+        include_upstream=args.upstream,
+        include_downstream=args.downstream,
+        task_ids=args.task_ids,
+    )
+
+    path = f"dags/{args.dag_id}/clearTaskInstances"
+    try:
+        response = api_client.post(path, json=body.model_dump(mode="json", exclude_none=True))
+        cleared = TaskInstanceCollectionResponse.model_validate_json(response.content)
+    except ServerResponseError as e:
+        rich.print(f"[red]Error clearing tasks for DAG {args.dag_id}, run {args.dag_run_id}: {e}[/red]")
+        sys.exit(1)
+
+    payload = cleared.model_dump(mode="json")
+    cleared_task_instances = payload.get("task_instances", [])

Review Comment:
   I wonder if it would be better to move this logic into `operations.py`, and keep the command focused on composing the request and formatting the output.



##########
airflow-ctl/src/airflowctl/ctl/commands/task_command.py:
##########
@@ -0,0 +1,60 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+import sys
+
+import rich
+
+from airflowctl.api.client import NEW_API_CLIENT, ClientKind, ServerResponseError, provide_api_client
+from airflowctl.api.datamodels.generated import ClearTaskInstancesBody, TaskInstanceCollectionResponse
+from airflowctl.ctl.console_formatting import AirflowConsole
+
+
+@provide_api_client(kind=ClientKind.CLI)
+def clear(args, api_client=NEW_API_CLIENT) -> None:
+    """
+    Clear task instances for a DAG run via POST ``/api/v2/dags/{dag_id}/clearTaskInstances``.
+
+    The DAG run is selected with ``dag_run_id`` in the JSON body (not the URL path).
+    """
+    body = ClearTaskInstancesBody(
+        dag_run_id=args.dag_run_id,
+        dry_run=args.dry_run,
+        only_failed=args.only_failed,
+        only_running=args.only_running,
+        include_upstream=args.upstream,
+        include_downstream=args.downstream,
+        task_ids=args.task_ids,
+    )
+
+    path = f"dags/{args.dag_id}/clearTaskInstances"
+    try:
+        response = api_client.post(path, json=body.model_dump(mode="json", exclude_none=True))
+        cleared = TaskInstanceCollectionResponse.model_validate_json(response.content)
+    except ServerResponseError as e:
+        rich.print(f"[red]Error clearing tasks for DAG {args.dag_id}, run {args.dag_run_id}: {e}[/red]")
+        sys.exit(1)
+
+    payload = cleared.model_dump(mode="json")
+    cleared_task_instances = payload.get("task_instances", [])

Review Comment:
   I think we can rely on `TaskInstanceCollectionResponse` always having `task_instances`.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org