Skip to content

Commit febd233

Browse files
authored
Update documentation priority weight (apache#47529)
1 parent a26ea38 commit febd233

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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]

docs/apache-airflow/administration-and-deployment/priority-weight.rst

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,18 @@ registering it in a plugin.
8484
:end-before: [END custom_priority_weight_strategy]
8585

8686

87-
Then to use it, you can create an instance of the custom class and provide it in the ``weight_rule`` parameter
88-
of the task or provide the path of the custom class:
87+
To check if the custom priority weight strategy is already available in Airflow, you can run the bash command
88+
``airflow plugins``. Then to use it, you can create an instance of the custom class and provide it in the
89+
``weight_rule`` parameter of the task or provide the path of the custom class:
8990

90-
.. code-block:: python
91-
92-
from custom_weight_rule_module import CustomPriorityWeightStrategy
91+
.. exampleinclude:: /../../airflow/example_dags/example_custom_weight.py
92+
:language: python
93+
:dedent: 0
94+
:start-after: [START example_custom_weight_dag]
95+
:end-before: [END example_custom_weight_dag]
9396

94-
# provide the class instance
95-
task1 = BashOperator(task_id="task", bash_command="echo 1", weight_rule=CustomPriorityWeightStrategy())
9697

97-
# or provide the path of the class
98-
task1 = BashOperator(
99-
task_id="task",
100-
bash_command="echo 1",
101-
weight_rule="custom_weight_rule_module.CustomPriorityWeightStrategy",
102-
)
98+
After the DAG is running, you can check the ``priority_weight`` parameter on the task to verify that it is using
99+
the custom priority strategy rule.
103100

104101
|experimental|

0 commit comments

Comments
 (0)