using MySql.Data.MySqlClient; using SinMaiLauncher.Models; namespace SinMaiLauncher.ChildProcessHolder; internal class ProcessStateBagMariaDb(ILogger logger, SettingReader infra) : ChildProcessStateBagForConsole(ChildProcessKind.MariaDb, infra.Maria) { protected override async Task CheckReadyAsync() { if (await base.CheckReadyAsync()) { //尝试连接 const int timeoutSecond = 15; const int retryIntervalMs = 1000; // 每秒重试一次 var startTime = DateTime.Now; var setting = infra.Settings.Infra.MariaDb; var connectionString = $"Server={setting.Host};Port={setting.Port};Uid={setting.Usr};Pwd={setting.Pwd};"; while (DateTime.Now - startTime < TimeSpan.FromSeconds(timeoutSecond)) { try { await using var connection = new MySqlConnection(connectionString); logger.LogInformation($"Attempting to connect to MySQL at {setting.Host}:{setting.Port}"); await connection.OpenAsync(); return true; // 连接成功 } catch (MySqlException ex) { logger.LogWarning(ex, "Connection failed. Retrying..."); await Task.Delay(retryIntervalMs); } catch (Exception ex) { logger.LogError(ex, "Unexpected error during connection attempt"); return false; } } } return false; } }