1+ from __future__ import annotations
2+
13import json
24import logging
35from importlib .metadata import entry_points
4- from typing import Any
6+ from typing import TYPE_CHECKING , Any
57
68from sqlmodel import Session as SQLModelSession , select
79
1113 GridSquareParameters ,
1214 LamellaSiteInfo ,
1315 MillingStepInfo ,
16+ MillingSteps ,
17+ StagePositionInfo ,
1418 StagePositionValues ,
1519)
1620
21+ if TYPE_CHECKING :
22+ from murfey .server .ispyb import TransportManager
23+
24+
1725logger = logging .getLogger ("murfey.workflows.fib.register_milling_progress" )
1826
1927
2028def _ensure_prerequisites (
2129 session_id : int ,
2230 instrument_name : str ,
2331 visit_name : str ,
24- site_info : LamellaSiteInfo ,
32+ project_name : str ,
33+ slot_number : int ,
34+ site_number : int ,
35+ transport_object : TransportManager ,
2536 murfey_db : SQLModelSession ,
2637):
2738 """
2839 Uses the FIB milling metadata provided to create the necessary DataCollectionGroup,
2940 Atlas, and GridSquare placeholders in Murfey if they don't already exist
3041
3142 """
32- # Skip this step if no TransportManager object is configured
33- if _transport_object is None :
34- logger .error ("No TransportManager object was configured" )
35- return None
36-
37- # Early exits if information needed to construct lookup tags are missing
38- if site_info .project_name is None :
39- logger .error ("Could not construct lookup tags; 'project_name' is missing" )
40- return None
41- if site_info .site_number is None :
42- logger .error ("Could not construct lookup tags; 'site_number' is missing" )
43- return None
44- site_number = site_info .site_number
45- if site_info .stage_info is None :
46- logger .error ("Could not construct lookup tags; 'stage_info' is missing" )
47- return None
48- if site_info .stage_info .preparation_site is None :
49- logger .error ("Could not construct lookup tags; 'preparation_site' is missing" )
50- return None
51- if site_info .stage_info .preparation_site .slot_number is None :
52- logger .error ("Could not construct lookup tags; 'slot_number' is missing" )
53- return None
54- slot_number = site_info .stage_info .preparation_site .slot_number
55- if site_info .site_name is None :
56- logger .error ("Could not construct lookup tags; 'site_name' is missing" )
57- return None
58-
5943 # Construct the DataCollectionGroup and GridSquare lookup tags
60- dcg_tag = f"{ site_info . project_name } --slot_{ slot_number } "
44+ dcg_tag = f"{ project_name } --slot_{ slot_number } "
6145
6246 # Determine variables to register data collection group and atlas with
6347 proposal_code = "" .join (char for char in visit_name .split ("-" )[0 ] if char .isalpha ())
@@ -111,7 +95,7 @@ def _ensure_prerequisites(
11195 .where (MurfeyDB .DataCollectionGroup .session_id == session_id )
11296 .where (MurfeyDB .DataCollectionGroup .tag == dcg_tag )
11397 ).one ()
114- grid_square_ispyb_result = _transport_object .do_insert_grid_square (
98+ grid_square_ispyb_result = transport_object .do_insert_grid_square (
11599 atlas_id = dcg_entry .atlas_id ,
116100 grid_square_id = site_number ,
117101 grid_square_parameters = GridSquareParameters (
@@ -180,27 +164,17 @@ def _ensure_prerequisites(
180164
181165
182166def _register_milling_step (
183- site_info : LamellaSiteInfo ,
167+ milling_steps : MillingSteps ,
168+ stage_info : StagePositionInfo ,
184169 grid_square : MurfeyDB .GridSquare ,
170+ transport_object : TransportManager ,
185171 murfey_db : SQLModelSession ,
186172):
187173 """
188174 Registers FIB milling metadata for the current lamella site as MillingStep entries
189175 in ISPyB. If successful, will proceed to create a backup copy of the inserted row
190176 in Murfey.
191177 """
192-
193- # Check if TransportManager has been configured
194- if _transport_object is None :
195- logger .error ("No TransportManager object was configured" )
196- return None
197- # Check information for milling steps is present
198- if site_info .steps is None :
199- logger .error ("No milling step info found in current message" )
200- return None
201- if site_info .stage_info is None :
202- logger .error ("No stage info found in current message" )
203- return None
204178 # Check that GridSquare has ID (for type checking)
205179 if grid_square .id is None :
206180 logger .error ("Current GridSquare entry has no ID" )
@@ -209,7 +183,7 @@ def _register_milling_step(
209183 # Iteratively go through the LamellaSiteInfo model and insert for each step
210184 for steps , stage_name in MILLING_STEP_LOOKUP :
211185 for step_name in steps :
212- step_info : MillingStepInfo | None = site_info . steps .__getattribute__ (
186+ step_info : MillingStepInfo | None = milling_steps .__getattribute__ (
213187 step_name
214188 )
215189 # Early continues if key information is missing
@@ -223,8 +197,8 @@ def _register_milling_step(
223197 logger .debug (f"No step name found for { step_name } " )
224198 continue
225199
226- stage_values : StagePositionValues | None = (
227- site_info . stage_info . __getattribute__ ( stage_name )
200+ stage_values : StagePositionValues | None = stage_info . __getattribute__ (
201+ stage_name
228202 )
229203 if stage_values is None :
230204 stage_values = StagePositionValues ()
@@ -239,7 +213,7 @@ def _register_milling_step(
239213
240214 if milling_step_entry is None :
241215 # Create a new ISPyB entry if no Murfey one is found
242- result = _transport_object .do_insert_milling_step (
216+ result = transport_object .do_insert_milling_step (
243217 # IDs
244218 recipe_name = step_info .recipe_name ,
245219 activity_name = step_info .step_name ,
@@ -301,7 +275,7 @@ def _register_milling_step(
301275 )
302276 else :
303277 # Update the existing ISPyB one if it already exists
304- result = _transport_object .do_update_milling_step (
278+ result = transport_object .do_update_milling_step (
305279 milling_step_id = milling_step_entry .id ,
306280 is_enabled = step_info .is_enabled ,
307281 status = step_info .status ,
@@ -368,20 +342,60 @@ def _register_milling_step(
368342
369343
370344def run (message : dict [str , Any ], murfey_db : SQLModelSession ):
371- # Outer try-finally block to handle database cleanup
345+ # Early exit if no TransportManager was set up
346+ if _transport_object is None :
347+ logger .error ("No TransportManager object was configured" )
348+ return {"success" : False , "requeue" : False }
349+
372350 try :
373- try :
374- # Parse and unpack incoming message
375- session_id = int (message ["session_id" ])
376- site_info = LamellaSiteInfo (** message ["site_info" ])
377- logger .debug (
378- "Received the following FIB metadata for registration:\n "
379- f"{ json .dumps (site_info .model_dump (exclude_none = True ), indent = 2 , default = str )} "
380- )
381- except Exception :
382- logger .error ("Error parsing contents of message" , exc_info = True )
383- return {"success" : False , "requeue" : False }
351+ # Parse and unpack incoming message
352+ session_id = int (message ["session_id" ])
353+ site_info = LamellaSiteInfo (** message ["site_info" ])
354+ logger .debug (
355+ "Received the following FIB metadata for registration:\n "
356+ f"{ json .dumps (site_info .model_dump (exclude_none = True ), indent = 2 , default = str )} "
357+ )
358+ except Exception :
359+ logger .error ("Error parsing contents of message" , exc_info = True )
360+ return {"success" : False , "requeue" : False }
361+
362+ # Early exits if information needed to construct lookup tags are missing
363+ # Project and site values
364+ if site_info .project_name is None :
365+ logger .error ("Could not construct lookup tags; 'project_name' is missing" )
366+ return {"success" : False , "requeue" : False }
367+ project_name = site_info .project_name
368+ if site_info .site_number is None :
369+ logger .error ("Could not construct lookup tags; 'site_number' is missing" )
370+ return {"success" : False , "requeue" : False }
371+ site_number = site_info .site_number
372+ if site_info .site_name is None :
373+ logger .error ("Could not construct lookup tags; 'site_name' is missing" )
374+ return {"success" : False , "requeue" : False }
375+ site_name = site_info .site_name
376+
377+ # Stage information
378+ if site_info .stage_info is None :
379+ logger .error ("Could not construct lookup tags; 'stage_info' is missing" )
380+ return {"success" : False , "requeue" : False }
381+ stage_info = site_info .stage_info
382+ if stage_info .preparation_site is None :
383+ logger .error ("Could not construct lookup tags; 'preparation_site' is missing" )
384+ return {"success" : False , "requeue" : False }
385+ preparation_site = stage_info .preparation_site
386+ if preparation_site .slot_number is None :
387+ logger .error ("Could not construct lookup tags; 'slot_number' is missing" )
388+ return {"success" : False , "requeue" : False }
389+ slot_number = preparation_site .slot_number
390+
391+ # Milling step information
392+ if site_info .steps is None :
393+ logger .error ("No milling step info found in current message" )
394+ return None
395+ milling_steps = site_info .steps
384396
397+ # Outer try-finally block to handle database cleanup
398+ try :
385399 try :
386400 # Load instrument name and visit ID
387401 murfey_session = murfey_db .exec (
@@ -401,7 +415,10 @@ def run(message: dict[str, Any], murfey_db: SQLModelSession):
401415 session_id = session_id ,
402416 instrument_name = instrument_name ,
403417 visit_name = visit_name ,
404- site_info = site_info ,
418+ project_name = project_name ,
419+ slot_number = slot_number ,
420+ site_number = site_number ,
421+ transport_object = _transport_object ,
405422 murfey_db = murfey_db ,
406423 )
407424 except Exception :
@@ -412,15 +429,17 @@ def run(message: dict[str, Any], murfey_db: SQLModelSession):
412429 return {"success" : False , "requeue" : False }
413430 if grid_square_entry is None :
414431 logger .error (
415- f"Could not create GridSquare database entry for site { site_info . site_name } "
432+ f"Could not create GridSquare database entry for site { site_name } "
416433 )
417434 return {"success" : False , "requeue" : False }
418435
419436 try :
420437 # Insert or update MillingStep entries
421438 _register_milling_step (
422- site_info = site_info ,
439+ milling_steps = milling_steps ,
440+ stage_info = stage_info ,
423441 grid_square = grid_square_entry ,
442+ transport_object = _transport_object ,
424443 murfey_db = murfey_db ,
425444 )
426445 except Exception :
@@ -429,9 +448,7 @@ def run(message: dict[str, Any], murfey_db: SQLModelSession):
429448 exc_info = True ,
430449 )
431450 return {"success" : False , "requeue" : False }
432- logger .info (
433- f"Successfully registered milling progress of site { site_info .site_name } "
434- )
451+ logger .info (f"Successfully registered milling progress of site { site_name } " )
435452 return {"success" : True }
436453 finally :
437454 murfey_db .close ()
0 commit comments