-
Notifications
You must be signed in to change notification settings - Fork 14.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AIRFLOW-5644] Simplify TriggerDagRunOperator usage #6317
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,6 @@ | |
) | ||
from airflow.operators.bash_operator import BashOperator | ||
from airflow.operators.check_operator import CheckOperator, ValueCheckOperator | ||
from airflow.operators.dagrun_operator import TriggerDagRunOperator | ||
from airflow.operators.dummy_operator import DummyOperator | ||
from airflow.operators.python_operator import PythonOperator | ||
from airflow.settings import Session | ||
|
@@ -511,18 +510,6 @@ def check_failure(context, test_case=self): | |
start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) | ||
self.assertTrue(data['called']) | ||
|
||
def test_trigger_dagrun(self): | ||
def trigga(_, obj): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I liked this name 😅 |
||
if True: | ||
return obj | ||
|
||
t = TriggerDagRunOperator( | ||
task_id='test_trigger_dagrun', | ||
trigger_dag_id='example_bash_operator', | ||
python_callable=trigga, | ||
dag=self.dag) | ||
t.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) | ||
|
||
def test_dryrun(self): | ||
t = BashOperator( | ||
task_id='test_dryrun', | ||
|
@@ -968,60 +955,6 @@ def test_run_command(self): | |
self.assertEqual(run_command('echo "foo bar"'), 'foo bar\n') | ||
self.assertRaises(AirflowConfigException, run_command, 'bash -c "exit 1"') | ||
|
||
def test_trigger_dagrun_with_execution_date(self): | ||
utc_now = timezone.utcnow() | ||
run_id = 'trig__' + utc_now.isoformat() | ||
|
||
def payload_generator(context, object): # pylint: disable=unused-argument | ||
object.run_id = run_id | ||
return object | ||
|
||
task = TriggerDagRunOperator(task_id='test_trigger_dagrun_with_execution_date', | ||
trigger_dag_id='example_bash_operator', | ||
python_callable=payload_generator, | ||
execution_date=utc_now, | ||
dag=self.dag) | ||
task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) | ||
dag_runs = DagRun.find(dag_id='example_bash_operator', run_id=run_id) | ||
self.assertEqual(len(dag_runs), 1) | ||
dag_run = dag_runs[0] | ||
self.assertEqual(dag_run.execution_date, utc_now) | ||
|
||
def test_trigger_dagrun_with_str_execution_date(self): | ||
utc_now_str = timezone.utcnow().isoformat() | ||
self.assertIsInstance(utc_now_str, (str,)) | ||
run_id = 'trig__' + utc_now_str | ||
|
||
def payload_generator(context, object): # pylint: disable=unused-argument | ||
object.run_id = run_id | ||
return object | ||
|
||
task = TriggerDagRunOperator( | ||
task_id='test_trigger_dagrun_with_str_execution_date', | ||
trigger_dag_id='example_bash_operator', | ||
python_callable=payload_generator, | ||
execution_date=utc_now_str, | ||
dag=self.dag) | ||
task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True) | ||
dag_runs = DagRun.find(dag_id='example_bash_operator', run_id=run_id) | ||
self.assertEqual(len(dag_runs), 1) | ||
dag_run = dag_runs[0] | ||
self.assertEqual(dag_run.execution_date.isoformat(), utc_now_str) | ||
|
||
def test_trigger_dagrun_with_templated_execution_date(self): | ||
task = TriggerDagRunOperator( | ||
task_id='test_trigger_dagrun_with_str_execution_date', | ||
trigger_dag_id='example_bash_operator', | ||
execution_date='{{ execution_date }}', | ||
dag=self.dag) | ||
|
||
self.assertTrue(isinstance(task.execution_date, str)) | ||
self.assertEqual(task.execution_date, '{{ execution_date }}') | ||
|
||
ti = TaskInstance(task=task, execution_date=DEFAULT_DATE) | ||
ti.render_templates() | ||
self.assertEqual(timezone.parse(task.execution_date), DEFAULT_DATE) | ||
|
||
def test_externally_triggered_dagrun(self): | ||
TI = TaskInstance | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to set the execution date on line 58 where you are setting it the first time?
IMO
self.execution_date = timezone.parse(self.execution_date)
is a kind of validation so that should be made in the constructor even if it will be called more often than in theexecute
.then in the
execute
you can just doWDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would like to use it as it is much shorter and simpler.
However, the
execution_date
can be templated. For example{{ execution_date }}
will fail intimezone.parse()
. So, we have to save it first, wait forexecute()
to be called and all variables to be templated, and only then can we calltimezone.parse()
on theexecution_date
:(