omkar-foss commented on code in PR #44220:
URL: https://github.com/apache/airflow/pull/44220#discussion_r1851898289
##########
airflow/api_fastapi/core_api/routes/public/task_instances.py:
##########
@@ -482,3 +485,88 @@ def get_mapped_task_instance_try_details(
map_index=map_index,
session=session,
)
+
+
+@task_instances_router.post(
+ "/clearTaskInstances",
+ responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
+)
+def post_clear_task_instances(
+ dag_id: str,
+ request: Request,
+ body: ClearTaskInstancesBody,
+ session: Annotated[Session, Depends(get_session)],
+) -> TaskInstanceReferenceCollectionResponse:
+ """Clear task instances."""
+ dag = request.app.state.dag_bag.get_dag(dag_id)
+ if not dag:
+ error_message = f"DAG {dag_id} not found"
+ raise HTTPException(status.HTTP_404_NOT_FOUND, error_message)
+
+ reset_dag_runs = body.reset_dag_runs
+ dry_run = body.dry_run
+ # We always pass dry_run here, otherwise this would try to confirm on the
terminal!
+ dag_run_id = body.dag_run_id
+ future = body.include_future
+ past = body.include_past
+ downstream = body.include_downstream
+ upstream = body.include_upstream
+
+ if dag_run_id is not None:
+ dag_run: DR | None = session.scalar(select(DR).where(DR.dag_id ==
dag_id, DR.run_id == dag_run_id))
+ if dag_run is None:
+ error_message = f"Dag Run id {dag_run_id} not found in dag
{dag_id}"
+ raise HTTPException(status.HTTP_404_NOT_FOUND, error_message)
+ body.start_date = dag_run.logical_date
+ body.end_date = dag_run.logical_date
+
+ if past:
+ body.start_date = None
+
+ if future:
+ body.end_date = None
+
+ task_ids = body.task_ids
+ if task_ids is not None:
+ task_id = [task[0] if isinstance(task, tuple) else task for task in
task_ids]
+ dag = dag.partial_subset(
+ task_ids_or_regex=task_id,
+ include_downstream=downstream,
+ include_upstream=upstream,
+ )
+
+ if len(dag.task_dict) > 1:
+ # If we had upstream/downstream etc then also include those!
+ task_ids.extend(tid for tid in dag.task_dict if tid != task_id)
+
+ task_instances = dag.clear(
+ dry_run=True,
+ task_ids=body.task_ids,
Review Comment:
Missed this! Done, thanks a lot 👍🏽
--
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]