Skip to content

Commit 66b4f67

Browse files
committed
moving decorator out of api
1 parent cdabc03 commit 66b4f67

File tree

2 files changed

+60
-54
lines changed

2 files changed

+60
-54
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from collections.abc import Callable, Coroutine
2+
from functools import wraps
3+
from typing import Any, ParamSpec, TypeVar
4+
5+
from aiohttp import web
6+
from models_library.projects_access import Owner
7+
from models_library.projects_state import ProjectStatus
8+
from servicelib.redis._project_lock import with_project_locked
9+
10+
from ..redis import get_redis_lock_manager_client_sdk
11+
from ..users.api import FullNameDict
12+
from .projects_api import retrieve_and_notify_project_locked_state
13+
14+
P = ParamSpec("P")
15+
R = TypeVar("R")
16+
17+
18+
def with_project_locked_and_notify(
19+
app: web.Application,
20+
*,
21+
project_uuid: str,
22+
status: ProjectStatus,
23+
user_id: int,
24+
user_name: FullNameDict,
25+
notify_users: bool,
26+
) -> Callable[
27+
[Callable[P, Coroutine[Any, Any, R]]], Callable[P, Coroutine[Any, Any, R]]
28+
]:
29+
def _decorator(
30+
func: Callable[P, Coroutine[Any, Any, R]],
31+
) -> Callable[P, Coroutine[Any, Any, R]]:
32+
@wraps(func)
33+
async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
34+
@with_project_locked(
35+
get_redis_lock_manager_client_sdk(app),
36+
project_uuid=project_uuid,
37+
status=status,
38+
owner=Owner(user_id=user_id, **user_name),
39+
)
40+
async def _locked_func() -> R:
41+
if notify_users:
42+
await retrieve_and_notify_project_locked_state(
43+
user_id, project_uuid, app
44+
)
45+
46+
return await func(*args, **kwargs)
47+
48+
result = await _locked_func()
49+
if notify_users:
50+
await retrieve_and_notify_project_locked_state(
51+
user_id, project_uuid, app
52+
)
53+
return result
54+
55+
return _wrapper
56+
57+
return _decorator

services/web/server/src/simcore_service_webserver/projects/projects_api.py

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
import json
1414
import logging
1515
from collections import defaultdict
16-
from collections.abc import Callable, Coroutine, Generator
16+
from collections.abc import Generator
1717
from contextlib import suppress
1818
from decimal import Decimal
19-
from functools import wraps
2019
from pprint import pformat
21-
from typing import Any, Final, ParamSpec, TypeVar, cast
20+
from typing import Any, Final, cast
2221
from uuid import UUID, uuid4
2322

2423
from aiohttp import web
@@ -81,11 +80,7 @@
8180
ServiceWaitingForManualInterventionError,
8281
ServiceWasNotFoundError,
8382
)
84-
from servicelib.redis import (
85-
get_project_locked_state,
86-
is_project_locked,
87-
with_project_locked,
88-
)
83+
from servicelib.redis import get_project_locked_state, is_project_locked
8984
from servicelib.redis._decorators import exclusive
9085
from servicelib.utils import fire_and_forget_task, logged_gather
9186
from simcore_postgres_database.models.users import UserRole
@@ -1860,52 +1855,6 @@ async def retrieve_and_notify_project_locked_state(
18601855
)
18611856

18621857

1863-
P = ParamSpec("P")
1864-
R = TypeVar("R")
1865-
1866-
1867-
def with_project_locked_and_notify(
1868-
app: web.Application,
1869-
*,
1870-
project_uuid: str,
1871-
status: ProjectStatus,
1872-
user_id: int,
1873-
user_name: FullNameDict,
1874-
notify_users: bool,
1875-
) -> Callable[
1876-
[Callable[P, Coroutine[Any, Any, R]]], Callable[P, Coroutine[Any, Any, R]]
1877-
]:
1878-
def _decorator(
1879-
func: Callable[P, Coroutine[Any, Any, R]],
1880-
) -> Callable[P, Coroutine[Any, Any, R]]:
1881-
@wraps(func)
1882-
async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
1883-
@with_project_locked(
1884-
get_redis_lock_manager_client_sdk(app),
1885-
project_uuid=project_uuid,
1886-
status=status,
1887-
owner=Owner(user_id=user_id, **user_name),
1888-
)
1889-
async def _locked_func() -> R:
1890-
if notify_users:
1891-
await retrieve_and_notify_project_locked_state(
1892-
user_id, project_uuid, app
1893-
)
1894-
1895-
return await func(*args, **kwargs)
1896-
1897-
result = await _locked_func()
1898-
if notify_users:
1899-
await retrieve_and_notify_project_locked_state(
1900-
user_id, project_uuid, app
1901-
)
1902-
return result
1903-
1904-
return _wrapper
1905-
1906-
return _decorator
1907-
1908-
19091858
async def get_project_inactivity(
19101859
app: web.Application, project_id: ProjectID
19111860
) -> GetProjectInactivityResponse:

0 commit comments

Comments
 (0)