From b4b442e56ae66451ad2014029c5f149ca07febfa Mon Sep 17 00:00:00 2001 From: Maurane GLAUDE Date: Wed, 19 Jun 2024 17:31:09 +0200 Subject: [PATCH] fix: add logs, upgrade server running checks, change kill timeout --- .github/workflows/python-package.yml | 6 ++-- src/antares_web_installer/app.py | 47 ++++++++++++++++++++-------- src/antares_web_installer/cli.py | 4 +-- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index ec6d3d6..c72639e 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -49,12 +49,12 @@ jobs: if: matrix.python-version == '3.8' && matrix.os == 'windows-latest' uses: actions/upload-artifact@v4 with: - name: AntaresWebInstaller-${{ matrix.os }}.zip - path: dist/AntaresWebInstaller-${{ matrix.os }} + name: AntaresWebInstaller-${{ matrix.os }} + path: dist/AntaresWebInstaller-${{ matrix.os }}.exe - name: Upload Ubuntu binaries if: matrix.python-version == '3.8' && matrix.os != 'windows-latest' uses: actions/upload-artifact@v4 with: - name: AntaresWebInstaller-${{ matrix.os }}.zip + name: AntaresWebInstaller-${{ matrix.os }} path: dist/AntaresWebInstaller-${{ matrix.os }} diff --git a/src/antares_web_installer/app.py b/src/antares_web_installer/app.py index eaad77b..84fb6d0 100644 --- a/src/antares_web_installer/app.py +++ b/src/antares_web_installer/app.py @@ -9,6 +9,7 @@ from pathlib import Path from shutil import copy2, copytree +import httpx import psutil from antares_web_installer.config import update_config @@ -64,14 +65,13 @@ def kill_running_server(self) -> None: Check whether Antares service is up. Kill the process if so. """ - server_name = SERVER_NAMES[self.os_name] for proc in psutil.process_iter(["pid", "name"]): - if server_name.lower() in proc.name().lower(): - logger.info("Running server found. We will attempt to stop it.") + if 'antareswebserve' in proc.name().lower(): + logger.info(f"Running server '{proc.name()}' (pid={proc.pid}) found. Attempt to stop it.") running_app = psutil.Process(pid=proc.pid) running_app.kill() - running_app.wait(15) + running_app.wait(5) logger.info("The application was successfully stopped.") @@ -79,24 +79,35 @@ def kill_running_server(self) -> None: def install_files(self): """ """ + logger.info(f"Starting installing files in {self.target_dir}...") + # if the target directory already exists and isn't empty if self.target_dir.is_dir() and list(self.target_dir.iterdir()): + logger.info("Existing files were found. Proceed checking old version...") # check app version version = self.check_version() logger.info(f"Old application version : {version}.") # update config file + logger.info("Update configuration file...") config_path = self.target_dir.joinpath("config.yaml") update_config(config_path, config_path, version) + logger.info("Configuration file updated.") + # copy binaries + logger.info("Update program files...") self.copy_files() + logger.info("Program files updated") + logger.info("Check new application version...") version = self.check_version() logger.info(f"New application version : {version}.") else: # copy all files from package + logger.info("No existing files found. Starting file copy...") copytree(self.source_dir, self.target_dir, dirs_exist_ok=True) + logger.info("Files was successfully copied.") def copy_files(self): """ @@ -152,10 +163,8 @@ def create_shortcuts(self): """ Create a local server icon and a browser icon on desktop and """ - # using pyshortcuts - logger.info("Generating server shortcut...") - # prepare a shortcut into the desktop directory + logger.info("Generating server shortcut on desktop...") shortcut_name = SHORTCUT_NAMES[self.os_name] shortcut_path = Path(get_desktop()).joinpath(shortcut_name) @@ -163,7 +172,9 @@ def create_shortcuts(self): shortcut_path.unlink(missing_ok=True) # shortcut generation - logging.info("Generating server shortcut...") + logger.info(f"Shortcut will be created in {shortcut_path}, " + f"linked to '{self.server_path}' " + f"and located in '{self.target_dir}' directory.") create_shortcut( shortcut_path, exe_path=self.server_path, @@ -171,17 +182,27 @@ def create_shortcuts(self): description="Launch Antares Web Server in background", ) - logger.info("Server shortcut successfully created.") + logger.info("Server shortcut was successfully created.") def start_server(self): """ Launch the local server as a background task """ + logger.info("Attempt to start the newly installed server...") args = [str(self.server_path)] - server_process = subprocess.Popen(args=args, start_new_session=True, cwd=self.target_dir) - time.sleep(2) # wait for the server to complete startup - if server_process.poll() is None: - logger.info("Server was started successfully.") + subprocess.Popen(args=args, start_new_session=True, cwd=self.target_dir) + + is_server_available = False + while not is_server_available: + try: + res = httpx.get("http://localhost:8080/", timeout=1) + except httpx.ConnectError: + logger.info("No response received. Retry ...") + time.sleep(1) + else: + if res.status_code: + logger.info(f"Server is now available ({res.status_code}).") + is_server_available = True def open_browser(self): """ diff --git a/src/antares_web_installer/cli.py b/src/antares_web_installer/cli.py index 841ee27..451e07e 100644 --- a/src/antares_web_installer/cli.py +++ b/src/antares_web_installer/cli.py @@ -59,8 +59,8 @@ def install_cli(src_dir: t.Union[str, Path], target_dir: t.Union[str, Path], **k Takes two positional argument : 'src_dir' source directory to copy files from and 'target' directory to paste files to. """ - target_dir = Path(target_dir).expanduser() - src_dir = Path(src_dir) + target_dir = Path(target_dir).expanduser().absolute() + src_dir = Path(src_dir).expanduser().absolute() logging.basicConfig(level=logging.INFO, format="[%(asctime)-15s] %(message)s", stream=sys.stdout)