kaxil commented on code in PR #61274:
URL: https://github.com/apache/airflow/pull/61274#discussion_r2879755565
##########
airflow-core/src/airflow/models/dagrun.py:
##########
@@ -1416,27 +1408,40 @@ def notify_dagrun_state_changed(self, msg: str):
# we can't get all the state changes on SchedulerJob,
# or LocalTaskJob, so we don't want to "falsely advertise" we notify
about that
- @provide_session
- def get_last_ti(self, dag: SerializedDAG, session: Session = NEW_SESSION)
-> TI | None:
- """Get Last TI from the dagrun to build and pass Execution context
object from server to then run callbacks."""
- tis = self.get_task_instances(session=session)
- # tis from a dagrun may not be a part of dag.partial_subset,
- # since dag.partial_subset is a subset of the dag.
- # This ensures that we will only use the accessible TI
- # context for the callback.
- if dag.partial:
- tis = [ti for ti in tis if not ti.state == State.NONE]
- # filter out removed tasks
- tis = natsorted(
- (ti for ti in tis if ti.state != TaskInstanceState.REMOVED),
- key=lambda ti: ti.task_id,
+ def produce_dag_callback(
+ self,
+ dag: SerializedDAG,
+ success: bool = True,
+ relevant_ti: TI | None = None,
+ reason: str = "success",
+ execute=False,
Review Comment:
```suggestion
execute: bool = False,
```
##########
airflow-core/src/airflow/models/dagrun.py:
##########
@@ -1287,25 +1280,24 @@ def recalculate(self) -> _UnfinishedStates:
self.set_state(DagRunState.FAILED)
self.notify_dagrun_state_changed(msg="all_tasks_deadlocked")
- if execute_callbacks and dag.has_on_failure_callback:
- self.handle_dag_callback(
- dag=cast("SDKDAG", dag),
+ if dag.has_on_failure_callback:
+ unfinished_non_schedulable = (ti for ti in unfinished.tis if
ti not in set(schedulable_tis))
+ finished_task_ids = {ti.task_id for ti in finished_tis}
+ blocking_ti = next(
+ iter(
Review Comment:
`iter()` around a generator expression is redundant — generators are already
iterators. `next(genexpr, None)` works directly.
Also, in the deadlock branch (`unfinished.should_schedule and not
are_runnable_tasks`), `schedulable_tis` is guaranteed to be empty (since
`are_runnable_tasks` is `False`), making the `set(schedulable_tis)` filter on
line 1284 a no-op. You could simplify `unfinished_non_schedulable` to just
`unfinished.tis`. If you want to keep it as defensive code, at least
pre-compute the set outside the generator to avoid recreating it per element:
```python
schedulable_set = set(schedulable_tis)
unfinished_non_schedulable = (ti for ti in unfinished.tis if ti not in
schedulable_set)
```
--
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]