|
7 | 7 | from functools import partial |
8 | 8 | from logging import getLogger |
9 | 9 | from pathlib import Path |
| 10 | +from threading import Lock |
10 | 11 | from typing import Annotated, Any, Optional |
11 | 12 | from urllib.parse import urlparse |
12 | 13 |
|
|
31 | 32 | watchers: dict[str | int, MultigridDirWatcher] = {} |
32 | 33 | rsyncers: dict[str, RSyncer] = {} |
33 | 34 | controllers: dict[int, MultigridController] = {} |
| 35 | +controller_lock = Lock() |
34 | 36 | data_collection_parameters: dict = {} |
35 | 37 | tokens = {} |
36 | 38 |
|
@@ -145,22 +147,26 @@ def check_token(session_id: MurfeySessionID): |
145 | 147 | def setup_multigrid_watcher( |
146 | 148 | session_id: MurfeySessionID, watcher_spec: MultigridWatcherSpec |
147 | 149 | ): |
| 150 | + # Remove dormant controllers from memory |
| 151 | + with controller_lock: |
| 152 | + controllers_to_remove = [ |
| 153 | + sid for sid, controller in controllers.items() if controller.dormant |
| 154 | + ] |
| 155 | + for sid in controllers_to_remove: |
| 156 | + del controllers[sid] |
| 157 | + |
148 | 158 | # Return 'True' if controllers are already set up |
149 | 159 | if controllers.get(session_id) is not None: |
150 | 160 | return {"success": True} |
151 | 161 |
|
152 | | - label = watcher_spec.label |
153 | | - for sid, controller in controllers.items(): |
154 | | - if controller.dormant: |
155 | | - del controllers[sid] |
156 | | - |
157 | 162 | # Load machine config as dictionary |
158 | 163 | machine_config: dict[str, Any] = requests.get( |
159 | 164 | f"{_get_murfey_url()}{url_path_for('session_control.router', 'machine_info_by_instrument', instrument_name=sanitise_nonpath(watcher_spec.instrument_name))}", |
160 | 165 | headers={"Authorization": f"Bearer {tokens[session_id]}"}, |
161 | 166 | ).json() |
162 | 167 |
|
163 | | - # Set up the multigrid controll controller |
| 168 | + # Set up the multigrid controller |
| 169 | + label = watcher_spec.label |
164 | 170 | controllers[session_id] = MultigridController( |
165 | 171 | [], |
166 | 172 | watcher_spec.visit, |
@@ -268,7 +274,9 @@ def finalise_rsyncer(session_id: MurfeySessionID, rsyncer_source: RsyncerSource) |
268 | 274 | @router.post("/sessions/{session_id}/finalise_session") |
269 | 275 | def finalise_session(session_id: MurfeySessionID): |
270 | 276 | watchers[session_id].request_stop() |
| 277 | + logger.debug(f"Stop request sent to multigrid watcher for session {session_id}") |
271 | 278 | controllers[session_id].finalise() |
| 279 | + logger.debug(f"Stop orders sent to multigrid controller for session {session_id} ") |
272 | 280 | return {"success": True} |
273 | 281 |
|
274 | 282 |
|
|
0 commit comments