Wait for slow MySQL service startup
This commit is contained in:
@@ -561,13 +561,11 @@ def ensure_mysql_service_running(
|
|||||||
exit_code = run_mysql_service_start_diagnostic(
|
exit_code = run_mysql_service_start_diagnostic(
|
||||||
service_name,
|
service_name,
|
||||||
service_log,
|
service_log,
|
||||||
|
mysql_executable=mysql_executable,
|
||||||
)
|
)
|
||||||
if exit_code not in {0, 1056}:
|
if exit_code not in {0, 1056}:
|
||||||
detail = _read_install_log_tail(default_mysql_service_log_path(), max_lines=24)
|
on_output(
|
||||||
suffix = f"\n{detail}" if detail else ""
|
f"Windows 服务启动命令返回 {exit_code},服务可能仍在后台恢复,继续等待运行状态..."
|
||||||
raise RuntimeError(
|
|
||||||
f"无法启动 Windows 服务 {service_name},退出码: {exit_code}。"
|
|
||||||
f" 详细日志: {default_mysql_service_log_path()}{suffix}"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
for attempt in range(max(attempts, 1)):
|
for attempt in range(max(attempts, 1)):
|
||||||
@@ -575,27 +573,37 @@ def ensure_mysql_service_running(
|
|||||||
if state == 4:
|
if state == 4:
|
||||||
on_output(f"Windows 服务 {service_name} 已启动。")
|
on_output(f"Windows 服务 {service_name} 已启动。")
|
||||||
return
|
return
|
||||||
if state == 1 and attempt >= 2:
|
if attempt == 4 or (attempt + 1) % 15 == 0:
|
||||||
detail = mysql_service_failure_detail(mysql_executable, state)
|
on_output(
|
||||||
raise RuntimeError(f"Windows 服务 {service_name} 启动后已停止。{detail}")
|
f"Windows 服务 {service_name} 正在启动或恢复,继续等待({attempt + 1}/{max(attempts, 1)})..."
|
||||||
|
)
|
||||||
time.sleep(retry_delay)
|
time.sleep(retry_delay)
|
||||||
detail = mysql_service_failure_detail(mysql_executable, state)
|
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(
|
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_service_name = service_name.replace("'", "''")
|
||||||
|
escaped_mysql_executable = str(mysql_executable or "").replace("'", "''")
|
||||||
return (
|
return (
|
||||||
"$ErrorActionPreference='Continue';"
|
"$ErrorActionPreference='Continue';"
|
||||||
f"$name='{escaped_service_name}';"
|
f"$name='{escaped_service_name}';"
|
||||||
|
f"$mysqlExe='{escaped_mysql_executable}';"
|
||||||
"$startError='';"
|
"$startError='';"
|
||||||
"try { Start-Service -Name $name -ErrorAction Stop } "
|
"try { Start-Service -Name $name -ErrorAction Stop } "
|
||||||
"catch { $startError=($_ | Out-String); Write-Output $startError };"
|
"catch { $startError=($_ | Out-String); Write-Output $startError };"
|
||||||
"Start-Sleep -Seconds 3;"
|
"Start-Sleep -Seconds 3;"
|
||||||
"$service=Get-CimInstance Win32_Service -Filter (\"Name='\"+$name+\"'\");"
|
"$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';"
|
"$ini=Join-Path $env:ProgramData 'MySQL\\MySQL Server 8.0\\my.ini';"
|
||||||
"Write-Output ('my.ini: '+$ini+' exists='+[bool](Test-Path -LiteralPath $ini));"
|
"Write-Output ('my.ini: '+$ini+' exists='+[bool](Test-Path -LiteralPath $ini));"
|
||||||
"$data=Join-Path $env:ProgramData 'MySQL\\MySQL Server 8.0\\Data';"
|
"$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;"
|
"Sort-Object LastWriteTime -Descending | Select-Object -First 1;"
|
||||||
"if($err){Write-Output ('Error log: '+$err.FullName); Get-Content -LiteralPath $err.FullName -Tail 30}"
|
"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.'};"
|
"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}"
|
"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:
|
with tempfile.TemporaryDirectory(prefix="autodeploy-mysql-service-") as temporary:
|
||||||
script_path = Path(temporary) / "start-mysql-service.ps1"
|
script_path = Path(temporary) / "start-mysql-service.ps1"
|
||||||
script_path.write_text(
|
script_path.write_text(
|
||||||
build_mysql_service_start_script(service_name),
|
build_mysql_service_start_script(service_name, mysql_executable),
|
||||||
encoding="utf-8-sig",
|
encoding="utf-8-sig",
|
||||||
)
|
)
|
||||||
return run_elevated_command_with_log(
|
return run_elevated_command_with_log(
|
||||||
|
|||||||
@@ -462,13 +462,33 @@ class InstallerTests(unittest.TestCase):
|
|||||||
self.assertEqual(commands, [["sc.exe", "start", "MySQL80"]])
|
self.assertEqual(commands, [["sc.exe", "start", "MySQL80"]])
|
||||||
self.assertTrue(any("已启动" in message for message in messages))
|
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):
|
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("Start-Service -Name $name", script)
|
||||||
self.assertIn("Get-CimInstance Win32_Service", script)
|
self.assertIn("Get-CimInstance Win32_Service", script)
|
||||||
|
self.assertIn("ExitCode,ProcessId", script)
|
||||||
self.assertIn("my.ini", script)
|
self.assertIn("my.ini", script)
|
||||||
self.assertIn("*.err", script)
|
self.assertIn("*.err", script)
|
||||||
self.assertIn("Get-Content", 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"}):
|
with patch.dict("os.environ", {"LOCALAPPDATA": r"C:\Users\tester\AppData\Local"}):
|
||||||
log_path = default_mysql_service_log_path()
|
log_path = default_mysql_service_log_path()
|
||||||
@@ -495,7 +515,11 @@ class InstallerTests(unittest.TestCase):
|
|||||||
"deploy_tool.installer.run_elevated_command_with_log",
|
"deploy_tool.installer.run_elevated_command_with_log",
|
||||||
side_effect=inspect_command,
|
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)
|
self.assertEqual(exit_code, 1)
|
||||||
|
|
||||||
@@ -509,7 +533,7 @@ class InstallerTests(unittest.TestCase):
|
|||||||
|
|
||||||
with (
|
with (
|
||||||
patch.dict("os.environ", {"PROGRAMDATA": str(program_data)}),
|
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"),
|
patch("deploy_tool.installer.time.sleep"),
|
||||||
):
|
):
|
||||||
with self.assertRaisesRegex(RuntimeError, "Data Dictionary initialization failed"):
|
with self.assertRaisesRegex(RuntimeError, "Data Dictionary initialization failed"):
|
||||||
@@ -517,6 +541,7 @@ class InstallerTests(unittest.TestCase):
|
|||||||
lambda _message: None,
|
lambda _message: None,
|
||||||
mysql_executable=Path("mysql.exe"),
|
mysql_executable=Path("mysql.exe"),
|
||||||
elevated_runner=lambda _command: 0,
|
elevated_runner=lambda _command: 0,
|
||||||
|
attempts=3,
|
||||||
)
|
)
|
||||||
self.assertEqual(find_mysql_error_log(Path("mysql.exe")), error_log)
|
self.assertEqual(find_mysql_error_log(Path("mysql.exe")), error_log)
|
||||||
detail = mysql_service_failure_detail(Path("mysql.exe"), 1)
|
detail = mysql_service_failure_detail(Path("mysql.exe"), 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user