You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/01/15 13:24:05 UTC

[GitHub] [airflow] kaxil commented on a change in pull request #13297: Use DAG context manager in examples (#12894)

kaxil commented on a change in pull request #13297:
URL: https://github.com/apache/airflow/pull/13297#discussion_r558303297



##########
File path: airflow/example_dags/example_bash_operator.py
##########
@@ -29,47 +29,43 @@
     'owner': 'airflow',
 }
 
-dag = DAG(
+with DAG(
     dag_id='example_bash_operator',
     default_args=args,
     schedule_interval='0 0 * * *',
     start_date=days_ago(2),
     dagrun_timeout=timedelta(minutes=60),
     tags=['example', 'example2'],
     params={"example_key": "example_value"},
-)
+) as dag:
 
-run_this_last = DummyOperator(
-    task_id='run_this_last',
-    dag=dag,
-)
+    run_this_last = DummyOperator(
+        task_id='run_this_last',
+    )
+
+    # [START howto_operator_bash]
+    run_this = BashOperator(
+        task_id='run_after_loop',
+        bash_command='echo 1',
+    )
+    # [END howto_operator_bash]
 
-# [START howto_operator_bash]
-run_this = BashOperator(
-    task_id='run_after_loop',
-    bash_command='echo 1',
-    dag=dag,
-)
-# [END howto_operator_bash]
+    run_this >> run_this_last
 
-run_this >> run_this_last
+    for i in range(3):
+        task = BashOperator(
+            task_id='runme_' + str(i),
+            bash_command='echo "{{ task_instance_key_str }}" && sleep 1',
+        )
+        task >> run_this
 
-for i in range(3):
-    task = BashOperator(
-        task_id='runme_' + str(i),
-        bash_command='echo "{{ task_instance_key_str }}" && sleep 1',
-        dag=dag,
+    # [START howto_operator_bash_template]
+    also_run_this = BashOperator(
+        task_id='also_run_this',
+        bash_command='echo "run_id={{ run_id }} | dag_run={{ dag_run }}"',
     )
-    task >> run_this
-
-# [START howto_operator_bash_template]
-also_run_this = BashOperator(
-    task_id='also_run_this',
-    bash_command='echo "run_id={{ run_id }} | dag_run={{ dag_run }}"',
-    dag=dag,
-)
-# [END howto_operator_bash_template]
-also_run_this >> run_this_last
+    # [END howto_operator_bash_template]
+    also_run_this >> run_this_last
 
-if __name__ == "__main__":
-    dag.cli()
+    if __name__ == "__main__":
+        dag.cli()

Review comment:
       ```suggestion
   if __name__ == "__main__":
       dag.cli()
   ```




----------------------------------------------------------------
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:
users@infra.apache.org