|
| 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 | +"""Example DAG demonstrating the usage of the CustomWeightRule.""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import datetime |
| 23 | + |
| 24 | +import pendulum |
| 25 | +from airflow.example_dags.plugins.decreasing_priority_weight_strategy import DecreasingPriorityStrategy |
| 26 | + |
| 27 | +# [START example_custom_weight_dag] |
| 28 | +from airflow.models.dag import DAG |
| 29 | +from airflow.providers.standard.operators.bash import BashOperator |
| 30 | +from airflow.providers.standard.operators.empty import EmptyOperator |
| 31 | + |
| 32 | +with DAG( |
| 33 | + dag_id="example_custom_weight", |
| 34 | + schedule="0 0 * * *", |
| 35 | + start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), |
| 36 | + catchup=False, |
| 37 | + dagrun_timeout=datetime.timedelta(minutes=60), |
| 38 | + tags=["example", "example2"], |
| 39 | +) as dag: |
| 40 | + start = EmptyOperator( |
| 41 | + task_id="start", |
| 42 | + ) |
| 43 | + |
| 44 | + # provide the class instance |
| 45 | + task_1 = BashOperator(task_id="task_1", bash_command="echo 1", weight_rule=DecreasingPriorityStrategy()) |
| 46 | + |
| 47 | + # or provide the path of the class |
| 48 | + task_2 = BashOperator( |
| 49 | + task_id="task_2", |
| 50 | + bash_command="echo 1", |
| 51 | + weight_rule="airflow.example_dags.plugins.decreasing_priority_weight_strategy.DecreasingPriorityStrategy", |
| 52 | + ) |
| 53 | + |
| 54 | + task_non_custom = BashOperator(task_id="task_non_custom", bash_command="echo 1", priority_weight=2) |
| 55 | + |
| 56 | + start >> [task_1, task_2, task_non_custom] |
| 57 | +# [END example_custom_weight_dag] |
0 commit comments