3131# nested subapp (with a name unlikely to cause collisions) containing this API and the
3232# static serving.
3333
34+ # Setup logger for the integration and a web logger. Override any default loggers.
35+ logger = mwi .logger .get (init = True )
36+
3437
3538def marshal_licensing_info (licensing_info ):
3639 """Gather/Marshal licensing information for MHLM and NLM Licensing types.
@@ -76,7 +79,7 @@ def marshal_error(error):
7679 }
7780
7881
79- def create_status_response (app , loadUrl = None ):
82+ async def create_status_response (app , loadUrl = None ):
8083 """Send a generic status response about the state of server,MATLAB and MATLAB Licensing
8184
8285 Args:
@@ -90,7 +93,7 @@ def create_status_response(app, loadUrl=None):
9093 return web .json_response (
9194 {
9295 "matlab" : {
93- "status" : state .get_matlab_state (),
96+ "status" : await state .get_matlab_state (),
9497 "version" : state .settings ["matlab_version" ],
9598 },
9699 "licensing" : marshal_licensing_info (state .licensing ),
@@ -123,7 +126,7 @@ async def get_status(req):
123126 Returns:
124127 JSONResponse: JSONResponse object containing information about the server, MATLAB and MATLAB Licensing.
125128 """
126- return create_status_response (req .app )
129+ return await create_status_response (req .app )
127130
128131
129132async def start_matlab (req ):
@@ -140,7 +143,7 @@ async def start_matlab(req):
140143 # Start MATLAB
141144 await state .start_matlab (restart_matlab = True )
142145
143- return create_status_response (req .app )
146+ return await create_status_response (req .app )
144147
145148
146149async def stop_matlab (req ):
@@ -156,7 +159,7 @@ async def stop_matlab(req):
156159
157160 await state .stop_matlab ()
158161
159- return create_status_response (req .app )
162+ return await create_status_response (req .app )
160163
161164
162165async def set_licensing_info (req ):
@@ -196,7 +199,7 @@ async def set_licensing_info(req):
196199 # Start MATLAB
197200 await state .start_matlab (restart_matlab = True )
198201
199- return create_status_response (req .app )
202+ return await create_status_response (req .app )
200203
201204
202205async def licensing_info_delete (req ):
@@ -218,7 +221,7 @@ async def licensing_info_delete(req):
218221 # Persist licensing information
219222 state .persist_licensing ()
220223
221- return create_status_response (req .app )
224+ return await create_status_response (req .app )
222225
223226
224227async def termination_integration_delete (req ):
@@ -230,7 +233,7 @@ async def termination_integration_delete(req):
230233 state = req .app ["state" ]
231234
232235 # Send response manually because this has to happen before the application exits
233- res = create_status_response (req .app , "../" )
236+ res = await create_status_response (req .app , "../" )
234237 await res .prepare (req )
235238 await res .write_eof ()
236239
@@ -470,7 +473,7 @@ async def matlab_starter(app):
470473 state = app ["state" ]
471474
472475 try :
473- if state .is_licensed () and state .get_matlab_state () == "down" :
476+ if state .is_licensed () and await state .get_matlab_state () == "down" :
474477 await state .start_matlab ()
475478 except asyncio .CancelledError :
476479 # Ensure MATLAB is terminated
@@ -495,8 +498,12 @@ async def cleanup_background_tasks(app):
495498 Args:
496499 app (aiohttp_server): Instance of aiohttp server
497500 """
498- logger = mwi . logger . get ()
501+ # First stop matlab
499502 state = app ["state" ]
503+ await state .stop_matlab ()
504+
505+ # Stop any running async tasks
506+ logger = mwi .logger .get ()
500507 tasks = state .tasks
501508 for task_name , task in tasks .items ():
502509 if not task .cancelled ():
@@ -507,10 +514,52 @@ async def cleanup_background_tasks(app):
507514 except asyncio .CancelledError :
508515 pass
509516
510- await state .stop_matlab ()
511517
518+ def configure_and_start (app ):
519+ """Configure the site for the app and update app with appropriate values
520+
521+ Args:
522+ app (aiohttp.web.Application): A aiohttp web server.
523+
524+ Returns:
525+ aiohttp.web.Application: Updated web server.
526+ """
527+ loop = util .get_event_loop ()
528+
529+ web_logger = None if not mwi_env .is_web_logging_enabled () else logger
530+
531+ # Setup runner
532+ runner = web .AppRunner (app , logger = web_logger , access_log = web_logger )
533+ loop .run_until_complete (runner .setup ())
534+
535+ # Prepare site to start, then set port of the app.
536+ site = util .prepare_site (app , runner )
537+
538+ # This would be required when MWI_APP_PORT env variable is not set and the site starts on a random port.
539+ app ["settings" ]["app_port" ] = site ._port
540+
541+ # Update the site origin in settings.
542+ # The origin will be used for communicating with the Embedded connector.
543+ app ["settings" ]["mwi_server_url" ] = site .name
544+
545+ loop .run_until_complete (site .start ())
512546
513- # config is has a default initializer because it needs to be callable without inputs from ADEV servers
547+ logger .debug ("Starting MATLAB proxy app" )
548+ logger .debug (
549+ f' with base_url: { app ["settings" ]["base_url" ]} and app_port:{ app ["settings" ]["app_port" ]} .'
550+ )
551+
552+ logger .info (
553+ util .prettify (
554+ boundary_filler = "=" ,
555+ text_arr = [f"MATLAB can be accessed at:" , site .name ],
556+ )
557+ )
558+
559+ return app
560+
561+
562+ # config has a default initializer because it needs to be callable without inputs from ADEV servers
514563def create_app (config_name = matlab_proxy .get_default_config_name ()):
515564 """Creates the web server and adds the routes,settings and env_config to the server.
516565
@@ -558,51 +607,17 @@ def create_app(config_name=matlab_proxy.get_default_config_name()):
558607def main ():
559608 """Starting point of the integration. Creates the web app and runs indefinitely."""
560609
561- # Setup logger for the integration and a web logger. Override any default loggers.
562- logger = mwi .logger .get (init = True )
563- web_logger = None if not mwi_env .is_web_logging_enabled () else logger
564-
565610 # The integration needs to be called with --config flag.
566611 # Parse the passed cli arguments.
567612 desired_configuration_name = util .parse_cli_args ()["config" ]
568613
569- app = create_app (desired_configuration_name )
570-
571- loop = asyncio .get_event_loop ()
572-
573- # Setup runner
574- runner = web .AppRunner (app , logger = web_logger , access_log = web_logger )
575- loop .run_until_complete (runner .setup ())
576-
577- # Prepare site to start, then set port of the app.
578- site = util .prepare_site (app , runner )
579- # This would be required when MWI_APP_PORT env variable is not set and the site starts on a random port.
580- app ["settings" ]["app_port" ] = site ._port
581- loop .run_until_complete (site .start ())
614+ # Create, configure and start the app.
615+ app = create_app (config_name = desired_configuration_name )
616+ app = configure_and_start (app )
582617
618+ loop = util .get_event_loop ()
619+ # Add signal handlers for the current python process
583620 loop = util .add_signal_handlers (loop )
584-
585- logger .debug ("Starting MATLAB proxy app" )
586- logger .debug (
587- f' with base_url: { app ["settings" ]["base_url" ]} and app_port:{ app ["settings" ]["app_port" ]} .'
588- )
589-
590- ssl_context = app ["settings" ]["ssl_context" ]
591- if ssl_context != None :
592- access_protocol = "https"
593- else :
594- access_protocol = "http"
595-
596- logger .info (
597- util .prettify (
598- boundary_filler = "=" ,
599- text_arr = [
600- f"MATLAB can be accessed at:" ,
601- f'{ access_protocol } ://localhost:{ app ["settings" ]["app_port" ]} { app ["settings" ]["base_url" ]} /index.html' ,
602- ],
603- )
604- )
605-
606621 loop .run_forever ()
607622
608623 async def shutdown ():
0 commit comments