@@ -34,6 +34,7 @@ def __init__(self, settings):
3434 self .processes = {"matlab" : None , "xvfb" : None }
3535 self .matlab_port = None
3636 self .licensing = None
37+ self .tasks = {}
3738 self .logs = {
3839 "matlab" : deque (maxlen = 200 ),
3940 }
@@ -66,6 +67,14 @@ def __reset_and_delete_cached_licensing(self):
6667 self .licensing = None
6768 self .__delete_cached_licensing_file ()
6869
70+ async def __update_and_persist_licensing (self ):
71+ successful_update = await self .update_entitlements ()
72+ if successful_update :
73+ self .persist_licensing ()
74+ else :
75+ self .__reset_and_delete_cached_licensing ()
76+ return successful_update
77+
6978 async def init_licensing (self ):
7079 """Initialize licensing from environment variable or cached file.
7180
@@ -122,12 +131,9 @@ async def init_licensing(self):
122131 ) - timedelta (hours = 1 )
123132
124133 if expiry_window > datetime .now (timezone .utc ):
125- successful_update = await self .update_entitlements ()
134+ successful_update = self .__update_and_persist_licensing ()
126135 if successful_update :
127136 logger .info ("Successful re-use of cached information." )
128- self .persist_licensing ()
129- else :
130- self .__reset_and_delete_cached_licensing ()
131137 else :
132138 self .__reset_and_delete_cached_licensing ()
133139 else :
@@ -198,8 +204,9 @@ async def set_licensing_mhlm(
198204 "entitlement_id" : entitlement_id ,
199205 }
200206
201- await self .update_entitlements ()
202- self .persist_licensing ()
207+ successful_update = self .__update_and_persist_licensing ()
208+ if successful_update :
209+ logger .info ("Login successful, persisting login information." )
203210
204211 except OnlineLicensingError as e :
205212 self .error = e
@@ -445,16 +452,19 @@ async def start_matlab(self, restart=False):
445452 self .processes ["matlab" ] = matlab
446453 logger .debug (f"Started MATLAB (PID={ matlab .pid } )" )
447454
448- async def reader ():
455+ async def matlab_stderr_reader ():
456+ logger .info ("Starting task to save error logs from MATLAB" )
449457 while not self .processes ["matlab" ].stderr .at_eof ():
458+ logger .info ("Checking for any error logs from MATLAB to save..." )
450459 line = await self .processes ["matlab" ].stderr .readline ()
451460 if line is None :
452461 break
462+ logger .info ("Saving error logs from MATLAB." )
453463 self .logs ["matlab" ].append (line )
454464 await self .handle_matlab_output ()
455465
456466 loop = asyncio .get_running_loop ()
457- loop .create_task (reader ())
467+ self . tasks [ "matlab_stderr_reader" ] = loop .create_task (matlab_stderr_reader ())
458468
459469 async def stop_matlab (self ):
460470 """Terminate MATLAB."""
@@ -497,12 +507,15 @@ async def handle_matlab_output(self):
497507 matlab = self .processes ["matlab" ]
498508
499509 # Wait for MATLAB process to exit
510+ logger .info ("handle_matlab_output Waiting for MATLAB to exit..." )
500511 await matlab .wait ()
501512
502513 rc = self .processes ["matlab" ].returncode
514+ logger .info (f"handle_matlab_output MATLAB has exited with errorcode: { rc } " )
503515
504516 # Look for errors if MATLAB was not intentionally stopped and had an error code
505517 if len (self .logs ["matlab" ]) > 0 and self .processes ["matlab" ].returncode != 0 :
518+ logger .info (f"handle_matlab_output Some error was found!" )
506519 err = None
507520 logs = [log .decode ().rstrip () for log in self .logs ["matlab" ]]
508521
0 commit comments