|
1 |
| -""" Defines **async** handlers for socket.io server |
| 1 | +""" socket io subsystem |
2 | 2 |
|
3 |
| - SEE https://pypi.python.org/pypi/python-socketio |
4 |
| - SEE http://python-socketio.readthedocs.io/en/latest/ |
5 |
| -""" |
6 |
| -# pylint: disable=C0111 |
7 |
| -# pylint: disable=W0703 |
8 | 3 |
|
| 4 | +""" |
9 | 5 | import logging
|
10 |
| -import socketio |
11 |
| - |
12 |
| -from s3wrapper.s3_client import S3Client |
13 |
| -from simcore_sdk.config.s3 import Config as s3_config |
14 |
| - |
15 |
| -from . import interactive_services_manager |
16 |
| - |
17 |
| -log = logging.getLogger(__file__) |
18 |
| - |
19 |
| -# TODO: separate API from server application! |
20 |
| -SIO = socketio.AsyncServer(async_mode="aiohttp", logging=log) |
21 | 6 |
|
| 7 | +from aiohttp import web |
22 | 8 |
|
23 |
| -@SIO.on("connect") |
24 |
| -def connect(sid, environ): |
25 |
| - # pylint: disable=W0613 |
26 |
| - # environ = WSGI evnironment dictionary |
27 |
| - log.debug("client %s connects", sid) |
28 |
| - interactive_services_manager.session_connect(sid) |
29 |
| - return True |
| 9 | +from .sockets_handlers import sio |
30 | 10 |
|
31 |
| -@SIO.on("startDynamic") |
32 |
| -async def start_dynamic_service(sid, data): |
33 |
| - log.debug("client %s starts dynamic service %s", sid, data) |
34 |
| - try: |
35 |
| - service_key = data["serviceKey"] |
36 |
| - service_version = "latest" |
37 |
| - # if "serviceVersion" in data: |
38 |
| - # service_version = data["serviceVersion"] |
39 |
| - node_id = data["nodeId"] |
40 |
| - result = await interactive_services_manager.start_service(sid, service_key, node_id, service_version) |
41 |
| - await SIO.emit("startDynamic", data=result, room=sid) |
42 |
| - except IOError: |
43 |
| - log.exception("Error emitting results") |
44 |
| - except Exception: |
45 |
| - log.exception("Error while starting service") |
46 | 11 |
|
47 |
| -@SIO.on("stopDynamic") |
48 |
| -async def stop_dynamic_service(sid, data): |
49 |
| - log.debug("client %s stops dynamic service %s", sid, data) |
50 |
| - try: |
51 |
| - node_id = data["nodeId"] |
52 |
| - await interactive_services_manager.stop_service(sid, node_id) |
53 |
| - except Exception: |
54 |
| - log.exception("Error while stopping service") |
| 12 | +log = logging.getLogger(__name__) |
55 | 13 |
|
56 |
| -@SIO.on("presignedUrl") |
57 |
| -async def retrieve_url_for_file(sid, data): |
58 |
| - log.debug("client %s requests S3 url for %s", sid, data) |
59 |
| - _config = s3_config() |
60 |
| - log.debug("S3 endpoint %s", _config.endpoint) |
61 | 14 |
|
| 15 | +def setup(app: web.Application): |
| 16 | + log.debug("Setting up %s ...", __name__) |
62 | 17 |
|
63 |
| - s3_client = S3Client(endpoint=_config.endpoint, |
64 |
| - access_key=_config.access_key, secret_key=_config.secret_key) |
65 |
| - url = s3_client.create_presigned_put_url(_config.bucket_name, data["fileName"]) |
66 |
| - #result = minioClient.presigned_put_object(data["bucketName"], data["fileName"]) |
67 |
| - # Response error is still possible since internally presigned does get |
68 |
| - # bucket location. |
69 |
| - data_out = {} |
70 |
| - data_out["url"] = url |
71 |
| - try: |
72 |
| - await SIO.emit("presignedUrl", data=data_out, room=sid) |
73 |
| - except IOError: |
74 |
| - log.exception("Error emitting results") |
75 |
| - |
76 |
| -@SIO.on("listObjects") |
77 |
| -async def list_S3_objects(sid, data): |
78 |
| - log.debug("client %s requests objects in storage. Extra argument %s", sid, data) |
79 |
| - _config = s3_config() |
80 |
| - |
81 |
| - s3_client = S3Client(endpoint=_config.endpoint, |
82 |
| - access_key=_config.access_key, secret_key=_config.secret_key) |
83 |
| - |
84 |
| - objects = s3_client.list_objects_v2(_config.bucket_name) |
85 |
| - data_out = [] |
86 |
| - location = "simcore.sandbox" |
87 |
| - for obj in objects: |
88 |
| - obj_info = {} |
89 |
| - obj_info["file_uuid"] = obj.bucket_name + "/" + obj.object_name |
90 |
| - obj_info["location"] = location |
91 |
| - obj_info["bucket_name"] = obj.bucket_name |
92 |
| - obj_info["object_name"] = obj.object_name |
93 |
| - obj_info["size"] = obj.size |
94 |
| - data_out.append(obj_info) |
95 |
| - try: |
96 |
| - await SIO.emit("listObjects", data=data_out, room=sid) |
97 |
| - except IOError: |
98 |
| - log.exception("Error emitting results") |
| 18 | + sio.attach(app) |
99 | 19 |
|
100 |
| -@SIO.on("disconnect") |
101 |
| -async def disconnect(sid): |
102 |
| - log.debug("client %s disconnected", sid) |
103 |
| - try: |
104 |
| - await interactive_services_manager.session_disconnected(sid) |
105 |
| - except Exception: |
106 |
| - log.exception("Error while disconnecting client") |
107 | 20 |
|
| 21 | +# alias |
| 22 | +setup_sockets = setup |
108 | 23 |
|
109 |
| -def setup_sio(app): |
110 |
| - log.debug("Setting up %s ...", __name__) |
111 | 24 |
|
112 |
| - SIO.attach(app) |
| 25 | +__all__ = ( |
| 26 | + "setup_sockets" |
| 27 | +) |
0 commit comments