|
| 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 | +"""This module contains a Google Cloud Task sensor.""" |
| 19 | +from typing import TYPE_CHECKING, Optional, Sequence, Union |
| 20 | + |
| 21 | +from airflow.providers.google.cloud.hooks.tasks import CloudTasksHook |
| 22 | +from airflow.sensors.base import BaseSensorOperator |
| 23 | + |
| 24 | +if TYPE_CHECKING: |
| 25 | + from airflow.utils.context import Context |
| 26 | + |
| 27 | + |
| 28 | +class TaskQueueEmptySensor(BaseSensorOperator): |
| 29 | + """ |
| 30 | + Pulls tasks count from a cloud task queue. |
| 31 | + Always waits for queue returning tasks count as 0. |
| 32 | +
|
| 33 | + :param project_id: the Google Cloud project ID for the subscription (templated) |
| 34 | + :param gcp_conn_id: The connection ID to use connecting to Google Cloud. |
| 35 | + :param queue_name: The queue name to for which task empty sensing is required. |
| 36 | + :param impersonation_chain: Optional service account to impersonate using short-term |
| 37 | + credentials, or chained list of accounts required to get the access_token |
| 38 | + of the last account in the list, which will be impersonated in the request. |
| 39 | + If set as a string, the account must grant the originating account |
| 40 | + the Service Account Token Creator IAM role. |
| 41 | + If set as a sequence, the identities from the list must grant |
| 42 | + Service Account Token Creator IAM role to the directly preceding identity, with first |
| 43 | + account from the list granting this role to the originating account (templated). |
| 44 | + """ |
| 45 | + |
| 46 | + template_fields: Sequence[str] = ( |
| 47 | + "project_id", |
| 48 | + "location", |
| 49 | + "queue_name", |
| 50 | + "gcp_conn_id", |
| 51 | + "impersonation_chain", |
| 52 | + ) |
| 53 | + |
| 54 | + def __init__( |
| 55 | + self, |
| 56 | + *, |
| 57 | + location: str, |
| 58 | + project_id: Optional[str] = None, |
| 59 | + queue_name: Optional[str] = None, |
| 60 | + gcp_conn_id: str = "google_cloud_default", |
| 61 | + impersonation_chain: Optional[Union[str, Sequence[str]]] = None, |
| 62 | + **kwargs, |
| 63 | + ) -> None: |
| 64 | + super().__init__(**kwargs) |
| 65 | + self.location = location |
| 66 | + self.project_id = project_id |
| 67 | + self.queue_name = queue_name |
| 68 | + self.gcp_conn_id = gcp_conn_id |
| 69 | + self.impersonation_chain = impersonation_chain |
| 70 | + |
| 71 | + def poke(self, context: "Context") -> bool: |
| 72 | + |
| 73 | + hook = CloudTasksHook( |
| 74 | + gcp_conn_id=self.gcp_conn_id, |
| 75 | + impersonation_chain=self.impersonation_chain, |
| 76 | + ) |
| 77 | + |
| 78 | + # TODO uncomment page_size once https://issuetracker.google.com/issues/155978649?pli=1 gets fixed |
| 79 | + tasks = hook.list_tasks( |
| 80 | + location=self.location, |
| 81 | + queue_name=self.queue_name, |
| 82 | + # page_size=1 |
| 83 | + ) |
| 84 | + |
| 85 | + self.log.info("tasks exhausted in cloud task queue?: %s" % (len(tasks) == 0)) |
| 86 | + |
| 87 | + return len(tasks) == 0 |
0 commit comments