-
Notifications
You must be signed in to change notification settings - Fork 32
🎨 Use async Redis client in Celery #7430
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
Closed
giancarloromeo
wants to merge
15
commits into
ITISFoundation:master
from
giancarloromeo:use-async-redis-client
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
32be5a5
use async redis client
giancarloromeo 5e0a95e
fix client
giancarloromeo b54a48e
fix setup
giancarloromeo 1d68a6c
remove timeout
giancarloromeo c605724
remove revoked
giancarloromeo 84315ea
clean
giancarloromeo 7800e4b
remove inspect
giancarloromeo 4e959a6
add hack
giancarloromeo ec2b890
add task store protocol
giancarloromeo d9caa19
Merge remote-tracking branch 'upstream/master' into use-async-redis-c…
giancarloromeo 8783d33
remove self
giancarloromeo b23966e
fix exists
giancarloromeo 4e662ea
remove unused
giancarloromeo ca89852
remove unused
giancarloromeo 4a06f20
Merge branch 'master' into use-async-redis-client
giancarloromeo 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
Empty file.
38 changes: 38 additions & 0 deletions
38
services/storage/src/simcore_service_storage/modules/celery/backends/_redis.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,38 @@ | ||
| from typing import Final | ||
|
|
||
| from servicelib.redis._client import RedisClientSDK | ||
|
|
||
| from ..models import TaskContext, TaskData, TaskID, TaskUUID, build_task_id_prefix | ||
|
|
||
| _CELERY_TASK_META_PREFIX: Final[str] = "celery-task-meta-" | ||
| _CELERY_TASK_ID_KEY_SEPARATOR: Final[str] = ":" | ||
| _CELERY_TASK_SCAN_COUNT_PER_BATCH: Final[int] = 10000 | ||
|
|
||
|
|
||
| class RedisTaskStore: | ||
| def __init__(self, redis_client_sdk: RedisClientSDK) -> None: | ||
| self._redis_client_sdk = redis_client_sdk | ||
|
|
||
| async def get_task_uuids(self, task_context: TaskContext) -> set[TaskUUID]: | ||
| search_key = ( | ||
| _CELERY_TASK_META_PREFIX | ||
| + build_task_id_prefix(task_context) | ||
| + _CELERY_TASK_ID_KEY_SEPARATOR | ||
| ) | ||
| keys = set() | ||
| async for key in self._redis_client_sdk.redis.scan_iter( | ||
| match=search_key + "*", count=_CELERY_TASK_SCAN_COUNT_PER_BATCH | ||
| ): | ||
| keys.add(TaskUUID(f"{key}".removeprefix(search_key))) | ||
| return keys | ||
|
|
||
| async def task_exists(self, task_id: TaskID) -> bool: | ||
| n = await self._redis_client_sdk.redis.exists(task_id) | ||
| assert isinstance(n, int) # nosec | ||
| return n > 0 | ||
|
|
||
| async def set_task(self, task_id: TaskID, task_data: TaskData) -> None: | ||
| await self._redis_client_sdk.redis.set( | ||
| task_id, | ||
| task_data.model_dump_json(), | ||
| ) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| from enum import StrEnum, auto | ||
| from typing import Any, Self, TypeAlias | ||
| from typing import Any, Final, Protocol, Self, TypeAlias | ||
| from uuid import UUID | ||
|
|
||
| from models_library.progress_bar import ProgressReport | ||
|
|
@@ -9,6 +9,20 @@ | |
| TaskID: TypeAlias = str | ||
| TaskUUID: TypeAlias = UUID | ||
|
|
||
| _CELERY_TASK_ID_KEY_SEPARATOR: Final[str] = ":" | ||
|
|
||
|
|
||
| def build_task_id_prefix(task_context: TaskContext) -> str: | ||
| return _CELERY_TASK_ID_KEY_SEPARATOR.join( | ||
| [f"{task_context[key]}" for key in sorted(task_context)] | ||
| ) | ||
|
|
||
|
|
||
| def build_task_id(task_context: TaskContext, task_uuid: TaskUUID) -> TaskID: | ||
| return _CELERY_TASK_ID_KEY_SEPARATOR.join( | ||
| [build_task_id_prefix(task_context), f"{task_uuid}"] | ||
| ) | ||
|
|
||
|
|
||
| class TaskState(StrEnum): | ||
| PENDING = auto() | ||
|
|
@@ -18,9 +32,21 @@ class TaskState(StrEnum): | |
| ABORTED = auto() | ||
|
|
||
|
|
||
| class TaskData(BaseModel): | ||
| status: str | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type? |
||
|
|
||
|
|
||
| _TASK_DONE = {TaskState.SUCCESS, TaskState.ERROR, TaskState.ABORTED} | ||
|
|
||
|
|
||
| class TaskStore(Protocol): | ||
| async def get_task_uuids(self, task_context: TaskContext) -> set[TaskUUID]: ... | ||
|
|
||
| async def task_exists(self, task_id: TaskID) -> bool: ... | ||
|
|
||
| async def set_task(self, task_id: TaskID, task_data: TaskData) -> None: ... | ||
|
|
||
|
|
||
| class TaskStatus(BaseModel): | ||
| task_uuid: TaskUUID | ||
| task_state: TaskState | ||
|
|
||
Oops, something went wrong.
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.