Bowrna commented on code in PR #23516:
URL: https://github.com/apache/airflow/pull/23516#discussion_r867521752
##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -450,6 +451,99 @@ def post_clear_task_instances(*, dag_id: str, session:
Session = NEW_SESSION) ->
)
[email protected]_access(
+ [
+ (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG),
+ (permissions.ACTION_CAN_READ, permissions.RESOURCE_DAG_RUN),
+ (permissions.ACTION_CAN_DELETE, permissions.RESOURCE_TASK_INSTANCE),
+ ],
+)
+@provide_session
+def post_clear_task_dag_run_instances(
+ *,
+ dag_id: str,
+ dag_run_id: str,
+ task_id: str,
+ session: Session = NEW_SESSION,
+) -> APIResponse:
+ """Clear task instances for given dag run."""
+ dag = current_app.dag_bag.get_dag(dag_id)
+ if not dag:
+ error_message = f"Dag id {dag_id} not found"
+ raise NotFound(error_message)
+
+ dag_run: Optional[DR] = (
+ session.query(DR).filter(DR.dag_id == dag_id, DR.run_id ==
dag_run_id).one_or_none()
+ )
+ if dag_run is None:
+ error_message = f'Dag Run id {dag_run_id} not found in dag {dag_id}'
+ raise NotFound(error_message)
+
+ ti = (
+ session.query(TI)
+ .filter(
+ TI.task_id == task_id,
+ TI.dag_id == dag_id,
+ TI.run_id == dag_run_id,
+ )
+ .join(TI.dag_run)
+ .one_or_none()
+ )
+ if ti is None:
+ raise NotFound(title="TaskInstance not found")
+
+ try:
+ data = clear_task_instance_dag_run_form.load(request.json)
+ except ValidationError as err:
+ raise BadRequest(detail=str(err.messages))
+ import pdb;pdb.set_trace()
+ reset_dag_runs = data.pop('reset_dag_runs')
+ dry_run = data.pop('dry_run')
+ only_failed = data.pop('only_failed')
+ recursive = data.pop('recursive')
+ map_indexes = data.pop('map_index')
+
+ task_ids: List[Union[str, Tuple[str, int]]]
+ if map_indexes is None:
+ task_ids = [task_id]
+ else:
+ task_ids = [(task_id, map_index) for map_index in map_indexes]
+
+ dag = dag.partial_subset(
+ task_ids_or_regex=[task_id],
+ include_downstream=data['include_downstream'],
+ include_upstream=data['include_upstream'],
+ )
+ if len(dag.task_ids) > 1:
+ # If we had upstream/downstream etc then also include those!
+ task_ids.extend(tid for tid in dag.task_ids if tid != task_id)
+
+ if dry_run:
+ task_instances = dag.clear(
+ start_date=None,
+ end_date=None,
+ task_ids=task_ids,
+ include_subdags=recursive,
+ include_parentdag=recursive,
+ only_failed=only_failed,
+ dry_run=True,
+ )
+ return task_instance_reference_collection_schema.dump(
+ TaskInstanceReferenceCollection(task_instances=task_instances.all())
+ )
+ else:
+ task_instance = dag.clear(
+ start_date=None,
+ end_date=None,
+ task_ids=task_ids,
+ include_subdags=recursive,
+ include_parentdag=recursive,
+ only_failed=only_failed,
+ )
Review Comment:
@ephraimbuddy Could you tell me what would be the right return data here? In
the old code in `views.py`, it returns the number of task instances cleared. Do
I have to do a similar return here?
--
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]