-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port 5475 ocean kill the integration if no partition assignment was m…
…ade for the consumer (#392) Description What - If there are two instances sharing the same identifier for the integration type with a Kafka event listener, the second instance will remain outdated and unassigned to any partitions. Therefor, it will not get any resyncs. We want to terminate the redundant instance since Ocean does not support multiple instances concurrently. Why - Ocean does not support multiple instances for the same integration & the kafka topic has only one partition. How - Checking if the integration received any partitions and shutting down the integration if not Type of change New feature (non-breaking change which adds functionality)
- Loading branch information
1 parent
9d6605c
commit 70fd236
Showing
6 changed files
with
132 additions
and
59 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
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,49 @@ | ||
import functools | ||
from typing import Callable, AsyncIterator, Any | ||
|
||
from port_ocean.context.event import event | ||
|
||
AsyncIteratorCallable = Callable[..., AsyncIterator[list[Any]]] | ||
|
||
|
||
def cache_iterator_result( | ||
cache_key: str, | ||
) -> Callable[[AsyncIteratorCallable], AsyncIteratorCallable]: | ||
""" | ||
This decorator caches the results of an async iterator function. It checks if the result is already in the cache | ||
and if not, it fetches the all the data and caches it at ocean.attributes cache the end of the iteration. | ||
The cache will be stored in the scope of the running event and will be removed when the event is finished. | ||
For example, you can use this to cache data coming back from the third-party API to avoid making the same request | ||
multiple times for each kind. | ||
Usage: | ||
```python | ||
@cache_iterator_result("my_cache_key") | ||
async def my_async_iterator_function(): | ||
# Your code here | ||
``` | ||
""" | ||
|
||
def decorator(func: AsyncIteratorCallable) -> AsyncIteratorCallable: | ||
@functools.wraps(func) | ||
async def wrapper(*args: Any, **kwargs: Any) -> Any: | ||
# Check if the result is already in the cache | ||
if cache := event.attributes.get(cache_key): | ||
yield cache | ||
return | ||
|
||
# If not in cache, fetch the data | ||
cached_results = list() | ||
async for result in func(*args, **kwargs): | ||
cached_results.extend(result) | ||
yield result | ||
|
||
# Cache the results | ||
event.attributes[cache_key] = cached_results | ||
return | ||
|
||
return wrapper | ||
|
||
return decorator |
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