matlab-proxy 0.16.0__py3-none-any.whl → 0.17.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of matlab-proxy might be problematic. Click here for more details.

matlab_proxy/app.py CHANGED
@@ -6,7 +6,6 @@ import mimetypes
6
6
  import pkgutil
7
7
  import secrets
8
8
  import sys
9
- import uuid
10
9
 
11
10
  import aiohttp
12
11
  from aiohttp import client_exceptions, web
@@ -97,16 +96,15 @@ def marshal_error(error):
97
96
 
98
97
 
99
98
  async def create_status_response(
100
- app, loadUrl=None, client_id=None, transfer_session=False, is_desktop=False
99
+ app, loadUrl=None, client_id=None, is_active_client=None
101
100
  ):
102
101
  """Send a generic status response about the state of server, MATLAB, MATLAB Licensing and the client session status.
103
102
 
104
103
  Args:
105
104
  app (aiohttp.web.Application): Web Server
106
105
  loadUrl (String, optional): Represents the root URL. Defaults to None.
107
- client_id (String, optional): Represents the unique client_id when concurrency check is enabled. Defaults to None.
108
- transfer_session (Boolean, optional): Represents whether the connection should be transfered or not when concurrency check is enabled. Defaults to False.
109
- is_desktop (Boolean, optional): Represents whether the request is made by the desktop app or some other kernel. Defaults to False.
106
+ client_id (String, optional): Represents the generated client_id when concurrency check is enabled and client does not have a client_id. Defaults to None.
107
+ is_active_client (Boolean, optional): Represents whether the current client is the active_client when concurrency check is enabled. Defaults to None.
110
108
 
111
109
  Returns:
112
110
  JSONResponse: A JSONResponse object containing the generic state of the server, MATLAB, MATLAB Licensing and the client session status.
@@ -124,17 +122,30 @@ async def create_status_response(
124
122
  "wsEnv": state.settings.get("ws_env", ""),
125
123
  }
126
124
 
127
- if IS_CONCURRENCY_CHECK_ENABLED and is_desktop:
128
- if not client_id:
129
- client_id = str(uuid.uuid4())
130
- status["clientId"] = client_id
125
+ if client_id:
126
+ status["clientId"] = client_id
127
+ if is_active_client is not None:
128
+ status["isActiveClient"] = is_active_client
131
129
 
132
- if not state.active_client or transfer_session:
133
- state.active_client = client_id
130
+ return web.json_response(status)
134
131
 
135
- status["isActiveClient"] = True if state.active_client == client_id else False
136
132
 
137
- return web.json_response(status)
133
+ @token_auth.authenticate_access_decorator
134
+ async def clear_client_id(req):
135
+ """API endpoint to reset the active client
136
+
137
+ Args:
138
+ req (HTTPRequest): HTTPRequest Object
139
+
140
+ Returns:
141
+ Response: an empty response in JSON format
142
+ """
143
+ # Sleep for one second prior to clearing the client id to ensure that any remaining get_status responses are fully processed first.
144
+ await asyncio.sleep(1)
145
+ state = req.app["state"]
146
+ state.active_client = None
147
+ # This response is of no relevance to the front-end as the client has already exited
148
+ return web.json_response({})
138
149
 
139
150
 
140
151
  @token_auth.authenticate_access_decorator
@@ -201,15 +212,17 @@ async def get_status(req):
201
212
  JSONResponse: JSONResponse object containing information about the server, MATLAB and MATLAB Licensing.
202
213
  """
203
214
  # The client sends the CLIENT_ID as a query parameter if the concurrency check has been set to true.
215
+ state = req.app["state"]
204
216
  client_id = req.query.get("MWI_CLIENT_ID", None)
205
217
  transfer_session = json.loads(req.query.get("TRANSFER_SESSION", "false"))
206
218
  is_desktop = req.query.get("IS_DESKTOP", False)
207
219
 
220
+ generated_client_id, is_active_client = state.get_session_status(
221
+ is_desktop, client_id, transfer_session
222
+ )
223
+
208
224
  return await create_status_response(
209
- req.app,
210
- client_id=client_id,
211
- transfer_session=transfer_session,
212
- is_desktop=is_desktop,
225
+ req.app, client_id=generated_client_id, is_active_client=is_active_client
213
226
  )
214
227
 
215
228
 
@@ -773,6 +786,8 @@ async def cleanup_background_tasks(app):
773
786
  # Stop any running async tasks
774
787
  logger = mwi.logger.get()
775
788
  tasks = state.tasks
789
+ if state.task_detect_client_status:
790
+ tasks["detect_client_status"] = state.task_detect_client_status
776
791
  for task_name, task in tasks.items():
777
792
  if not task.cancelled():
778
793
  logger.debug(f"Cancelling MWI task: {task_name} : {task} ")
@@ -868,6 +883,7 @@ def create_app(config_name=matlab_proxy.get_default_config_name()):
868
883
  app.router.add_route("GET", f"{base_url}/get_auth_token", get_auth_token)
869
884
  app.router.add_route("GET", f"{base_url}/get_env_config", get_env_config)
870
885
  app.router.add_route("PUT", f"{base_url}/start_matlab", start_matlab)
886
+ app.router.add_route("POST", f"{base_url}/clear_client_id", clear_client_id)
871
887
  app.router.add_route("DELETE", f"{base_url}/stop_matlab", stop_matlab)
872
888
  app.router.add_route("PUT", f"{base_url}/set_licensing_info", set_licensing_info)
873
889
  app.router.add_route("PUT", f"{base_url}/update_entitlement", update_entitlement)
matlab_proxy/app_state.py CHANGED
@@ -9,11 +9,13 @@ import time
9
9
  from collections import deque
10
10
  from datetime import datetime, timedelta, timezone
11
11
  from typing import Final, Optional
12
+ import uuid
12
13
 
13
14
  from matlab_proxy import util
14
15
  from matlab_proxy.constants import (
15
16
  CONNECTOR_SECUREPORT_FILENAME,
16
17
  MATLAB_LOGS_FILE_NAME,
18
+ IS_CONCURRENCY_CHECK_ENABLED,
17
19
  )
18
20
  from matlab_proxy.settings import (
19
21
  get_process_startup_timeout,
@@ -103,6 +105,12 @@ class AppState:
103
105
  # connected to the backend
104
106
  self.active_client = None
105
107
 
108
+ # Used to detect whether the active client is actively sending out request or is inactive
109
+ self.active_client_request_detected = False
110
+
111
+ # An event loop task to handle the detection of client activity
112
+ self.task_detect_client_status = None
113
+
106
114
  def __get_cached_config_file(self):
107
115
  """Get the cached config file
108
116
 
@@ -1304,3 +1312,76 @@ class AppState:
1304
1312
  if err is not None:
1305
1313
  self.error = err
1306
1314
  log_error(logger, err)
1315
+
1316
+ def get_session_status(self, is_desktop, client_id, transfer_session):
1317
+ """
1318
+ Determines the session status for a client, potentially generating a new client ID.
1319
+
1320
+ This function is responsible for managing and tracking the session status of a client.
1321
+ It can generate a new client ID if one is not provided and the conditions are met.
1322
+ It also manages the active client status within the session, especially in scenarios
1323
+ involving desktop clients and when concurrency checks are enabled.
1324
+
1325
+ Args:
1326
+ is_desktop (bool): A flag indicating whether the client is a desktop client.
1327
+ client_id (str or None): The client ID. If None, a new client ID may be generated.
1328
+ transfer_session (bool): Indicates whether the session should be transferred to this client.
1329
+
1330
+ Returns:
1331
+ tuple:
1332
+ - A 2-tuple containing the generated client ID (or None if not generated) and
1333
+ a boolean indicating whether the client is considered the active client.
1334
+ - If concurrency checks are not enabled or the client is not a desktop client, it returns None for both
1335
+ the generated client ID and the active client status.
1336
+ """
1337
+ if IS_CONCURRENCY_CHECK_ENABLED and is_desktop:
1338
+ generated_client_id = None
1339
+ if not client_id:
1340
+ generated_client_id = str(uuid.uuid4())
1341
+ client_id = generated_client_id
1342
+
1343
+ if not self.active_client or transfer_session:
1344
+ self.active_client = client_id
1345
+
1346
+ if not self.task_detect_client_status:
1347
+ # Create the loop to detect the active status of the client
1348
+ loop = util.get_event_loop()
1349
+ self.task_detect_client_status = loop.create_task(
1350
+ self.detect_active_client_status()
1351
+ )
1352
+
1353
+ if self.active_client == client_id:
1354
+ is_active_client = True
1355
+ self.active_client_request_detected = True
1356
+ else:
1357
+ is_active_client = False
1358
+ return generated_client_id, is_active_client
1359
+ return None, None
1360
+
1361
+ async def detect_active_client_status(self, sleep_time=1, max_inactive_count=10):
1362
+ """Detects whether the client is online or not by continuously checking if the active client is making requests
1363
+
1364
+ Args:
1365
+ sleep_time (int): The time in seconds for which the process waits before checking for the next get_status request from the active client.
1366
+ max_inactive_count (int): The maximum number of times the check for the request from the active_client fails before reseting the active client id.
1367
+ """
1368
+ inactive_count = 0
1369
+ while self.active_client:
1370
+ # Check if the get_status request from the active client is received or not
1371
+ await asyncio.sleep(sleep_time)
1372
+ if self.active_client_request_detected:
1373
+ self.active_client_request_detected = False
1374
+ inactive_count = 0
1375
+ else:
1376
+ inactive_count = inactive_count + 1
1377
+ if inactive_count > max_inactive_count:
1378
+ # If no request is received from the active_client for more than 10 seconds then clear the active client id
1379
+ inactive_count = 0
1380
+ self.active_client = None
1381
+ if self.task_detect_client_status:
1382
+ try:
1383
+ # Self cleanup of the task
1384
+ self.task_detect_client_status.cancel()
1385
+ self.task_detect_client_status = None
1386
+ except Exception as e:
1387
+ logger.error("Cleaning of task: 'detect_client_status' failed.")
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "files": {
3
3
  "main.css": "./static/css/main.47712126.css",
4
- "main.js": "./static/js/main.522d83ba.js",
4
+ "main.js": "./static/js/main.5b5ca2f2.js",
5
5
  "static/media/mathworks-pictograms.svg?20181009": "./static/media/mathworks-pictograms.f6f087b008b5a9435f26.svg",
6
6
  "static/media/MATLAB-env-blur.png": "./static/media/MATLAB-env-blur.4fc94edbc82d3184e5cb.png",
7
7
  "static/media/mathworks.svg?20181004": "./static/media/mathworks.80a3218e1ba29f0573fb.svg",
@@ -35,10 +35,10 @@
35
35
  "static/media/gripper.svg": "./static/media/gripper.9defbc5e76d0de8bb6e0.svg",
36
36
  "static/media/arrow.svg": "./static/media/arrow.0c2968b90bd9a64c8c3f.svg",
37
37
  "main.47712126.css.map": "./static/css/main.47712126.css.map",
38
- "main.522d83ba.js.map": "./static/js/main.522d83ba.js.map"
38
+ "main.5b5ca2f2.js.map": "./static/js/main.5b5ca2f2.js.map"
39
39
  },
40
40
  "entrypoints": [
41
41
  "static/css/main.47712126.css",
42
- "static/js/main.522d83ba.js"
42
+ "static/js/main.5b5ca2f2.js"
43
43
  ]
44
44
  }
@@ -1 +1 @@
1
- <!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="./favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="MATLAB"/><meta name="internal_mw_identifier" content="MWI_MATLAB_PROXY_IDENTIFIER"/><link rel="manifest" href="./manifest.json"/><title>MATLAB</title><script defer="defer" src="./static/js/main.522d83ba.js"></script><link href="./static/css/main.47712126.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
1
+ <!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="./favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="MATLAB"/><meta name="internal_mw_identifier" content="MWI_MATLAB_PROXY_IDENTIFIER"/><link rel="manifest" href="./manifest.json"/><title>MATLAB</title><script defer="defer" src="./static/js/main.5b5ca2f2.js"></script><link href="./static/css/main.47712126.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>