lucafuji commented on a change in pull request #6870: [AIRFLOW-0578] Check
return code
URL: https://github.com/apache/airflow/pull/6870#discussion_r372644672
##########
File path: tests/jobs/test_local_task_job.py
##########
@@ -81,6 +81,110 @@ def test_localtaskjob_essential_attr(self):
check_result_2 = [getattr(job1, attr) is not None for attr in
essential_attr]
self.assertTrue(all(check_result_2))
+ def test_invalid_return_code_with_bash_operator(self):
+ with create_session() as session:
+ dagbag = models.DagBag(
+ dag_folder=TEST_DAG_FOLDER,
+ include_examples=False,
+ )
+ dag_id = 'test_invalid_return_code'
+ valid_task_id = 'test_valid_return_code_bash_operator'
+ invalid_task_id = 'test_invalid_return_code_bash_operator'
+ dag = dagbag.get_dag(dag_id)
+ invalid_task = dag.get_task(invalid_task_id)
+ dr = dag.create_dagrun(run_id="test",
+ state=State.SUCCESS,
+ execution_date=DEFAULT_DATE,
+ start_date=DEFAULT_DATE,
+ session=session)
+ invalid_ti = TI(task=invalid_task, execution_date=DEFAULT_DATE)
+ invalid_ti.refresh_from_db()
+ invalid_ti.state = State.RUNNING
+ invalid_ti.hostname = get_hostname()
+ session.commit()
+
+ invalid_job = LocalTaskJob(task_instance=invalid_ti,
+ ignore_ti_state=True,
+ executor=SequentialExecutor())
+ with self.assertRaisesRegex(AirflowException,
+ "LocalTaskJob process exited with non
zero status"):
+ invalid_job.run()
+
+ invalid_ti = dr.get_task_instance(task_id=invalid_task.task_id,
+ session=session)
+ # task that returns invalid return code should fail.
+ self.assertEqual(invalid_ti.state, State.FAILED)
+ self.assertIsNotNone(invalid_ti.pid)
+
+ valid_task = dag.get_task(valid_task_id)
+ valid_ti = TI(task=valid_task, execution_date=DEFAULT_DATE)
+ valid_ti.refresh_from_db()
+ valid_ti.state = State.RUNNING
+ valid_ti.hostname = get_hostname()
+ session.commit()
+
+ valid_job = LocalTaskJob(task_instance=valid_ti,
+ ignore_ti_state=True,
+ executor=SequentialExecutor())
+ valid_job.run()
+ valid_ti = dr.get_task_instance(task_id=valid_task.task_id,
+ session=session)
+ # task that returns valid return code should succeed.
+ self.assertEqual(valid_ti.state, State.SUCCESS)
+ self.assertIsNotNone(valid_ti.pid)
+
+ def test_invalid_return_code_with_python_operator(self):
+ with create_session() as session:
+ dagbag = models.DagBag(
+ dag_folder=TEST_DAG_FOLDER,
+ include_examples=False,
+ )
+ dag_id = 'test_invalid_return_code'
+ valid_task_id = 'test_valid_return_code_python_operator'
+ invalid_task_id = 'test_invalid_return_code_python_operator'
+ dag = dagbag.get_dag(dag_id)
+ invalid_task = dag.get_task(invalid_task_id)
+ dr = dag.create_dagrun(run_id="test",
+ state=State.SUCCESS,
+ execution_date=DEFAULT_DATE,
+ start_date=DEFAULT_DATE,
+ session=session)
+ invalid_ti = TI(task=invalid_task, execution_date=DEFAULT_DATE)
+ invalid_ti.refresh_from_db()
+ invalid_ti.state = State.RUNNING
+ invalid_ti.hostname = get_hostname()
+ session.commit()
+
+ invalid_job = LocalTaskJob(task_instance=invalid_ti,
+ ignore_ti_state=True,
+ executor=SequentialExecutor())
+ with self.assertRaisesRegex(AirflowException,
+ "LocalTaskJob process exited with non
zero status"):
+ invalid_job.run()
+
+ invalid_ti = dr.get_task_instance(task_id=invalid_task.task_id,
+ session=session)
+ # task that returns invalid return code should fail.
+ self.assertEqual(invalid_ti.state, State.FAILED)
+ self.assertIsNotNone(invalid_ti.pid)
+
+ valid_task = dag.get_task(valid_task_id)
+ valid_ti = TI(task=valid_task, execution_date=DEFAULT_DATE)
+ valid_ti.refresh_from_db()
+ valid_ti.state = State.RUNNING
+ valid_ti.hostname = get_hostname()
+ session.commit()
+
+ valid_job = LocalTaskJob(task_instance=valid_ti,
+ ignore_ti_state=True,
+ executor=SequentialExecutor())
+ valid_job.run()
+ valid_ti = dr.get_task_instance(task_id=valid_task.task_id,
+ session=session)
+ # task that returns valid return code should succeed.
+ self.assertEqual(valid_ti.state, State.RUNNING)
Review comment:
That's the problem of python operator.....if you call sys.exit(0) from the
python operator. It will not do any post processing like setting the state to
succeed which is done in python operator.
So for this case I have following suggestions
1. disallow calling sys.exit in python operator, this can be done by a code
scan before running.
2. running the python operator in another process and having the task state
management in its own process.
I think it will be another PR if we really want to fix this.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services