Skip to content

Commit 9878dc2

Browse files
committed
Rename DagRun scheduling state update method
1 parent 26897c6 commit 9878dc2

6 files changed

Lines changed: 105 additions & 87 deletions

File tree

airflow-core/src/airflow/jobs/scheduler_job_runner.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,13 +1740,13 @@ def _update_dag_run_state_for_paused_dags(self, *, session: Session = NEW_SESSIO
17401740
.group_by(DagRun)
17411741
)
17421742
)
1743-
# Team name should be added before listeners are called in update_state()
1743+
# Team name should be added before listeners are called in schedule_dag_run()
17441744
self._stamp_team_names(paused_runs, session)
17451745
for dag_run in paused_runs:
17461746
dag = self.scheduler_dag_bag.get_dag_for_run(dag_run=dag_run, session=session)
17471747
if dag is not None:
17481748
dag_run.dag = dag
1749-
_, callback_to_run = dag_run.update_state(execute_callbacks=False, session=session)
1749+
_, callback_to_run = dag_run.schedule_dag_run(execute_callbacks=False, session=session)
17501750
if callback_to_run:
17511751
self._send_dag_callbacks_to_processor(dag, callback_to_run)
17521752
except Exception as e: # should not fail the scheduler
@@ -3000,8 +3000,7 @@ def _schedule_dag_run(
30003000

30013001
dag_run.scheduled_by_job_id = self.job.id
30023002

3003-
# TODO[HA]: Rename update_state -> schedule_dag_run, ?? something else?
3004-
schedulable_tis, callback_to_run = dag_run.update_state(session=session, execute_callbacks=False)
3003+
schedulable_tis, callback_to_run = dag_run.schedule_dag_run(session=session, execute_callbacks=False)
30053004

30063005
if dag_run.state in State.finished_dr_states and dag_run.run_type in (
30073006
DagRunType.SCHEDULED,

airflow-core/src/airflow/models/dagrun.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ def _emit_dagrun_span(self, state: DagRunState):
12481248
span.end()
12491249

12501250
@provide_session
1251-
def update_state(
1251+
def schedule_dag_run(
12521252
self, *, session: Session = NEW_SESSION, execute_callbacks: bool = True
12531253
) -> tuple[list[TI], DagCallbackRequest | None]:
12541254
"""
@@ -1446,6 +1446,12 @@ def recalculate(self) -> _UnfinishedStates:
14461446

14471447
return schedulable_tis, callback
14481448

1449+
@provide_session
1450+
def update_state(
1451+
self, *, session: Session = NEW_SESSION, execute_callbacks: bool = True
1452+
) -> tuple[list[TI], DagCallbackRequest | None]:
1453+
return self.schedule_dag_run(session=session, execute_callbacks=execute_callbacks)
1454+
14491455
@provide_session
14501456
def task_instance_scheduling_decisions(self, *, session: Session = NEW_SESSION) -> TISchedulingDecision:
14511457
tis = self.get_task_instances(session=session, state=State.task_states)
@@ -1562,7 +1568,7 @@ def produce_dag_callback(
15621568
def execute_dag_callbacks(
15631569
self, dag: SDKDAG, success: bool = True, relevant_ti: TI | None = None, reason: str = "success"
15641570
):
1565-
"""Only needed for `dag.test` where `execute_callbacks=True` is passed to `update_state`."""
1571+
"""Only needed for `dag.test` where `execute_callbacks=True` is passed to `schedule_dag_run`."""
15661572
from airflow.api_fastapi.execution_api.datamodels.taskinstance import (
15671573
TaskInstance as TIDataModel,
15681574
TIRunContext,
@@ -1762,7 +1768,7 @@ def _emit_true_scheduling_delay_stats_for_finished_state(self, finished_tis: lis
17621768
The true scheduling delay stats is defined as the time when the first
17631769
task in DAG starts minus the expected DAG run datetime.
17641770
1765-
This helper method is used in ``update_state`` when the state of the
1771+
This helper method is used in ``schedule_dag_run`` when the state of the
17661772
DAG run is updated to a completed status (either success or failure).
17671773
It finds the first started task within the DAG, calculates the run's
17681774
expected start time based on the logical date and timetable, and gets

airflow-core/tests/unit/models/test_dag.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ def add_failed_dag_run(dag, id, logical_date):
944944
)
945945
ti_op1 = dr.get_task_instance(task_id=op1.task_id, session=session)
946946
ti_op1.set_state(state=TaskInstanceState.FAILED, session=session)
947-
dr.update_state(session=session)
947+
dr.schedule_dag_run(session=session)
948948

949949
dag_id = "dag_paused_after_limit"
950950
dag = DAG(dag_id, schedule=None, is_paused_upon_creation=False, max_consecutive_failed_dag_runs=2)
@@ -969,9 +969,9 @@ def add_failed_dag_run(dag, id, logical_date):
969969

970970
@staticmethod
971971
def _add_dag_run(scheduler_dag, op1, session, run_id, logical_date, run_after, ti_state, run_state):
972-
"""Create a dagrun, set the task-instance state, and call update_state.
972+
"""Create a dagrun, set the task-instance state, and call schedule_dag_run.
973973
974-
update_state triggers _check_last_n_dagruns_failed only when the run
974+
schedule_dag_run triggers _check_last_n_dagruns_failed only when the run
975975
transitions to FAILED, so it is a no-op for SUCCESS runs.
976976
"""
977977
dr = scheduler_dag.create_dagrun(
@@ -986,7 +986,7 @@ def _add_dag_run(scheduler_dag, op1, session, run_id, logical_date, run_after, t
986986
)
987987
ti = dr.get_task_instance(task_id=op1.task_id, session=session)
988988
ti.set_state(state=ti_state, session=session)
989-
dr.update_state(session=session)
989+
dr.schedule_dag_run(session=session)
990990
return dr
991991

992992
def test_dag_paused_after_limit_orders_by_run_after(self, testing_dag_bundle):
@@ -1014,7 +1014,7 @@ def test_dag_paused_after_limit_orders_by_run_after(self, testing_dag_bundle):
10141014
assert not session.get(DagModel, dag.dag_id).is_paused
10151015

10161016
# Run 1: oldest by run_after but LATEST logical_date — SUCCESS.
1017-
# update_state is a no-op here because _check_last_n_dagruns_failed
1017+
# schedule_dag_run is a no-op here because _check_last_n_dagruns_failed
10181018
# is only invoked on the FAILED branch.
10191019
self._add_dag_run(
10201020
scheduler_dag,

0 commit comments

Comments
 (0)