def get_task_instance(dag_id, task_id, execution_date):
    """Return the task object identified by the given dag_id and task_id."""

    dagbag = DagBag()

    # Check DAG exists.
    if dag_id not in dagbag.dags:
        error_message = "Dag id {} not found".format(dag_id)
        raise DagNotFound(error_message)

    # Get DAG object and check Task Exists
    dag = dagbag.get_dag(dag_id)
    if not dag.has_task(task_id):
        error_message = 'Task {} not found in dag {}'.format(task_id, dag_id)
        raise TaskNotFound(error_message)

    # Get DagRun object and check that it exists
    dagrun = dag.get_dagrun(execution_date=execution_date)
    if not dagrun:
        error_message = ('Dag Run for date {} not found in dag {}'
                         .format(execution_date, dag_id))
        raise DagRunNotFound(error_message)

    # Get task instance object and check that it exists
    task_instance = dagrun.get_task_instance(task_id)
    if not task_instance:
        error_message = ('Task {} instance for date {} not found'
                         .format(task_id, execution_date))
        raise TaskInstanceNotFound(error_message)

    return task_instance
Exemple #2
0
def _get_ti(
    task: BaseOperator,
    exec_date_or_run_id: str,
    map_index: int,
    *,
    create_if_necessary: bool = False,
    session: Session = NEW_SESSION,
) -> TaskInstance:
    """Get the task instance through DagRun.run_id, if that fails, get the TI the old way"""
    if task.is_mapped:
        if map_index < 0:
            raise RuntimeError("No map_index passed to mapped task")
    elif map_index >= 0:
        raise RuntimeError("map_index passed to non-mapped task")
    dag_run = _get_dag_run(
        dag=task.dag,
        exec_date_or_run_id=exec_date_or_run_id,
        create_if_necessary=create_if_necessary,
        session=session,
    )

    ti_or_none = dag_run.get_task_instance(task.task_id, map_index=map_index, session=session)
    if ti_or_none is None:
        if not create_if_necessary:
            raise TaskInstanceNotFound(
                f"TaskInstance for {task.dag.dag_id}, {task.task_id}, map={map_index} with "
                f"run_id or execution_date of {exec_date_or_run_id!r} not found"
            )
        # TODO: Validate map_index is in range?
        ti = TaskInstance(task, run_id=dag_run.run_id, map_index=map_index)
        ti.dag_run = dag_run
    else:
        ti = ti_or_none
    ti.refresh_from_task(task)
    return ti
Exemple #3
0
def _get_ti(
    task: BaseOperator,
    exec_date_or_run_id: str,
    *,
    create_if_necessary: bool = False,
    session: Session = NEW_SESSION,
) -> TaskInstance:
    """Get the task instance through DagRun.run_id, if that fails, get the TI the old way"""
    dag_run = _get_dag_run(
        dag=task.dag,
        exec_date_or_run_id=exec_date_or_run_id,
        create_if_necessary=create_if_necessary,
        session=session,
    )

    ti_or_none = dag_run.get_task_instance(task.task_id)
    if ti_or_none is None:
        if not create_if_necessary:
            raise TaskInstanceNotFound(
                f"TaskInstance for {task.dag.dag_id}, {task.task_id} with "
                f"run_id or execution_date of {exec_date_or_run_id!r} not found"
            )
        ti = TaskInstance(task, run_id=dag_run.run_id)
        ti.dag_run = dag_run
    else:
        ti = ti_or_none
    ti.refresh_from_task(task)
    return ti
Exemple #4
0
def get_task_instance(dag_id: str, task_id: str, execution_date: datetime) -> TaskInstance:
    """Return the task instance identified by the given dag_id, task_id and execution_date."""
    dag = check_and_get_dag(dag_id, task_id)

    dagrun = check_and_get_dagrun(dag=dag, execution_date=execution_date)
    # Get task instance object and check that it exists
    task_instance = dagrun.get_task_instance(task_id)
    if not task_instance:
        error_message = f'Task {task_id} instance for date {execution_date} not found'
        raise TaskInstanceNotFound(error_message)

    return task_instance
Exemple #5
0
def get_task_instance(
        dag_id, task_id,
        execution_date):  # type: (str, str, datetime) -> TaskInstance
    """Return the task object identified by the given dag_id and task_id."""
    dag = check_and_get_dag(dag_id, task_id)

    dagrun = check_and_get_dagrun(dag=dag, execution_date=execution_date)
    # Get task instance object and check that it exists
    task_instance = dagrun.get_task_instance(task_id)
    if not task_instance:
        error_message = ('Task {} instance for date {} not found'.format(
            task_id, execution_date))
        raise TaskInstanceNotFound(error_message)

    return task_instance