(was_still_installing, run_fn)
| 744 | |
| 745 | |
| 746 | def do_start_backend(was_still_installing, run_fn): |
| 747 | global WEBUI_HOST, WEBUI_PORT |
| 748 | |
| 749 | config = getConfig() |
| 750 | backend_config = config.get("backend_config") or {} |
| 751 | |
| 752 | WEBUI_HOST = backend_config.get("host", "localhost") |
| 753 | WEBUI_PORT = backend_config.get("port", "7860") |
| 754 | |
| 755 | def restart_if_webui_dies_after_starting(): |
| 756 | has_started = False |
| 757 | |
| 758 | while True: |
| 759 | if backend_process is None: |
| 760 | return |
| 761 | |
| 762 | # Check if the process is actually dead |
| 763 | return_code = backend_process.poll() |
| 764 | |
| 765 | if return_code is not None: |
| 766 | # Process has terminated |
| 767 | if has_started: |
| 768 | print(f"######################## Backend process died with code {return_code}. Restarting...") |
| 769 | stop_backend() |
| 770 | backend_thread = Thread(target=target) |
| 771 | backend_thread.start() |
| 772 | break |
| 773 | else: |
| 774 | # Process died before starting successfully |
| 775 | print(f"######################## Backend process failed to start (exit code {return_code})") |
| 776 | break |
| 777 | elif not has_started: |
| 778 | # Process is running, check if it has started successfully via ping |
| 779 | try: |
| 780 | ping(timeout=5) |
| 781 | has_started = True |
| 782 | |
| 783 | if was_still_installing: |
| 784 | ui = config.get("ui", {}) |
| 785 | net = config.get("net", {}) |
| 786 | port = net.get("listen_port", 9000) |
| 787 | |
| 788 | if ui.get("open_browser_on_start", True): |
| 789 | import webbrowser |
| 790 | |
| 791 | log.info("Opening browser..") |
| 792 | |
| 793 | webbrowser.open(f"http://localhost:{port}") |
| 794 | except (TimeoutError, ConnectionError): |
| 795 | pass # Still starting up |
| 796 | except Exception: |
| 797 | import traceback |
| 798 | |
| 799 | log.exception(traceback.format_exc()) |
| 800 | |
| 801 | time.sleep(1) |
| 802 | |
| 803 | def target(): |
no test coverage detected