moiseenkov commented on code in PR #29462:
URL: https://github.com/apache/airflow/pull/29462#discussion_r1117213518


##########
airflow/providers/google/cloud/triggers/cloud_storage_transfer_service.py:
##########
@@ -0,0 +1,120 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import asyncio
+from typing import Any, AsyncIterator
+
+from google.api_core.exceptions import GoogleAPIError
+from google.cloud.storage_transfer_v1.types import TransferOperation
+
+from airflow import AirflowException
+from airflow.providers.google.cloud.hooks.cloud_storage_transfer_service 
import (
+    CloudDataTransferServiceAsyncHook,
+)
+from airflow.triggers.base import BaseTrigger, TriggerEvent
+
+
+class CloudStorageTransferServiceCreateJobsTrigger(BaseTrigger):
+    """
+    StorageTransferJobTrigger run on the trigger worker to perform Cloud 
Storage Transfer job
+
+    :param job_names: List of transfer jobs names
+    :param project_id: GCP project id
+    """
+
+    def __init__(self, job_names: list[str], project_id: str | None = None, 
poll_interval: int = 10):
+        super().__init__()
+        self.project_id = project_id
+        self.job_names = job_names
+        self.poll_interval = poll_interval
+
+    def serialize(self) -> tuple[str, dict[str, Any]]:
+        """Serializes StorageTransferJobsTrigger arguments and classpath."""
+        return (
+            
"airflow.providers.google.cloud.triggers.cloud_storage_transfer_service."
+            "CloudStorageTransferServiceCreateJobsTrigger",
+            {
+                "project_id": self.project_id,
+                "job_names": self.job_names,
+                "poll_interval": self.poll_interval,
+            },
+        )
+
+    async def run(self) -> AsyncIterator["TriggerEvent"]:  # type: 
ignore[override]
+        """Gets current data storage transfer jobs and yields a TriggerEvent"""
+        async_hook = self.get_async_hook()
+
+        while True:
+            self.log.info("Attempting to request jobs statuses")
+            jobs_completed_successful = 0
+            try:
+                jobs_pager = await 
async_hook.get_jobs(job_names=self.job_names)
+                jobs, awaitable_operations = [], []
+                async for job in jobs_pager:
+                    operation = async_hook.get_latest_operation(job)
+                    jobs.append(job)
+                    awaitable_operations.append(operation)
+
+                operations: list[TransferOperation] = await 
asyncio.gather(*awaitable_operations)
+
+                for job, operation in zip(jobs, operations):
+                    if operation is None:
+                        yield TriggerEvent(

Review Comment:
   If one of the jobs fails then the trigger yields TriggerEvent with 
information about this failure. After this event handled the DAG run marks 
failed. Each created transfer job remains in the Google Cloud Storage Service. 
All jobs work independently thus failed jobs won't interfere with healthy jobs.



-- 
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.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to