amoghrajesh commented on code in PR #66463:
URL: https://github.com/apache/airflow/pull/66463#discussion_r3231894237


##########
airflow-core/tests/unit/state/test_metastore.py:
##########


Review Comment:
   Added `test_custom_backend_is_skipped` which mocks a StateBackend, asserts 
the "Custom backend configured" message is printed, and verifies no cleanup() 
is called.



##########
airflow-core/src/airflow/cli/commands/state_store_command.py:
##########
@@ -0,0 +1,50 @@
+# 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 logging
+
+from airflow.state import get_state_backend
+from airflow.state.metastore import MetastoreStateBackend
+
+log = logging.getLogger(__name__)
+
+# Other state operations (list, get, delete per key) will be added here in the 
future.
+
+
+def cleanup(args) -> None:
+    """Remove expired task state rows via the configured state backend."""
+    backend = get_state_backend()
+
+    if args.dry_run:
+        if isinstance(backend, MetastoreStateBackend):
+            summary = backend._summary_dry_run_()
+            expired = summary["expired"]
+            if not expired:
+                print("Nothing to delete.")
+                return
+            print(f"Would delete {len(expired)} task state row(s):\n")
+            for dag_id, run_id, task_id, map_index, key in expired:
+                print(
+                    f"  Dag {dag_id!r}, run {run_id!r}, task {task_id!r}, 
map_index {map_index!r}, key {key!r}"
+                )
+        else:
+            print("Custom backend configured — cannot preview rows.")

Review Comment:
   Cool, what do you think about this: I renamed the command to 
`cleanup-task-states` and updated the description to explicitly say "Only 
applies when MetastoreStateBackend is configured; custom backends are skipped." 
Also moved the backend check to a top-level early return so the limitation is 
enforced before any dry-run logic.



##########
airflow-core/src/airflow/cli/cli_config.py:
##########
@@ -2108,6 +2115,11 @@ class GroupCommand(NamedTuple):
         help="Display providers",
         subcommands=PROVIDERS_COMMANDS,
     ),
+    GroupCommand(
+        name="state-store",
+        help="Manage task and asset state storage",

Review Comment:
   Renamed the command to `cleanup-task-states` so it's scoped correctly from 
the start. The group help is now accurate since we only have task state 
management. Asset state retention can be added as a separate 
`cleanup-asset-states` command when we get there.
   



-- 
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]

Reply via email to