subdag.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. """Helper function to generate a DAG and operators given some arguments."""
  19. from __future__ import annotations
  20. # [START subdag]
  21. import pendulum
  22. from airflow.models.dag import DAG
  23. from airflow.operators.empty import EmptyOperator
  24. def subdag(parent_dag_name, child_dag_name, args) -> DAG:
  25. """
  26. Generate a DAG to be used as a subdag.
  27. :param str parent_dag_name: Id of the parent DAG
  28. :param str child_dag_name: Id of the child DAG
  29. :param dict args: Default arguments to provide to the subdag
  30. :return: DAG to use as a subdag
  31. """
  32. dag_subdag = DAG(
  33. dag_id=f"{parent_dag_name}.{child_dag_name}",
  34. default_args=args,
  35. start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
  36. catchup=False,
  37. schedule="@daily",
  38. )
  39. for i in range(5):
  40. EmptyOperator(
  41. task_id=f"{child_dag_name}-task-{i + 1}",
  42. default_args=args,
  43. dag=dag_subdag,
  44. )
  45. return dag_subdag
  46. # [END subdag]