Exemple #1
0
        public static ConnectionCheck Run(RaciClient client, int count = DefaultAttempts, int timeout = MaxTimeout, ILogger logger = null)
        {
            if (logger == null)
            {
                logger = RaciLog.DefaultLogger;
            }
            ConnectionCheck result = new ConnectionCheck(timeout);

            result.Url = client?.BaseUrl?.AbsoluteUri;
            if (count <= 0)
            {
                count = DefaultAttempts;
            }

            logger.LogInformation($"RACI Client connection test starting (timeout={result.Timeout:f2}ms");

            for (int i = 0; i < count; i++)
            {
                double?respTime = null;
                string respMsg  = null;
                if (client != null || client.BaseUrl != null)
                {
                    try
                    {
                        IRestRequest request = client.CreateRequest <bool>(Method.GET, "service/ping");
                        DateTime     ti      = DateTime.Now;
                        client.Timeout = (int)result.Timeout;
                        IRestResponse <bool> resp = null;
                        resp = client.Execute <bool>(request);

                        if (resp != null)
                        {
                            if (resp.ResponseStatus == ResponseStatus.TimedOut)
                            {
                                respTime = (DateTime.Now - ti).TotalMilliseconds;
                                respMsg  = "Timeout";
                            }
                            else if (resp.ResponseStatus != ResponseStatus.Completed)
                            {
                                respTime = null;
                                respMsg  = resp.ResponseStatus.ToString();
                                if (resp.ErrorException != null)
                                {
                                    respMsg = $"{respMsg}: {resp.ErrorException.Message}";
                                }
                            }
                            else if (resp.StatusCode != System.Net.HttpStatusCode.OK)
                            {
                                respTime = null;
                                respMsg  = resp.StatusDescription.ToString();
                            }
                            else
                            {
                                respTime = (DateTime.Now - ti).TotalMilliseconds;
                                respMsg  = "OK";
                            }
                        }
                        else
                        {
                            respTime = null;
                            respMsg  = "No response";
                        }
                    }
                    catch (Exception ex)
                    {
                        respMsg  = ex.Message;
                        respTime = null;
                    }
                }
                result.AddResponse(respTime, respMsg);
            }
            logger.LogInformation($"Connection test results: {result.Successful}/{result.Count} OK, Avg Time: {result.AvgResponseTime??Double.NaN}ms, Quality: {result.Quality*100:f0}%");
            return(result);
        }
Exemple #2
0
 public static ConnectionCheck Run(Uri uri, int count = DefaultAttempts, int timeout = MaxTimeout, ILogger logger = null) =>
 Run(RaciClient.Create(uri), count, timeout, logger);