Skip to content

Commit 25b2c7c

Browse files
committed
jobs service and repo layers
1 parent 722f3e5 commit 25b2c7c

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

services/web/server/src/simcore_service_webserver/projects/_controller/projects_rpc.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from servicelib.rabbitmq import RPCRouter
88

99
from ...rabbitmq import get_rabbitmq_rpc_server
10+
from .. import _jobs_service
1011

1112
router = RPCRouter()
1213

@@ -21,11 +22,14 @@ async def mark_project_as_job(
2122
project_uuid: ProjectID,
2223
job_parent_resource_name: str,
2324
) -> None:
24-
msg = (
25-
f"You have reached {__name__} but this feature is still not implemented. "
26-
f"Inputs: {app=}, {product_name=}, {user_id=}, {project_uuid=}, {job_parent_resource_name=}"
25+
26+
await _jobs_service.set_project_as_job(
27+
app,
28+
product_name=product_name,
29+
user_id=user_id,
30+
project_uuid=project_uuid,
31+
job_parent_resource_name=job_parent_resource_name,
2732
)
28-
raise NotImplementedError(msg)
2933

3034

3135
async def register_rpc_routes_on_startup(app: web.Application):
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import logging
2+
3+
from models_library.projects import ProjectID
4+
from simcore_postgres_database.models.projects_to_jobs import projects_to_jobs
5+
from simcore_postgres_database.utils_repos import transaction_context
6+
from sqlalchemy.dialects.postgresql import insert as pg_insert
7+
from sqlalchemy.ext.asyncio import AsyncConnection
8+
9+
from ..db.base_repository import BaseRepository
10+
11+
_logger = logging.getLogger(__name__)
12+
13+
14+
class ProjectJobsRepository(BaseRepository):
15+
16+
async def set_project_as_job(
17+
self,
18+
connection: AsyncConnection | None = None,
19+
*,
20+
project_uuid: ProjectID,
21+
job_parent_resource_name: str,
22+
) -> int:
23+
async with transaction_context(self.engine, connection) as conn:
24+
stmt = (
25+
pg_insert(projects_to_jobs)
26+
.values(
27+
project_uuid=project_uuid,
28+
job_parent_resource_name=job_parent_resource_name,
29+
)
30+
.on_conflict_do_update(
31+
index_elements=["project_uuid", "job_parent_resource_name"],
32+
set_={"job_parent_resource_name": job_parent_resource_name},
33+
)
34+
.returning(projects_to_jobs.c.id)
35+
)
36+
37+
result = await conn.execute(stmt)
38+
row = result.one()
39+
return row.id
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import logging
2+
from typing import Annotated
3+
4+
from aiohttp import web
5+
from models_library.products import ProductName
6+
from models_library.projects import ProjectID
7+
from models_library.users import UserID
8+
from pydantic import AfterValidator, validate_call
9+
10+
from ._access_rights_service import check_user_project_permission
11+
from ._jobs_repository import ProjectJobsRepository
12+
13+
_logger = logging.getLogger(__name__)
14+
15+
16+
def _validate_job_parent_resource_name(value: str) -> str:
17+
if value and not value.startswith("/") and not value.endswith("/") and "/" in value:
18+
return value
19+
msg = "Invalid format: must contain '/' but cannot start or end with '/'"
20+
raise ValueError(msg)
21+
22+
23+
@validate_call(config={"arbitrary_types_allowed": True})
24+
async def set_project_as_job(
25+
app: web.Application,
26+
*,
27+
product_name: ProductName,
28+
user_id: UserID,
29+
project_uuid: ProjectID,
30+
job_parent_resource_name: Annotated[
31+
str, AfterValidator(_validate_job_parent_resource_name)
32+
],
33+
) -> None:
34+
35+
await check_user_project_permission(
36+
app,
37+
project_id=project_uuid,
38+
user_id=user_id,
39+
product_name=product_name,
40+
permission="write",
41+
)
42+
43+
repo = ProjectJobsRepository.create_from_app(app)
44+
45+
projects_to_jobs_id = await repo.set_project_as_job(
46+
project_uuid=project_uuid, job_parent_resource_name=job_parent_resource_name
47+
)
48+
assert projects_to_jobs_id # nosec

0 commit comments

Comments
 (0)