Skip to content

Commit c767367

Browse files
committed
document usage @pcrespov
1 parent 36d92ff commit c767367

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/service-library/src/servicelib/celery/models.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@
1919

2020

2121
class TaskFilter(BaseModel):
22+
"""
23+
Class for associating metadata with a celery task. The implementation is very flexible and allows "clients" to define their own metadata.
24+
The class exposes a filtering mechanism to list tasks using wildcards.
25+
26+
Example usage:
27+
class MyTaskFilter(TaskFilter):
28+
user_id: int | Wildcard
29+
product_name: int | Wildcard
30+
client_name: str
31+
32+
Listing tasks using the filter `MyTaskFilter(user_id=123, product_name=Wildcard, client_name="my-app")` will return all tasks with
33+
user_id 123, any product_name submitted from my-app.
34+
35+
If the metadata schema is known, the class allows deserializing the metadata (recreate_as_model)
36+
37+
"""
38+
2239
model_config = ConfigDict(extra="allow")
2340

2441
@model_validator(mode="after")
@@ -47,7 +64,7 @@ def get_task_id(self, task_uuid: TaskUUID | Literal["*"]) -> TaskID:
4764
)
4865

4966
@classmethod
50-
def recreate_model(cls, task_id: TaskID, model: type[ModelType]) -> ModelType:
67+
def recreate_as_model(cls, task_id: TaskID, model: type[ModelType]) -> ModelType:
5168
filter_dict = cls.recreate_data(task_id)
5269
return model.model_validate(filter_dict)
5370

packages/service-library/tests/test_celery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class MyModel(BaseModel):
6363
task_filter = TaskFilter.model_validate(mymodel.model_dump())
6464
task_uuid = TaskUUID(_faker.uuid4())
6565
task_id = task_filter.get_task_id(task_uuid)
66-
assert TaskFilter.recreate_model(task_id=task_id, model=MyModel) == mymodel
66+
assert TaskFilter.recreate_as_model(task_id=task_id, model=MyModel) == mymodel
6767

6868

6969
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)