diff --git a/lyrebird/application.py b/lyrebird/application.py index cced4da8d..b4c66e772 100644 --- a/lyrebird/application.py +++ b/lyrebird/application.py @@ -1,3 +1,5 @@ +import webbrowser + from flask import jsonify from functools import reduce @@ -50,6 +52,17 @@ def make_fail_response(msg, **kwargs): encoders_decoders = None +def start_server_without_mock(): + for name in server: + if name == 'mock': + continue + server[name].start() + + +def start_mock_server(): + server['mock'].start() + + def start_server(): for name in server: server[name].start() @@ -86,6 +99,8 @@ def root_dir(): return _cm.ROOT +NO_BROSWER = False + # --------- Lyrebird status --------- ''' [INITING] --> /run lyrebird main method/ --> [READY] @@ -135,9 +150,15 @@ def status_listener(event): is_all_status_checkpoints_ok = reduce(lambda x, y: x and y, status_checkpoints.values()) if is_all_status_checkpoints_ok: + # Set global status global status status = 'READY' + server['event'].publish('system', {'action': 'init_module', 'status': 'READY', 'module': 'all'}) + + # auto open web browser + if not NO_BROSWER: + webbrowser.open(f'http://localhost:{config["mock.port"]}') def process_status_listener(): server['event'].subscribe('system', status_listener) diff --git a/lyrebird/manager.py b/lyrebird/manager.py index f712e266b..e7651c5f1 100644 --- a/lyrebird/manager.py +++ b/lyrebird/manager.py @@ -7,7 +7,6 @@ import socket import threading import traceback -import webbrowser from pathlib import Path from packaging.version import parse as vparse @@ -188,16 +187,19 @@ def run(args: argparse.Namespace): application.status_checkpoints['mitm_proxy'] = False application.server['proxy'] = LyrebirdProxyServer() - application.server['mock'] = LyrebirdMockServer() application.server['extra.mock'] = ExtraMockServer() application.server['db'] = LyrebirdDatabaseServer(path=args.database) application.server['plugin'] = PluginManager() application.server['checker'] = LyrebirdCheckerServer() + # Mock mush init after other servers + application.server['mock'] = LyrebirdMockServer() + # handle progress message application.process_status_listener() - application.start_server() + # Start server without mock server, mock server must start after all blueprint is done + application.start_server_without_mock() # int statistics reporter application.reporter = reporter.Reporter() @@ -222,9 +224,12 @@ def run(args: argparse.Namespace): if args.script: application.server['checker'].load_scripts(args.script) + # Start server without mock server, mock server must start after all blueprint is done + application.start_mock_server() + # auto open web browser if not args.no_browser: - webbrowser.open(f'http://localhost:{application.config["mock.port"]}') + application.NO_BROSWER = args.no_browser # main process is ready, publish system event application.status_ready() diff --git a/lyrebird/mock/extra_mock_server/server.py b/lyrebird/mock/extra_mock_server/server.py index e475d4b43..42752f631 100644 --- a/lyrebird/mock/extra_mock_server/server.py +++ b/lyrebird/mock/extra_mock_server/server.py @@ -89,7 +89,10 @@ async def proxy(context: LyrebirdProxyContext): async def forward(context: LyrebirdProxyContext): logger.info(f'forward {context.forward_url}') - return await send_request(context, context.forward_url) + try: + return await send_request(context, context.forward_url) + except Exception: + logger.debug(f'Forward request error, it may caused by mock server not ready. URL: {context.forward_url}') async def req_handler(request: web.Request): diff --git a/lyrebird/version.py b/lyrebird/version.py index 0410cc02c..102d81afa 100644 --- a/lyrebird/version.py +++ b/lyrebird/version.py @@ -1,3 +1,3 @@ -IVERSION = (2, 10, 2) +IVERSION = (2, 10, 3) VERSION = ".".join(str(i) for i in IVERSION) LYREBIRD = "Lyrebird " + VERSION