jason810496 commented on code in PR #52917: URL: https://github.com/apache/airflow/pull/52917#discussion_r2188081556
########## providers/redis/src/airflow/providers/redis/triggers/redis_await_message.py: ########## @@ -0,0 +1,69 @@ +# 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 + +from airflow.providers.redis.hooks.redis import RedisHook +from airflow.triggers.base import BaseTrigger, TriggerEvent + + +class AwaitMessageTrigger(BaseTrigger): + """ + A trigger that waits for a message matching specific criteria to arrive in Redis. + + The behavior of this trigger is as follows: + - poll the Redis pubsub for a message, if no message returned, sleep + + :param channels: The channels that should be searched for messages + :param redis_conn_id: The connection object to use, defaults to "redis_default" + :param poll_interval: How long the trigger should sleep after reaching the end of the Redis log + (seconds), defaults to 60 + """ + + def __init__( + self, + channels: list[str] | str, + redis_conn_id: str = "redis_default", + poll_interval: float = 60, + ) -> None: + self.channels = channels + self.redis_conn_id = redis_conn_id + self.poll_interval = poll_interval + + def serialize(self) -> tuple[str, dict[str, Any]]: + return ( + "airflow.providers.redis.triggers.redis_await_message.AwaitMessageTrigger", + { + "channels": self.channels, + "redis_conn_id": self.redis_conn_id, + "poll_interval": self.poll_interval, + }, + ) + + async def run(self): + hook = RedisHook(redis_conn_id=self.redis_conn_id).get_conn().pubsub() + hook.subscribe(self.channels) + + while True: + message = hook.get_message() Review Comment: I think we need `sync_to_async` to avoid blocking call in asyncio. For example: https://github.com/apache/airflow/blob/main/providers/apache/kafka/src/airflow/providers/apache/kafka/triggers/await_message.py#L94-L102 -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
