Skip to content

Commit 31b34e5

Browse files
committed
reusing
1 parent 41adf50 commit 31b34e5

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

services/web/server/src/simcore_service_webserver/director_v2/_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ async def stop_computation(self, project_id: ProjectID, user_id: UserID):
9393
_APP_KEY = f"{__name__}.{DirectorV2RestClient.__name__}"
9494

9595

96-
def get_client(app: web.Application) -> DirectorV2RestClient | None:
97-
app_key: DirectorV2RestClient | None = app.get(_APP_KEY)
98-
return app_key
96+
def set_directorv2_client(app: web.Application, obj: DirectorV2RestClient):
97+
app[_APP_KEY] = obj
9998

10099

101-
def set_client(app: web.Application, obj: DirectorV2RestClient):
102-
app[_APP_KEY] = obj
100+
def get_directorv2_client(app: web.Application) -> DirectorV2RestClient:
101+
app_key: DirectorV2RestClient = app[_APP_KEY]
102+
return app_key

services/web/server/src/simcore_service_webserver/director_v2/_controller/rest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ async def stop_computation(request: web.Request) -> web.Response:
162162
@permission_required("project.read")
163163
@handle_rest_requests_exceptions
164164
async def get_computation(request: web.Request) -> web.Response:
165-
computations = DirectorV2RestClient(request.app)
165+
dv2_client = DirectorV2RestClient(request.app)
166166
run_policy = get_project_run_policy(request.app)
167167
assert run_policy # nosec
168168

@@ -179,7 +179,7 @@ async def get_computation(request: web.Request) -> web.Response:
179179

180180
list_computation_tasks = await asyncio.gather(
181181
*[
182-
computations.get_computation(project_id=pid, user_id=user_id)
182+
dv2_client.get_computation(project_id=pid, user_id=user_id)
183183
for pid in project_ids
184184
]
185185
)

services/web/server/src/simcore_service_webserver/director_v2/_service.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
GroupExtraProperties,
2121
GroupExtraPropertiesRepo,
2222
)
23+
from simcore_service_webserver.director_v2._client import get_directorv2_client
2324

2425
from ..application_settings import get_application_settings
2526
from ..db.plugin import get_database_engine
@@ -39,13 +40,16 @@
3940
#
4041
# PIPELINE RESOURCE ----------------------
4142
#
42-
# TODO: REFACTOR! the client class above and the free functions below are duplicates of the same interface!
4343

4444

4545
@log_decorator(logger=_logger)
4646
async def create_or_update_pipeline(
47-
app: web.Application, user_id: UserID, project_id: ProjectID, product_name: str
47+
app: web.Application,
48+
user_id: UserID,
49+
project_id: ProjectID,
50+
product_name: ProductName,
4851
) -> DataType | None:
52+
# NOTE https://github.com/ITISFoundation/osparc-simcore/issues/7527
4953
settings: DirectorV2Settings = get_plugin_settings(app)
5054

5155
backend_url = settings.base_url / "computations"
@@ -61,7 +65,7 @@ async def create_or_update_pipeline(
6165
product_name=product_name,
6266
),
6367
}
64-
# request to director-v2
68+
6569
try:
6670
computation_task_out = await request_director_v2(
6771
app, "POST", backend_url, expected_status=web.HTTPCreated, data=body
@@ -102,18 +106,14 @@ async def is_pipeline_running(
102106
async def get_computation_task(
103107
app: web.Application, user_id: UserID, project_id: ProjectID
104108
) -> ComputationTask | None:
105-
settings: DirectorV2Settings = get_plugin_settings(app)
106-
backend_url = (settings.base_url / f"computations/{project_id}").update_query(
107-
user_id=int(user_id)
108-
)
109109

110-
# request to director-v2
111110
try:
112-
computation_task_out_dict = await request_director_v2(
113-
app, "GET", backend_url, expected_status=web.HTTPOk
111+
dv2_computation = await get_directorv2_client(app).get_computation(
112+
project_id=project_id, user_id=user_id
114113
)
115-
task_out = ComputationTask.model_validate(computation_task_out_dict)
114+
task_out = ComputationTask.model_validate(dv2_computation, from_attributes=True)
116115
_logger.debug("found computation task: %s", f"{task_out=}")
116+
117117
return task_out
118118
except DirectorServiceError as exc:
119119
if exc.status == status.HTTP_404_NOT_FOUND:
@@ -129,13 +129,8 @@ async def get_computation_task(
129129
async def stop_pipeline(
130130
app: web.Application, *, user_id: PositiveInt, project_id: ProjectID
131131
):
132-
settings: DirectorV2Settings = get_plugin_settings(app)
133-
await request_director_v2(
134-
app,
135-
"POST",
136-
url=settings.base_url / f"computations/{project_id}:stop",
137-
expected_status=web.HTTPAccepted,
138-
data={"user_id": user_id},
132+
await get_directorv2_client(app).stop_computation(
133+
project_id=project_id, user_id=user_id
139134
)
140135

141136

@@ -147,6 +142,8 @@ async def delete_pipeline(
147142
*,
148143
force: bool = True,
149144
) -> None:
145+
# NOTE https://github.com/ITISFoundation/osparc-simcore/issues/7527
146+
150147
settings: DirectorV2Settings = get_plugin_settings(app)
151148
await request_director_v2(
152149
app,
@@ -171,6 +168,7 @@ async def get_batch_tasks_outputs(
171168
project_id: ProjectID,
172169
selection: TasksSelection,
173170
) -> TasksOutputs:
171+
# NOTE https://github.com/ITISFoundation/osparc-simcore/issues/7527
174172
settings: DirectorV2Settings = get_plugin_settings(app)
175173
response_payload = await request_director_v2(
176174
app,
@@ -193,18 +191,13 @@ async def get_batch_tasks_outputs(
193191
return TasksOutputs(**response_payload)
194192

195193

196-
#
197-
# WALLETS ----------------------
198-
#
199-
200-
201194
async def get_wallet_info(
202195
app: web.Application,
203196
*,
204197
product: Product,
205198
user_id: UserID,
206199
project_id: ProjectID,
207-
product_name: str,
200+
product_name: ProductName,
208201
) -> WalletInfo | None:
209202
app_settings = get_application_settings(app)
210203
if not (

services/web/server/src/simcore_service_webserver/director_v2/plugin.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
)
1010

1111
from ..rest.plugin import setup_rest
12-
from . import _client, _controller
12+
from . import _controller
13+
from ._client import DirectorV2RestClient, get_directorv2_client, set_directorv2_client
1314
from ._service_abc import set_project_run_policy
1415
from ._service_abc_default import DefaultProjectRunPolicy
1516

@@ -27,7 +28,9 @@ def setup_director_v2(app: web.Application):
2728
assert app[APP_SETTINGS_KEY].WEBSERVER_DIRECTOR_V2 # nosec
2829

2930
# client to communicate with director-v2 service
30-
_client.set_client(app, _client.DirectorV2RestClient(app))
31+
client = DirectorV2RestClient(app)
32+
set_directorv2_client(app, client)
33+
assert get_directorv2_client(app) == client # nosec
3134

3235
# routes at the web-server app
3336
setup_rest(app)

0 commit comments

Comments
 (0)