|
16 | 16 | import matlab_proxy |
17 | 17 | from matlab_proxy import constants, settings, util |
18 | 18 | from matlab_proxy.app_state import AppState |
19 | | -from matlab_proxy.constants import IS_CONCURRENCY_CHECK_ENABLED |
| 19 | +from matlab_proxy.constants import IS_CONCURRENCY_CHECK_ENABLED, ExitReason |
20 | 20 | from matlab_proxy.util import mwi |
21 | 21 | from matlab_proxy.util.mwi import download, token_auth |
22 | 22 | from matlab_proxy.util.mwi import environment_variables as mwi_env |
@@ -444,6 +444,11 @@ async def shutdown_integration_delete(req): |
444 | 444 |
|
445 | 445 | logger.info(f"Shutting down {state.settings['integration_name']}...") |
446 | 446 | state.is_shutting_down = True |
| 447 | + |
| 448 | + try: |
| 449 | + state.exit_reason = ExitReason[req.query.get("reason", "").upper()] |
| 450 | + except KeyError: |
| 451 | + pass |
447 | 452 | res = create_status_response(req.app, "../") |
448 | 453 |
|
449 | 454 | # Schedule the shutdown to happen after the response is sent |
@@ -1008,41 +1013,56 @@ def create_and_start_app(config_name): |
1008 | 1013 | Args: |
1009 | 1014 | config_name (str): Name of the configuration to use with matlab-proxy. |
1010 | 1015 | """ |
1011 | | - util.system.configure_no_proxy_in_env(logger) |
| 1016 | + exit_code = int(ExitReason.UNEXPECTED_ERROR) |
1012 | 1017 |
|
1013 | | - # Create, configure and start the app. |
1014 | | - app = create_app(config_name) |
1015 | | - app = configure_and_start(app) |
| 1018 | + try: |
| 1019 | + util.system.configure_no_proxy_in_env(logger) |
1016 | 1020 |
|
1017 | | - loop = util.get_event_loop() |
| 1021 | + # Create, configure and start the app. |
| 1022 | + app = create_app(config_name) |
| 1023 | + app = configure_and_start(app) |
1018 | 1024 |
|
1019 | | - # Add signal handlers for the current python process |
1020 | | - loop = util.add_signal_handlers(loop) |
1021 | | - try: |
1022 | | - # Further execution is stopped here until an interrupt is raised |
1023 | | - loop.run_forever() |
| 1025 | + loop = util.get_event_loop() |
1024 | 1026 |
|
1025 | | - except SystemExit: |
1026 | | - pass |
| 1027 | + # Add signal handlers for the current python process |
| 1028 | + loop = util.add_signal_handlers(loop) |
| 1029 | + try: |
| 1030 | + # Further execution is stopped here until an interrupt is raised |
| 1031 | + loop.run_forever() |
1027 | 1032 |
|
1028 | | - # After handling the interrupt, proceed with shutting down the server gracefully. |
1029 | | - try: |
1030 | | - # aiohttp shutdown to be invoked before cleanup - |
1031 | | - # https://docs.aiohttp.org/en/stable/web_reference.html#aiohttp.web.Application.shutdown |
1032 | | - loop.run_until_complete(app.shutdown()) |
1033 | | - loop.run_until_complete(app.cleanup()) |
| 1033 | + except SystemExit: |
| 1034 | + pass |
1034 | 1035 |
|
1035 | | - running_tasks = asyncio.all_tasks(loop) |
| 1036 | + # After handling the interrupt, proceed with shutting down the server gracefully. |
| 1037 | + try: |
| 1038 | + # aiohttp shutdown to be invoked before cleanup - |
| 1039 | + # https://docs.aiohttp.org/en/stable/web_reference.html#aiohttp.web.Application.shutdown |
| 1040 | + loop.run_until_complete(app.shutdown()) |
| 1041 | + loop.run_until_complete(app.cleanup()) |
| 1042 | + |
| 1043 | + running_tasks = asyncio.all_tasks(loop) |
| 1044 | + |
| 1045 | + # Gracefully cancel all running background tasks |
| 1046 | + loop.run_until_complete(util.cancel_tasks(running_tasks)) |
| 1047 | + |
| 1048 | + except Exception: |
| 1049 | + pass |
1036 | 1050 |
|
1037 | | - # Gracefully cancel all running background tasks |
1038 | | - loop.run_until_complete(util.cancel_tasks(running_tasks)) |
| 1051 | + state = app["state"] |
| 1052 | + logger.info( |
| 1053 | + f"Finished shutting down (reason: {state.exit_reason.name}, " |
| 1054 | + f"code: {int(state.exit_reason)}). Thank you for using the MATLAB proxy." |
| 1055 | + ) |
| 1056 | + loop.close() |
| 1057 | + exit_code = int(state.exit_reason) |
| 1058 | + |
| 1059 | + except SystemExit: |
| 1060 | + logger.exception("Unexpected SystemExit raised during execution.") |
1039 | 1061 |
|
1040 | 1062 | except Exception: |
1041 | | - pass |
| 1063 | + logger.exception("Unexpected error caused shutdown.") |
1042 | 1064 |
|
1043 | | - logger.info("Finished shutting down. Thank you for using the MATLAB proxy.") |
1044 | | - loop.close() |
1045 | | - sys.exit(0) |
| 1065 | + sys.exit(exit_code) |
1046 | 1066 |
|
1047 | 1067 |
|
1048 | 1068 | def print_version_and_exit(): |
|
0 commit comments