|
| 1 | +# Copyright 2023 Google LLC. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +""" Two Airflow DAGs that demonstrate the mechanism of triggering DAGs with Pub/Sub messages |
| 15 | +
|
| 16 | + Usage: Replace <PROJECT_ID> with the project ID of your project |
| 17 | +""" |
| 18 | + |
| 19 | +# [START composer_pubsub_trigger_dag] |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +from datetime import datetime |
| 23 | +import time |
| 24 | + |
| 25 | +from airflow import DAG |
| 26 | +from airflow import XComArg |
| 27 | +from airflow.operators.python import PythonOperator |
| 28 | +from airflow.operators.trigger_dagrun import TriggerDagRunOperator |
| 29 | +from airflow.providers.google.cloud.operators.pubsub import ( |
| 30 | + PubSubCreateSubscriptionOperator, |
| 31 | + PubSubPullOperator, |
| 32 | +) |
| 33 | + |
| 34 | +PROJECT_ID = "<PROJECT_ID>" |
| 35 | +TOPIC_ID = "dag-topic-trigger" |
| 36 | +SUBSCRIPTION = "trigger_dag_subscription" |
| 37 | + |
| 38 | + |
| 39 | +def handle_messages(pulled_messages, context): |
| 40 | + dag_ids = list() |
| 41 | + for idx, m in enumerate(pulled_messages): |
| 42 | + data = m.message.data.decode('utf-8') |
| 43 | + print(f'message {idx} data is {data}') |
| 44 | + dag_ids.append(data) |
| 45 | + return dag_ids |
| 46 | + |
| 47 | + |
| 48 | +# This DAG will run minutely and handle pub/sub messages by triggering target DAG |
| 49 | +with DAG('trigger_dag', |
| 50 | + start_date=datetime(2021, 1, 1), |
| 51 | + schedule_interval="* * * * *", |
| 52 | + max_active_runs=1, |
| 53 | + catchup=False) as trigger_dag: |
| 54 | + |
| 55 | + # If subscription exists, we will use it. If not - create new one |
| 56 | + subscribe_task = PubSubCreateSubscriptionOperator(task_id="subscribe_task", |
| 57 | + project_id=PROJECT_ID, |
| 58 | + topic=TOPIC_ID, |
| 59 | + subscription=SUBSCRIPTION) |
| 60 | + |
| 61 | + subscription = subscribe_task.output |
| 62 | + |
| 63 | + # Proceed maximum 50 messages in callback function handle_messages |
| 64 | + # Here we acknowledge messages automatically. You can use PubSubHook.acknowledge to acknowledge in downstream tasks |
| 65 | + # https://airflow.apache.org/docs/apache-airflow-providers-google/stable/_api/airflow/providers/google/cloud/hooks/pubsub/index.html#airflow.providers.google.cloud.hooks.pubsub.PubSubHook.acknowledge |
| 66 | + pull_messages_operator = PubSubPullOperator( |
| 67 | + task_id="pull_messages_operator", |
| 68 | + project_id=PROJECT_ID, |
| 69 | + ack_messages=True, |
| 70 | + messages_callback=handle_messages, |
| 71 | + subscription=subscription, |
| 72 | + max_messages=50, |
| 73 | + ) |
| 74 | + |
| 75 | + # Here we use Dynamic Task Mapping to trigger DAGs according to messages content |
| 76 | + # https://airflow.apache.org/docs/apache-airflow/2.3.0/concepts/dynamic-task-mapping.html |
| 77 | + trigger_target_dag = TriggerDagRunOperator\ |
| 78 | + .partial(task_id='trigger_target')\ |
| 79 | + .expand(trigger_dag_id=XComArg(pull_messages_operator)) |
| 80 | + |
| 81 | + (subscribe_task >> pull_messages_operator >> trigger_target_dag) |
| 82 | + |
| 83 | + |
| 84 | +def _some_heavy_task(): |
| 85 | + print('Do some operation...') |
| 86 | + time.sleep(1) |
| 87 | + print('Done!') |
| 88 | + |
| 89 | + |
| 90 | +# Simple target DAG |
| 91 | +with DAG( |
| 92 | + 'target_dag', |
| 93 | + start_date=datetime(2022, 1, 1), |
| 94 | + # Not scheduled, trigger only |
| 95 | + schedule_interval=None, |
| 96 | + catchup=False) as target_dag: |
| 97 | + |
| 98 | + some_heavy_task = PythonOperator(task_id='some_heavy_task', |
| 99 | + python_callable=_some_heavy_task) |
| 100 | + |
| 101 | + (some_heavy_task) |
| 102 | +# [END composer_pubsub_trigger_dag] |
0 commit comments