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(
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user