Exemplo n.º 1
0
        private async Task ProcessRuleAsync(MonitoringRule rule)
        {
            try
            {
                var lastNotificationDate = await _notificationsCache.GetLastNotificationDateAsync(rule.Id);

                if (rule.NeedsNotification(lastNotificationDate))
                {
                    var action = new SendNotificationMonitoringAction();
                    rule.ActionsByTypeName[action.TypeName].CopyTo(action);

                    if (string.IsNullOrWhiteSpace(action.NotificationChannelId))
                    {
                        await _notificationSender.SendAsync(rule.GetNotificationText());
                    }
                    else
                    {
                        await _notificationSender.SendAsync(action.NotificationChannelId, rule.GetNotificationText());
                    }

                    await _notificationsCache.AddOrUpdateAsync(rule.Id,
                                                               DateTime.UtcNow.Add(MonitoringRuleExtensions.NotificationRemindPeriod));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to ProcessRule. {@Rule}", rule.Name);
            }
        }
Exemplo n.º 2
0
    public static bool NeedsNotification(this MonitoringRule rule, DateTime?lastNotificationDate)
    {
        lastNotificationDate ??= DateTime.MinValue;
        var action = new SendNotificationMonitoringAction();

        if (rule.ActionsByTypeName == null || !rule.ActionsByTypeName.TryGetValue(action.TypeName, out _))
        {
            return(false);
        }

        if (rule.CurrentState == null)
        {
            return(false);
        }

        if (rule.CurrentState.IsActive != rule.PrevState?.IsActive)
        {
            return(true);
        }

        var timeToRemind = DateTime.UtcNow - lastNotificationDate > NotificationRemindPeriod;

        if (rule.CurrentState.IsActive && timeToRemind)
        {
            return(true);
        }

        return(false);
    }