airflow.models.dagrun

Module Contents

class airflow.models.dagrun.DagRun[source]

Bases: airflow.models.base.Base, airflow.utils.log.logging_mixin.LoggingMixin

DagRun describes an instance of a Dag. It can be created by the scheduler (for regular runs) or by an external trigger

__tablename__ = dag_run[source]
ID_PREFIX = scheduled__[source]
ID_FORMAT_PREFIX[source]
id[source]
dag_id[source]
execution_date[source]
start_date[source]
end_date[source]
_state[source]
run_id[source]
external_trigger[source]
conf[source]
dag[source]
__table_args__[source]
state[source]
is_backfill[source]
__repr__(self)[source]
get_state(self)[source]
set_state(self, state)[source]
classmethod id_for_date(cls, date, prefix=ID_FORMAT_PREFIX)[source]
refresh_from_db(self, session=None)[source]

Reloads the current dagrun from the database

Parameters

session – database session

static find(dag_id=None, run_id=None, execution_date=None, state=None, external_trigger=None, no_backfills=False, session=None)[source]

Returns a set of dag runs for the given search criteria.

Parameters
  • dag_id (int, list) – the dag_id to find dag runs for

  • run_id (str) – defines the the run id for this dag run

  • execution_date (datetime.datetime) – the execution date

  • state (str) – the state of the dag run

  • external_trigger (bool) – whether this dag run is externally triggered

  • no_backfills (bool) – return no backfills (True), return all (False). Defaults to False

  • session (sqlalchemy.orm.session.Session) – database session

get_task_instances(self, state=None, session=None)[source]

Returns the task instances for this dag run

get_task_instance(self, task_id, session=None)[source]

Returns the task instance specified by task_id for this dag run

Parameters

task_id – the task id

get_dag(self)[source]

Returns the Dag associated with this DagRun.

Returns

DAG

get_previous_dagrun(self, state=None, session=None)[source]

The previous DagRun, if there is one

get_previous_scheduled_dagrun(self, session=None)[source]

The previous, SCHEDULED DagRun, if there is one

update_state(self, session=None)[source]

Determines the overall state of the DagRun based on the state of its TaskInstances.

Returns

ready_tis: the tis that can be scheduled in the current loop

Rtype ready_tis

list[airflow.models.TaskInstance]

_get_ready_tis(self, scheduleable_tasks, finished_tasks, session)[source]
_are_premature_tis(self, unfinished_tasks, finished_tasks, session)[source]
_emit_true_scheduling_delay_stats_for_finished_state(self, finished_tis)[source]

This is a helper method to emit the true scheduling delay stats, which is defined as the time when the first task in DAG starts minus the expected DAG run datetime. This method will be used in the update_state method when the state of the DagRun is updated to a completed status (either success or failure). The method will find the first started task within the DAG and calculate the expected DagRun start time (based on dag.execution_date & dag.schedule_interval), and minus these two values to get the delay. The emitted data may contains outlier (e.g. when the first task was cleared, so the second task’s start_date will be used), but we can get rid of the the outliers on the stats side through the dashboards tooling built. Note, the stat will only be emitted if the DagRun is a scheduler triggered one (i.e. external_trigger is False).

_emit_duration_stats_for_finished_state(self)[source]
verify_integrity(self, session=None)[source]

Verifies the DagRun by checking for removed tasks or tasks that are not in the database yet. It will set state to removed or add the task if required.

static get_run(session, dag_id, execution_date)[source]
Parameters
  • dag_id (unicode) – DAG ID

  • execution_date (datetime) – execution date

Returns

DagRun corresponding to the given dag_id and execution date if one exists. None otherwise.

Return type

airflow.models.DagRun

classmethod get_latest_runs(cls, session)[source]

Returns the latest DagRun for each DAG.

Was this entry helpful?