Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Jan 17, 2025
1 parent 3357798 commit bc5c1a1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from aiofiles.tempfile import TemporaryDirectory as AioTemporaryDirectory
from aiohttp import web
from models_library.projects import ProjectID
from models_library.projects_access import Owner
from models_library.projects_state import ProjectStatus
from servicelib.redis import with_project_locked
Expand All @@ -14,7 +15,7 @@
from .._constants import RQ_PRODUCT_KEY
from .._meta import API_VTAG
from ..login.decorators import login_required
from ..projects.projects_api import retrieve_and_notify_project_locked_state
from ..projects.projects_api import create_user_notification_cb
from ..redis import get_redis_lock_manager_client_sdk
from ..security.decorators import permission_required
from ..users.api import get_user_fullname
Expand Down Expand Up @@ -53,8 +54,8 @@ async def export_project(request: web.Request):
owner=Owner(
user_id=user_id, **await get_user_fullname(request.app, user_id=user_id)
),
notification_cb=retrieve_and_notify_project_locked_state(
user_id, project_uuid, request.app
notification_cb=create_user_notification_cb(
user_id, ProjectID(f"{project_uuid}"), request.app
),
)
async def _() -> tuple[Callable[[], Coroutine[Any, Any, None]], Path]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,16 @@ async def _copy() -> None:
await long_running_task.result()

if needs_lock_source_project:

async def _notification_cb() -> None:
await projects_api.retrieve_and_notify_project_locked_state(
user_id, source_project["uuid"], app
)

await with_project_locked(
get_redis_lock_manager_client_sdk(app),
project_uuid=source_project["uuid"],
status=ProjectStatus.CLONING,
owner=Owner(
user_id=user_id, **await get_user_fullname(app, user_id=user_id)
),
notification_cb=_notification_cb,
notification_cb=projects_api.create_user_notification_cb(
user_id, ProjectID(f"{source_project['uuid']}"), app
),
)(_copy)()
else:
await _copy()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
""" handlers for project states
"""
"""handlers for project states"""

import contextlib
import functools
Expand Down Expand Up @@ -131,7 +129,7 @@ async def open_project(request: web.Request) -> web.Response:

if not await projects_api.try_open_project_for_user(
req_ctx.user_id,
project_uuid=f"{path_params.project_id}",
project_uuid=path_params.project_id,
client_session_id=client_session_id,
app=request.app,
max_number_of_studies_per_user=product.max_open_studies_per_user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1244,9 +1244,18 @@ async def _clean_user_disconnected_clients(
await user_session.remove(PROJECT_ID_KEY)


def create_user_notification_cb(
user_id: UserID, project_uuid: ProjectID, app: web.Application
):
async def _notification_cb() -> None:
await retrieve_and_notify_project_locked_state(user_id, f"{project_uuid}", app)

return _notification_cb


async def try_open_project_for_user(
user_id: UserID,
project_uuid: str,
project_uuid: ProjectID,
client_session_id: str,
app: web.Application,
max_number_of_studies_per_user: int | None,
Expand All @@ -1267,9 +1276,7 @@ async def try_open_project_for_user(
owner=Owner(
user_id=user_id, **await get_user_fullname(app, user_id=user_id)
),
notification_cb=retrieve_and_notify_project_locked_state(
user_id, project_uuid, app
),
notification_cb=create_user_notification_cb(user_id, project_uuid, app),
)
async def _open_project() -> bool:
with managed_resource(user_id, client_session_id, app) as user_session:
Expand All @@ -1296,11 +1303,11 @@ async def _open_project() -> bool:
sessions_with_project: list[
UserSessionID
] = await user_session.find_users_of_resource(
app, PROJECT_ID_KEY, project_uuid
app, PROJECT_ID_KEY, f"{project_uuid}"
)
if not sessions_with_project:
# no one has the project so we assign it
await user_session.add(PROJECT_ID_KEY, project_uuid)
await user_session.add(PROJECT_ID_KEY, f"{project_uuid}")
return True

# Otherwise if this is the only user (NOTE: a session = user_id + client_seesion_id !)
Expand All @@ -1316,7 +1323,7 @@ async def _open_project() -> bool:
app,
):
# steal the project
await user_session.add(PROJECT_ID_KEY, project_uuid)
await user_session.add(PROJECT_ID_KEY, f"{project_uuid}")
await _clean_user_disconnected_clients(
sessions_with_project, app
)
Expand Down Expand Up @@ -1765,7 +1772,7 @@ async def remove_project_dynamic_services(
status=ProjectStatus.CLOSING,
owner=Owner(user_id=user_id, **user_name_data),
notification_cb=(
retrieve_and_notify_project_locked_state(user_id, project_uuid, app)
create_user_notification_cb(user_id, ProjectID(project_uuid), app)
if notify_users
else None
),
Expand Down

0 comments on commit bc5c1a1

Please sign in to comment.