Пример #1
0
        private List <TriggerItem> GatherMetricsFromSF()
        {
            var triggerItems = new List <TriggerItem>();

            foreach (var application in _root.Applications)
            {
                foreach (var service in application.Services)
                {
                    foreach (var partition in service.Partitions)
                    {
                        foreach (var replica in partition.Replicas)
                        {
                            var triggerItem = new TriggerItem()
                            {
                                Application = application,
                                Service     = service,
                                Partition   = partition,
                                Replica     = replica
                            };
                            foreach (var metricReport in replica.DeploymentDetails.ReportedLoad)
                            {
                                triggerItem.Metrics.Add(new TriggerItemMetric()
                                {
                                    Name            = metricReport.Name,
                                    Value           = metricReport.Value,
                                    LastReportedUtc = metricReport.LastReportedUtc
                                });
                            }
                            if (triggerItem.Metrics.Any())
                            {
                                triggerItems.Add(triggerItem);
                                WatchdogEventSource.Current.TriggerItemReceived(triggerItem.ToString());
                            }
                        }
                    }
                }
            }

            return(triggerItems);
        }
        public void Execute(List <TriggerItem> triggerItems)
        {
            var         filteredTriggerItems = new List <TriggerItem>();
            TriggerItem defaultTriggerItem   = null;

            if (!triggerItems.Any())
            {
                return;
            }

            if (TriggerExpression == null)
            {
                defaultTriggerItem = triggerItems.First();
                WatchdogEventSource.Current.RuleFired(this.ToString(defaultTriggerItem), defaultTriggerItem.ToString(), false);
                return;
            }

            if (RuleFilter != null)
            {
                foreach (var triggerItem in triggerItems)
                {
                    var result = (RuleFilter.Execute(triggerItem) == "1" ? true : false);
                    if (result)
                    {
                        filteredTriggerItems.Add(triggerItem);
                    }
                }

                if (!filteredTriggerItems.Any())
                {
                    defaultTriggerItem = triggerItems.First();
                    _firstTriggered    = null;
                    _triggeringItem    = null;
                    WatchdogEventSource.Current.RuleFired(this.ToString(defaultTriggerItem), defaultTriggerItem.ToString(), false);
                    return;
                }
            }

            if (AggregateData)
            {
                filteredTriggerItems = CreateAggregatedData(filteredTriggerItems);
            }

            foreach (var filteredTriggerItem in filteredTriggerItems)
            {
                if (TriggerExpression.Execute(filteredTriggerItem) != 0)
                {
                    if (_firstTriggered == null)
                    {
                        _firstTriggered = DateTime.UtcNow;
                    }
                    _triggeringItem = filteredTriggerItem;
                    WatchdogEventSource.Current.RuleFired(this.ToString(filteredTriggerItem), filteredTriggerItem.ToString(), true);
                    return;
                }
            }

            defaultTriggerItem = triggerItems.First();
            _firstTriggered    = null;
            _triggeringItem    = null;
            WatchdogEventSource.Current.RuleFired(this.ToString(defaultTriggerItem), defaultTriggerItem.ToString(), false);
        }
        public void ExecuteAction()
        {
            if (_firstTriggered != null && (DateTime.UtcNow - TriggerPeriod) > _firstTriggered)
            {
                //  make sure no recent action was taken on this
                foreach (var actionItem in ActionHistory)
                {
                    if (actionItem.TriggerItem.Application.Properties.ApplicationName == _triggeringItem.Application.Properties.ApplicationName &&
                        actionItem.TriggerItem.Service.Properties.ServiceName == _triggeringItem.Service.Properties.ServiceName &&
                        (DateTime.UtcNow - ActionGracePeriod) < actionItem.TimeTriggeredUtc)
                    {
                        // we should do any new action yet - we have a grace period
                        return;
                    }
                }

                TriggerAction.Execute(_triggeringItem);
                WatchdogEventSource.Current.ActionFired(TriggerAction.GetType().FullName, this.ToString(_triggeringItem), _triggeringItem.ToString());

                ActionHistory.Add(new RuleActionItem()
                {
                    TimeTriggeredUtc = DateTime.UtcNow,
                    TriggerItem      = _triggeringItem,
                    TriggerAction    = TriggerAction
                });
            }
        }