pierrejeambrun commented on code in PR #42975:
URL: https://github.com/apache/airflow/pull/42975#discussion_r1842355598
##########
airflow/api_fastapi/core_api/routes/public/dag_run.py:
##########
@@ -142,3 +147,49 @@ def patch_dag_run(
dag_run = session.get(DagRun, dag_run.id)
return DAGRunResponse.model_validate(dag_run, from_attributes=True)
+
+
+@dag_run_router.post("/{dag_run_id}/clear",
responses=create_openapi_http_exception_doc([401, 403, 404]))
Review Comment:
```suggestion
@dag_run_router.post("/{dag_run_id}/clear",
responses=create_openapi_http_exception_doc([404]))
```
Convention changed here https://github.com/apache/airflow/pull/43932.
401, 403 are now directly inherited from the base router.
##########
airflow/api_fastapi/core_api/routes/public/dag_run.py:
##########
@@ -142,3 +147,49 @@ def patch_dag_run(
dag_run = session.get(DagRun, dag_run.id)
return DAGRunResponse.model_validate(dag_run, from_attributes=True)
+
+
+@dag_run_router.post("/{dag_run_id}/clear",
responses=create_openapi_http_exception_doc([401, 403, 404]))
+def clear_dag_run(
+ dag_id: str,
+ dag_run_id: str,
+ body: DAGRunClearBody,
+ request: Request,
+ session: Annotated[Session, Depends(get_session)],
+) -> TaskInstanceCollectionResponse | DAGRunResponse:
+ dag_run = session.scalar(select(DagRun).filter_by(dag_id=dag_id,
run_id=dag_run_id))
+ if dag_run is None:
+ raise HTTPException(
+ 404, f"The DagRun with dag_id: `{dag_id}` and run_id:
`{dag_run_id}` was not found"
+ )
+
+ dag: DAG = request.app.state.dag_bag.get_dag(dag_id)
+ start_date = dag_run.logical_date
+ end_date = dag_run.logical_date
+
+ if body.dry_run:
+ task_instances = dag.clear(
+ start_date=start_date,
+ end_date=end_date,
+ task_ids=None,
+ only_failed=False,
+ dry_run=True,
+ session=session,
+ )
+
+ return TaskInstanceCollectionResponse(
+ task_instances=[
+ TaskInstanceResponse.model_validate(ti, from_attributes=True)
for ti in task_instances
+ ],
+ total_entries=len(task_instances),
+ )
+ else:
+ dag.clear(
+ start_date=dag_run.start_date,
+ end_date=dag_run.end_date,
+ task_ids=None,
+ only_failed=False,
+ session=session,
+ )
+ dag_run_cleared =
session.scalar(select(DagRun).filter_by(dag_id=dag_id, run_id=dag_run_id))
Review Comment:
```suggestion
dag_run_cleared =
session.scalar(select(DagRun).where(DagRun.id==dag_run.id))
```
Might be easier ?
--
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]