forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deferrable
GCSObjectExistenceSensorAsync
(apache#28763)
This PR donates the following developed GCSObjectExistenceSensorAsync` in [astronomer-providers](https://github.com/astronomer/astronomer-providers) repo to apache airflow. `GCSObjectExistenceSensorAsync`
- Loading branch information
1 parent
35ad16d
commit 284cd52
Showing
7 changed files
with
402 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# | ||
# 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 aiohttp import ClientSession | ||
|
||
from airflow.providers.google.cloud.hooks.gcs import GCSAsyncHook | ||
from airflow.triggers.base import BaseTrigger, TriggerEvent | ||
|
||
|
||
class GCSBlobTrigger(BaseTrigger): | ||
""" | ||
A trigger that fires and it finds the requested file or folder present in the given bucket. | ||
:param bucket: the bucket in the google cloud storage where the objects are residing. | ||
:param object_name: the file or folder present in the bucket | ||
:param google_cloud_conn_id: reference to the Google Connection | ||
:param poke_interval: polling period in seconds to check for file/folder | ||
""" | ||
|
||
def __init__( | ||
self, | ||
bucket: str, | ||
object_name: str, | ||
poke_interval: float, | ||
google_cloud_conn_id: str, | ||
hook_params: dict[str, Any], | ||
): | ||
super().__init__() | ||
self.bucket = bucket | ||
self.object_name = object_name | ||
self.poke_interval = poke_interval | ||
self.google_cloud_conn_id: str = google_cloud_conn_id | ||
self.hook_params = hook_params | ||
|
||
def serialize(self) -> tuple[str, dict[str, Any]]: | ||
"""Serializes GCSBlobTrigger arguments and classpath.""" | ||
return ( | ||
"airflow.providers.google.cloud.triggers.gcs.GCSBlobTrigger", | ||
{ | ||
"bucket": self.bucket, | ||
"object_name": self.object_name, | ||
"poke_interval": self.poke_interval, | ||
"google_cloud_conn_id": self.google_cloud_conn_id, | ||
"hook_params": self.hook_params, | ||
}, | ||
) | ||
|
||
async def run(self) -> AsyncIterator["TriggerEvent"]: | ||
"""Simple loop until the relevant file/folder is found.""" | ||
try: | ||
hook = self._get_async_hook() | ||
while True: | ||
res = await self._object_exists( | ||
hook=hook, bucket_name=self.bucket, object_name=self.object_name | ||
) | ||
if res == "success": | ||
yield TriggerEvent({"status": "success", "message": res}) | ||
await asyncio.sleep(self.poke_interval) | ||
except Exception as e: | ||
yield TriggerEvent({"status": "error", "message": str(e)}) | ||
return | ||
|
||
def _get_async_hook(self) -> GCSAsyncHook: | ||
return GCSAsyncHook(gcp_conn_id=self.google_cloud_conn_id, **self.hook_params) | ||
|
||
async def _object_exists(self, hook: GCSAsyncHook, bucket_name: str, object_name: str) -> str: | ||
""" | ||
Checks for the existence of a file in Google Cloud Storage. | ||
:param bucket_name: The Google Cloud Storage bucket where the object is. | ||
:param object_name: The name of the blob_name to check in the Google cloud | ||
storage bucket. | ||
""" | ||
async with ClientSession() as s: | ||
client = await hook.get_storage_client(s) | ||
bucket = client.get_bucket(bucket_name) | ||
object_response = await bucket.blob_exists(blob_name=object_name) | ||
if object_response: | ||
return "success" | ||
return "pending" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.