-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Add AwaitMessageTrigger for Redis PubSub #52917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2a1c5a
add redis_pub_sub trigger
Jasperora 8eacb2a
add sync_to_async
Jasperora 01c6fc2
add docs for Redis trigger
Jasperora b02e9e3
fix = length
Jasperora 3270d81
fix - length
Jasperora f41d81d
add Triggers in toctree in index.rst
Jasperora File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
|
|
||
| Connection types <connections> | ||
| Logging <logging/index> | ||
| Triggers <triggers> | ||
|
|
||
| .. toctree:: | ||
| :hidden: | ||
|
|
||
This file contains hidden or 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,29 @@ | ||
| .. 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. | ||
|
|
||
|
|
||
| Redis Triggers | ||
| ============== | ||
|
|
||
| .. _howto/triggers:AwaitMessageTrigger: | ||
|
|
||
| AwaitMessageTrigger | ||
| ------------------- | ||
|
|
||
| The ``AwaitMessageTrigger`` is a trigger that asynchronously waits for a message to arrive on one or more specified Redis PubSub channels. | ||
|
|
||
| For parameter definitions take a look at :class:`~airflow.providers.redis.triggers.redis_await_message.AwaitMessageTrigger`. |
This file contains hidden or 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 hidden or 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
17 changes: 17 additions & 0 deletions
17
providers/redis/src/airflow/providers/redis/triggers/__init__.py
This file contains hidden or 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,17 @@ | ||
| # | ||
| # 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. |
72 changes: 72 additions & 0 deletions
72
providers/redis/src/airflow/providers/redis/triggers/redis_await_message.py
This file contains hidden or 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,72 @@ | ||
| # 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 asgiref.sync import sync_to_async | ||
|
|
||
| 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) | ||
|
|
||
| async_get_message = sync_to_async(hook.get_message) | ||
| while True: | ||
| message = await async_get_message() | ||
|
|
||
| if message and message["type"] == "message": | ||
| yield TriggerEvent(message) | ||
| break | ||
| await asyncio.sleep(self.poll_interval) | ||
This file contains hidden or 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,17 @@ | ||
| # | ||
| # 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. |
90 changes: 90 additions & 0 deletions
90
providers/redis/tests/unit/redis/triggers/test_redis_await_message.py
This file contains hidden or 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,90 @@ | ||
| # | ||
| # 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 unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from airflow.providers.redis.triggers.redis_await_message import AwaitMessageTrigger | ||
|
|
||
|
|
||
| class TestAwaitMessageTrigger: | ||
| def test_trigger_serialization(self): | ||
| trigger = AwaitMessageTrigger( | ||
| channels=["test_channel"], | ||
| redis_conn_id="redis_default", | ||
| poll_interval=30, | ||
| ) | ||
|
|
||
| assert isinstance(trigger, AwaitMessageTrigger) | ||
|
|
||
| classpath, kwargs = trigger.serialize() | ||
|
|
||
| assert classpath == "airflow.providers.redis.triggers.redis_await_message.AwaitMessageTrigger" | ||
| assert kwargs == dict( | ||
| channels=["test_channel"], | ||
| redis_conn_id="redis_default", | ||
| poll_interval=30, | ||
| ) | ||
|
|
||
| @patch("airflow.providers.redis.hooks.redis.RedisHook.get_conn") | ||
| @pytest.mark.asyncio | ||
| async def test_trigger_run_succeed(self, mock_redis_conn): | ||
| trigger = AwaitMessageTrigger( | ||
| channels="test", | ||
| redis_conn_id="redis_default", | ||
| poll_interval=0.0001, | ||
| ) | ||
|
|
||
| mock_redis_conn().pubsub().get_message.return_value = { | ||
| "type": "message", | ||
| "channel": "test", | ||
| "data": "d1", | ||
| } | ||
|
|
||
| trigger_gen = trigger.run() | ||
| task = asyncio.create_task(trigger_gen.__anext__()) | ||
| event = await task | ||
| assert task.done() is True | ||
| assert event.payload["data"] == "d1" | ||
| assert event.payload["channel"] == "test" | ||
| asyncio.get_event_loop().stop() | ||
|
|
||
| @patch("airflow.providers.redis.hooks.redis.RedisHook.get_conn") | ||
| @pytest.mark.asyncio | ||
| async def test_trigger_run_fail(self, mock_redis_conn): | ||
| trigger = AwaitMessageTrigger( | ||
| channels="test", | ||
| redis_conn_id="redis_default", | ||
| poll_interval=0.01, | ||
| ) | ||
|
|
||
| mock_redis_conn().pubsub().get_message.return_value = { | ||
| "type": "subscribe", | ||
| "channel": "test", | ||
| "data": "d1", | ||
| } | ||
|
|
||
| trigger_gen = trigger.run() | ||
| task = asyncio.create_task(trigger_gen.__anext__()) | ||
| await asyncio.sleep(1.0) | ||
| assert task.done() is False | ||
| task.cancel() | ||
| asyncio.get_event_loop().stop() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.