public bool WaitUntilConnectionSuccessful()

in Configurator/Core/Server/MySqlServerInstance.cs [1546:1654]


    public bool WaitUntilConnectionSuccessful(bool reportStatusChanges, int maxRetries = DEFAULT_MAX_CONNECTION_RETRIES)
    {
      if (maxRetries < 0)
      {
        // Set a reasonable maximum, no need to have a never-ending loop.
        maxRetries = 100;
      }

      int currentRetry = 0;
      var currentUseOldSettings = UseOldSettings;
      if (reportStatusChanges)
      {
        ReportStatus(string.Format(Resources.ServerConfigWaitingForSuccesfulConnectionText, NameWithVersion, maxRetries));
      }

      var success = false;
      uint connectionTimeOut = 10;
      const int WAITING_TIME_BETWEEN_CONNECTIONS_IN_SECONDS = 5;
      while (!success && currentRetry < maxRetries)
      {
        var flipSettings = true;
        currentRetry++;
        if (currentRetry > 1)
        {
          if (reportStatusChanges)
          {
            ReportStatus(string.Format(Resources.ServerConfigWaitingForSuccesfulConnectionRetryWaitText, WAITING_TIME_BETWEEN_CONNECTIONS_IN_SECONDS));
          }

          Thread.Sleep(WAITING_TIME_BETWEEN_CONNECTIONS_IN_SECONDS * 1000);
        }

        try
        {
          var connStringBuilder = GetConnectionStringBuilder();
          connStringBuilder.ConnectionTimeout = connectionTimeOut;
          if (reportStatusChanges)
          {
            ReportStatus(string.Format(Resources.ServerConfigWaitingForSuccesfulConnectionRetryText, currentRetry, connStringBuilder.GetHostIdentifier(), connStringBuilder.UserID, string.IsNullOrEmpty(connStringBuilder.Password) ? "no" : "a"));
          }

          using (var c = new MySqlConnection(connStringBuilder.ConnectionString))
          {
            c.Open();
          }

          success = true;
        }
        catch (System.TimeoutException timeoutException)
        {
          // Increase the timeout, see what happens in the next retry.
          connectionTimeOut *= 2;
          Logger.LogException(timeoutException);
          if (reportStatusChanges)
          {
            ReportStatus($"Timeout error: {timeoutException.Message}");
            ReportStatus($"Increasing timeout to {connectionTimeOut} seconds and retrying.");
          }
        }
        catch (MySqlException mySqlException)
        {
          if (mySqlException.Message.IndexOf("access denied", StringComparison.InvariantCultureIgnoreCase) >= 0)
          {
            // This means the current root user can't connect with the current credentials but the Server is accepting connections
            success = true;
            break;
          }

          if (reportStatusChanges)
          {
            ReportStatus($"MySQL error {mySqlException.Number}: {mySqlException.Message}");
          }

          Logger.LogException(mySqlException);
          if (mySqlException.Message.IndexOf("hosts", StringComparison.InvariantCultureIgnoreCase) > 0)
          {
            if (UseOldSettings)
            {
              UseOldSettings = false;
              flipSettings = false;
            }
          }

          if (flipSettings)
          {
            // Try flipping the UseOldSettings value and reconnecting
            UseOldSettings = !UseOldSettings;
          }
        }
        catch (Exception ex)
        {
          Logger.LogException(ex);
          if (reportStatusChanges)
          {
            ReportStatus($"Unknown error: {ex.Message}");
          }
        }
      }

      if (reportStatusChanges)
      {
        ReportStatus(success
                      ? string.Format(Resources.ServerConfigWaitingForSuccesfulConnectionSuccessText, NameWithVersion)
                      : string.Format(Resources.ServerConfigWaitingForSuccesfulConnectionFailedText, NameWithVersion, currentRetry));
      }

      UseOldSettings = currentUseOldSettings;
      return success;
    }