Example #1
0
        public static async Task <StatusReport> WaitForHealthCheckAsync(this Route53Helper r53h, string name,
                                                                        HealthCheckStatus status, //Unhealthy, Healthy
                                                                        int timeout_s)
        {
            var sw = Stopwatch.StartNew();
            var hc = await r53h.GetHealthCheckAsync(name, throwIfNotFound : true);

            StatusReport report;
            var          healthyStatus   = hc.HealthCheckConfig.Inverted ? HealthCheckStatus.Unhealthy : HealthCheckStatus.Healthy;
            var          unHealthyStatus = hc.HealthCheckConfig.Inverted ? HealthCheckStatus.Healthy : HealthCheckStatus.Unhealthy;

            do
            {
                var hcs = await r53h.GetHealthCheckStatusAsync(hc.Id);

                report = hcs.HealthCheckObservations.OrderByDescending(x => x.StatusReport.CheckedTime).First().StatusReport;

                if ((report.Status.ContainsAny("Success", HealthCheckStatus.Healthy.ToString()) && status == healthyStatus) ||
                    (report.Status.ContainsAny("Failure", HealthCheckStatus.Unhealthy.ToString()) && status == unHealthyStatus))
                {
                    return(report);
                }

                await _locker.Lock(() => Task.Delay(1000));
            }while (sw.ElapsedMilliseconds < (timeout_s * 1000));

            throw new Exception($"Health Check '{name}' coudn't reach '{status}' status within {timeout_s} [s], last state was: '{report.JsonSerialize(Newtonsoft.Json.Formatting.Indented)}'.");
        }
Example #2
0
        public static async Task <HealthCheck> UpsertCloudWatchHealthCheckAsync(this Route53Helper r53h,
                                                                                string name,
                                                                                string alarmName,
                                                                                string alarmRegion,
                                                                                bool inverted = false,
                                                                                InsufficientDataHealthStatus insufficientDataHealthStatus = null,
                                                                                bool throwIfNotFound = true,
                                                                                CancellationToken cancellationToken = default(CancellationToken))
        {
            var hc = await r53h.GetHealthCheckAsync(name, throwIfNotFound : throwIfNotFound, cancellationToken : cancellationToken);

            if (hc == null)
            {
                hc = (await r53h.CreateCloudWatchHealthCheckAsync(
                          $"{name}-{Guid.NewGuid().ToString()}",
                          alarmName,
                          alarmRegion,
                          inverted,
                          insufficientDataHealthStatus,
                          cancellationToken)).HealthCheck;

                await r53h.ChangeTagsForHealthCheckAsync(hc.Id, new Tag[] { new Tag()
                                                                            {
                                                                                Key = "Name", Value = name
                                                                            } });

                await Task.Delay(1000); //ensure record was created

                return(hc);
            }

            var response = await r53h.UpdateHealthCheckAsync(new UpdateHealthCheckRequest()
            {
                HealthCheckId   = hc.Id,
                AlarmIdentifier = new AlarmIdentifier()
                {
                    Name   = alarmName,
                    Region = alarmRegion,
                },
                Inverted = inverted,
                InsufficientDataHealthStatus = insufficientDataHealthStatus ?? InsufficientDataHealthStatus.Unhealthy
            }, cancellationToken);

            await Task.Delay(1000); //ensure record was updated

            return(response.HealthCheck);
        }
Example #3
0
        public static async Task <DeleteHealthCheckResponse> DeleteHealthCheckByNameAsync(
            this Route53Helper r53h,
            string name,
            bool throwIfNotFound,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var hc = await r53h.GetHealthCheckAsync(name, throwIfNotFound : throwIfNotFound, cancellationToken : cancellationToken);

            if (hc == null && !throwIfNotFound)
            {
                return new DeleteHealthCheckResponse()
                       {
                           HttpStatusCode = System.Net.HttpStatusCode.NotFound
                       }
            }
            ;

            return(await r53h.DeleteHealthCheckAsync(hc.Id, cancellationToken));
        }
Example #4
0
        public static async Task <HealthCheck> UpsertHealthCheckAsync(this Route53Helper r53h,
                                                                      string name,
                                                                      string uri,
                                                                      int port,
                                                                      string path,
                                                                      int failureTreshold,
                                                                      string searchString  = null,
                                                                      bool throwIfNotFound = true,
                                                                      CancellationToken cancellationToken = default(CancellationToken))
        {
            var hc = await r53h.GetHealthCheckAsync(name, throwIfNotFound : throwIfNotFound, cancellationToken : cancellationToken);

            if (hc == null)
            {
                var result = await r53h.CreateHealthCheckAsync(
                    name,
                    uri,
                    port,
                    path,
                    searchString,
                    failureTreshold : failureTreshold);

                return(result.HealthCheck);
            }

            var response = await r53h.UpdateHealthCheckAsync(new UpdateHealthCheckRequest()
            {
                HealthCheckId            = hc.Id,
                FullyQualifiedDomainName = uri,
                Port             = port,
                ResourcePath     = path,
                SearchString     = searchString,
                FailureThreshold = failureTreshold,
                EnableSNI        = hc.HealthCheckConfig.EnableSNI,
            }, cancellationToken);

            return(response.HealthCheck);
        }
Example #5
0
 public static async Task <bool> HealthCheckExistsAsync(this Route53Helper r53h, string name, bool throwIfNotFound = true, CancellationToken cancellationToken = default(CancellationToken))
 => (await r53h.GetHealthCheckAsync(name, throwIfNotFound: false, cancellationToken: cancellationToken)) != null;