Skip to content

Commit

Permalink
[AIRFLOW-1017] get_task_instance shouldn't throw exception when no TI
Browse files Browse the repository at this point in the history
get_task_instance should return None instead of
throwing exception in the case where dagrun does not have the task
instance.

Closes #2178 from aoen/ddavydov--
one_instead_of_first_for_dagrun
  • Loading branch information
aoen committed Mar 22, 2017
1 parent b586bd6 commit b2b9587
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion airflow/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4017,7 +4017,7 @@ def get_task_instance(self, task_id, session=None):
TI.dag_id == self.dag_id,
TI.execution_date == self.execution_date,
TI.task_id == task_id
).one()
).first()

return ti

Expand Down
26 changes: 26 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,32 @@ def test_dagrun_success_conditions(self):
state = dr.update_state()
self.assertEqual(State.FAILED, state)

def test_get_task_instance_on_empty_dagrun(self):
"""
Make sure that a proper value is returned when a dagrun has no task instances
"""
session = settings.Session()

# Any dag will work for this
dag = self.dagbag.get_dag('test_dagrun_short_circuit_false')
now = datetime.datetime.now()

# Don't use create_dagrun since it will create the task instances too which we
# don't want
dag_run = models.DagRun(
dag_id=dag.dag_id,
run_id='manual__' + now.isoformat(),
execution_date=now,
start_date=now,
state=State.RUNNING,
external_trigger=False,
)
session.add(dag_run)
session.commit()

ti = dag_run.get_task_instance('test_short_circuit_false')
self.assertEqual(None, ti)


class DagBagTest(unittest.TestCase):

Expand Down

2 comments on commit b2b9587

@bolkedebruin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When can this issue occur? Ie when does a dag run not have a task instance for a certain task? We verify dag runs against their structure so not getting the task instance is a violation of that structure.

@aoen
Copy link
Contributor Author

@aoen aoen commented on b2b9587 Apr 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can happen if e.g. someone creates a dagrun via ui. It's a good safety measure anyways. We were seeing this crash our scheduler after upgrading to rc 4.

Please sign in to comment.