From 7966b061c08e351bee23e3a0be65304d8682f444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=B9=8F?= Date: Fri, 9 Oct 2026 10:54:03 +0800 Subject: [PATCH] Wait for slow MySQL service startup --- deploy_tool/installer.py | 55 ++++++++++++++++++++++++++++++---------- tests/test_installer.py | 31 +++++++++++++++++++--- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/deploy_tool/installer.py b/deploy_tool/installer.py index b53b39d..fafd79a 100644 --- a/deploy_tool/installer.py +++ b/deploy_tool/installer.py @@ -561,13 +561,11 @@ def ensure_mysql_service_running( exit_code = run_mysql_service_start_diagnostic( service_name, service_log, + mysql_executable=mysql_executable, ) if exit_code not in {0, 1056}: - detail = _read_install_log_tail(default_mysql_service_log_path(), max_lines=24) - suffix = f"\n{detail}" if detail else "" - raise RuntimeError( - f"无法启动 Windows 服务 {service_name},退出码: {exit_code}。" - f" 详细日志: {default_mysql_service_log_path()}{suffix}" + on_output( + f"Windows 服务启动命令返回 {exit_code},服务可能仍在后台恢复,继续等待运行状态..." ) for attempt in range(max(attempts, 1)): @@ -575,27 +573,37 @@ def ensure_mysql_service_running( if state == 4: on_output(f"Windows 服务 {service_name} 已启动。") return - if state == 1 and attempt >= 2: - detail = mysql_service_failure_detail(mysql_executable, state) - raise RuntimeError(f"Windows 服务 {service_name} 启动后已停止。{detail}") + if attempt == 4 or (attempt + 1) % 15 == 0: + on_output( + f"Windows 服务 {service_name} 正在启动或恢复,继续等待({attempt + 1}/{max(attempts, 1)})..." + ) time.sleep(retry_delay) detail = mysql_service_failure_detail(mysql_executable, state) + diagnostic = _read_install_log_tail(default_mysql_service_log_path(), max_lines=24) + diagnostic_text = f"\n启动诊断:\n{diagnostic}" if diagnostic else "" raise RuntimeError( - f"Windows 服务 {service_name} 启动超时。{detail}" + f"Windows 服务 {service_name} 启动超时,启动命令退出码: {exit_code}。" + f"{detail}{diagnostic_text}" ) -def build_mysql_service_start_script(service_name: str = "MySQL80") -> str: +def build_mysql_service_start_script( + service_name: str = "MySQL80", + mysql_executable: Path | None = None, +) -> str: escaped_service_name = service_name.replace("'", "''") + escaped_mysql_executable = str(mysql_executable or "").replace("'", "''") return ( "$ErrorActionPreference='Continue';" f"$name='{escaped_service_name}';" + f"$mysqlExe='{escaped_mysql_executable}';" "$startError='';" "try { Start-Service -Name $name -ErrorAction Stop } " "catch { $startError=($_ | Out-String); Write-Output $startError };" "Start-Sleep -Seconds 3;" "$service=Get-CimInstance Win32_Service -Filter (\"Name='\"+$name+\"'\");" - "$service | Select-Object Name,State,StartName,PathName | Format-List | Out-String | Write-Output;" + "$service | Select-Object Name,State,StartName,PathName,ExitCode,ProcessId | " + "Format-List | Out-String | Write-Output;" "$ini=Join-Path $env:ProgramData 'MySQL\\MySQL Server 8.0\\my.ini';" "Write-Output ('my.ini: '+$ini+' exists='+[bool](Test-Path -LiteralPath $ini));" "$data=Join-Path $env:ProgramData 'MySQL\\MySQL Server 8.0\\Data';" @@ -604,15 +612,36 @@ def build_mysql_service_start_script(service_name: str = "MySQL80") -> str: "Sort-Object LastWriteTime -Descending | Select-Object -First 1;" "if($err){Write-Output ('Error log: '+$err.FullName); Get-Content -LiteralPath $err.FullName -Tail 30}" "else{Write-Output 'No .err log found with administrator privileges.'};" + "if($mysqlExe -and (Test-Path -LiteralPath $mysqlExe)){" + "Write-Output ('mysqld.exe: '+$mysqlExe);" + "Write-Output 'mysqld version:'; & $mysqlExe --version 2>&1 | Out-String | Write-Output;" + "Write-Output 'mysqld configuration validation:';" + "& $mysqlExe (\"--defaults-file=\"+$ini) --validate-config --console 2>&1 | Out-String | Write-Output;" + "Write-Output ('validate-config exit code: '+$LASTEXITCODE)" + "};" + "$since=(Get-Date).AddMinutes(-10);" + "Write-Output 'Recent Windows service events:';" + "Get-WinEvent -FilterHashtable @{LogName='System';StartTime=$since} -ErrorAction SilentlyContinue | " + "Where-Object {$_.ProviderName -eq 'Service Control Manager' -and $_.Message -match $name} | " + "Select-Object -First 8 TimeCreated,Id,LevelDisplayName,Message | Format-List | Out-String | Write-Output;" + "Write-Output 'Recent MySQL/Application events:';" + "Get-WinEvent -FilterHashtable @{LogName='Application';StartTime=$since} -ErrorAction SilentlyContinue | " + "Where-Object {$_.ProviderName -match 'MySQL|Application Error' -or $_.Message -match 'mysqld|MySQL80'} | " + "Select-Object -First 8 TimeCreated,ProviderName,Id,LevelDisplayName,Message | " + "Format-List | Out-String | Write-Output;" "if($service.State -eq 'Running'){exit 0}else{exit 1}" ) -def run_mysql_service_start_diagnostic(service_name: str, log_path: Path) -> int: +def run_mysql_service_start_diagnostic( + service_name: str, + log_path: Path, + mysql_executable: Path | None = None, +) -> int: with tempfile.TemporaryDirectory(prefix="autodeploy-mysql-service-") as temporary: script_path = Path(temporary) / "start-mysql-service.ps1" script_path.write_text( - build_mysql_service_start_script(service_name), + build_mysql_service_start_script(service_name, mysql_executable), encoding="utf-8-sig", ) return run_elevated_command_with_log( diff --git a/tests/test_installer.py b/tests/test_installer.py index fabb1e1..83c47e5 100644 --- a/tests/test_installer.py +++ b/tests/test_installer.py @@ -462,13 +462,33 @@ class InstallerTests(unittest.TestCase): self.assertEqual(commands, [["sc.exe", "start", "MySQL80"]]) self.assertTrue(any("已启动" in message for message in messages)) + def test_waits_for_mysql_service_when_start_command_reports_failure(self): + with ( + patch("deploy_tool.installer.query_mysql_service_state", side_effect=[1, 1, 2, 4]), + patch("deploy_tool.installer.time.sleep"), + ): + messages = [] + ensure_mysql_service_running( + messages.append, + elevated_runner=lambda _command: 1, + ) + + self.assertTrue(any("继续等待运行状态" in message for message in messages)) + self.assertTrue(any("已启动" in message for message in messages)) + def test_builds_privileged_mysql_service_start_diagnostic(self): - script = build_mysql_service_start_script() + script = build_mysql_service_start_script( + mysql_executable=Path(r"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe") + ) self.assertIn("Start-Service -Name $name", script) self.assertIn("Get-CimInstance Win32_Service", script) + self.assertIn("ExitCode,ProcessId", script) self.assertIn("my.ini", script) self.assertIn("*.err", script) self.assertIn("Get-Content", script) + self.assertIn("--validate-config", script) + self.assertIn("Get-WinEvent", script) + self.assertIn("mysqld.exe", script) with patch.dict("os.environ", {"LOCALAPPDATA": r"C:\Users\tester\AppData\Local"}): log_path = default_mysql_service_log_path() @@ -495,7 +515,11 @@ class InstallerTests(unittest.TestCase): "deploy_tool.installer.run_elevated_command_with_log", side_effect=inspect_command, ): - exit_code = run_mysql_service_start_diagnostic("MySQL80", log_path) + exit_code = run_mysql_service_start_diagnostic( + "MySQL80", + log_path, + mysql_executable=Path("mysqld.exe"), + ) self.assertEqual(exit_code, 1) @@ -509,7 +533,7 @@ class InstallerTests(unittest.TestCase): with ( patch.dict("os.environ", {"PROGRAMDATA": str(program_data)}), - patch("deploy_tool.installer.query_mysql_service_state", side_effect=[1, 2, 1, 1]), + patch("deploy_tool.installer.query_mysql_service_state", return_value=1), patch("deploy_tool.installer.time.sleep"), ): with self.assertRaisesRegex(RuntimeError, "Data Dictionary initialization failed"): @@ -517,6 +541,7 @@ class InstallerTests(unittest.TestCase): lambda _message: None, mysql_executable=Path("mysql.exe"), elevated_runner=lambda _command: 0, + attempts=3, ) self.assertEqual(find_mysql_error_log(Path("mysql.exe")), error_log) detail = mysql_service_failure_detail(Path("mysql.exe"), 1)