Exemplo n.º 1
0
        public async Task Should_Store_Checks_For_Separate_Services(IHealthCheckStorage storage)
        {
            var unhealthy = new HealthCheckStatus(HealthStatus.Unhealthy);
            var healthy   = new HealthCheckStatus(HealthStatus.Healthy);

            await storage.CheckedAsync("s1", unhealthy);

            await storage.CheckedAsync("s2", healthy);

            Assert.Equal(unhealthy, await storage.GetCurrentStatusAsync("s1"));
            Assert.Equal(healthy, await storage.GetCurrentStatusAsync("s2"));
        }
Exemplo n.º 2
0
        public async Task Should_Replace_Older_Value(IHealthCheckStorage storage)
        {
            var olderCheck = new HealthCheckStatus(HealthStatus.Unhealthy)
            {
                DateTime = DateTime.UtcNow.AddSeconds(-1)
            };
            var newerCheck = new HealthCheckStatus(HealthStatus.Healthy);

            await storage.CheckedAsync("test", olderCheck);

            Assert.Equal(olderCheck, await storage.GetCurrentStatusAsync("test"));

            await storage.CheckedAsync("test", newerCheck);

            Assert.Equal(newerCheck, await storage.GetCurrentStatusAsync("test"));
        }
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken())
        {
            var healthStatus = await _healthCheckStorage.GetCurrentStatusAsync(ServiceName);

            if (healthStatus == null || healthStatus.DateTime + CheckPeriod < DateTime.UtcNow)
            {
                healthStatus = await _healthCheckService.CheckHealthAsync();

                await _healthCheckStorage.CheckedAsync(ServiceName, healthStatus);
            }
            return(healthStatus.Status switch
            {
                HealthStatus.Healthy => HealthCheckResult.Healthy(),
                _ => HealthCheckResult.Unhealthy(healthStatus.ErrorMessage)
            });
Exemplo n.º 4
0
        public async Task SendSmsAsync(string to, string body)
        {
            if (string.IsNullOrWhiteSpace(to))
            {
                throw new ArgumentException(nameof(to));
            }

            if (string.IsNullOrWhiteSpace(body))
            {
                throw new ArgumentException(nameof(body));
            }

            try
            {
                var messageResource = await MessageResource.CreateAsync(
                    new PhoneNumber(to),
                    @from : new PhoneNumber(_options.Value.From),
                    body : body
                    );

                _logger.LogDebug("Sent message to {to}; message Sid: {sid}",
                                 to, messageResource.Sid);

                await _healthCheckStorage.CheckedAsync(ISmsSender.ServiceName,
                                                       new HealthCheckStatus(HealthStatus.Healthy));
            }
            catch (ApiException e)
            {
                _logger.LogError(e, "Failed to send SMS to {to}, Twilio Error {code} - {info}", to, e.Code, e.MoreInfo);

                await _healthCheckStorage.CheckedAsync(ISmsSender.ServiceName,
                                                       new HealthCheckStatus(HealthStatus.Unhealthy, e.Message));

                throw new SmsSenderException($"Failed to send SMS to {to}", e);
            }
        }