Ejemplo n.º 1
0
            public async T.Task ProcessEventAsync(ISnEvent snEvent, CancellationToken cancel)
            {
                using (var op = SnTrace.Test.StartOperation($"ProcessEvent {GetType().Name} {snEvent.GetType().Name}"))
                {
                    await T.Task.Delay(50, cancel).ConfigureAwait(false);

                    op.Successful = true;
                }
            }
Ejemplo n.º 2
0
            protected override async T.Task SaveEventAsync(ISnEvent snEvent)
            {
                using (var op = SnTrace.Test.StartOperation("Save event"))
                {
                    await T.Task.Delay(10);

                    op.Successful = true;
                }

                await base.SaveEventAsync(snEvent);
            }
Ejemplo n.º 3
0
        private async Task FireEventAsync(ISnEvent snEvent, Task nodeObserverTask)
        {
            if (!IsFeatureEnabled(3))
            {
                return;
            }

            // Create a waiting list and add the 'nodeObserverTask' if there is.
            var syncTasks = new List <Task>();

            if (nodeObserverTask != null)
            {
                syncTasks.Add(nodeObserverTask);
            }

            // If the event is IAuditLogEvent, call async, memorize it, and do not wait.
            if (snEvent is IAuditLogEvent auditLogEvent)
            {
                if (AuditLogEventProcessor != null)
                {
                    syncTasks.Add(AuditLogEventProcessor.ProcessEventAsync(auditLogEvent));
                }
            }

            if (!(snEvent is IInternalEvent))
            {
                // Persists the event
                await SaveEventAsync(snEvent);

                // Call all async processors and forget them
                foreach (var processor in AsyncEventProcessors)
                    #pragma warning disable 4014
                {
                    processor.ProcessEventAsync(snEvent);
                }
                    #pragma warning restore 4014
            }

            // Wait for all synchronous tasks.
            if (syncTasks.Count > 0)
            {
                await Task.WhenAll(syncTasks.ToArray()).ConfigureAwait(false);
            }
        }
        /// <inheritdoc/>
        public IEnumerable <WebHookSubscriptionInfo> GetRelevantSubscriptions(ISnEvent snEvent)
        {
            //TODO: implement a subscription cache that is invalidated when a subscription changes
            // Do NOT cache nodes, their data is already cached. Cache only ids, paths, or trees.

            List <WebHookSubscriptionInfo> allSubs;

            // load subscriptions in elevated mode because this is a system feature
            using (new SystemAccount())
            {
                // ReSharper disable once RedundantBoolCompare
                allSubs = Content.All.DisableAutofilters().Where(c =>
                                                                 c.InTree("/Root/System/WebHooks") &&
                                                                 c.ContentHandler is WebHookSubscription &&
                                                                 (bool)c["Enabled"] == true)
                          .AsEnumerable()
                          .Select(c => (WebHookSubscription)c.ContentHandler)
                          .SelectMany(sub =>
                {
                    // prefilter: check if this event is relevant for the subscription
                    var eventTypes = sub.GetRelevantEventTypes(snEvent);

                    // handle multiple relevant event types by adding the subscription multiple times
                    return(eventTypes.Select(et => new WebHookSubscriptionInfo(sub, et)));
                })
                          .Where(si => si != null && si.Subscription.IsValid)
                          .ToList();
            }

            if (!allSubs.Any())
            {
                return(Array.Empty <WebHookSubscriptionInfo>());
            }

            // use the already constructed Content instance if possible
            var content = snEvent.NodeEventArgs.SourceNode is GenericContent gc
                ? gc.Content
                : Content.Create(snEvent.NodeEventArgs.SourceNode);

            // filter by the query defined by the subscriber
            var pe = new PredicationEngine(content);

            return(allSubs.Where(sub => pe.IsTrue(sub.Subscription.FilterQuery)).ToList());
        }
Ejemplo n.º 5
0
        public async Task ProcessEventAsync(ISnEvent snEvent, CancellationToken cancel)
        {
            var node = snEvent.NodeEventArgs.SourceNode;

            // currently we deal with content-related events only
            if (node == null)
            {
                return;
            }

            var subscriptions = _subscriptionStore.GetRelevantSubscriptions(snEvent)
                                .Where(si => si.Subscription.Enabled && si.Subscription.IsValid)
                                .ToArray();

            if (subscriptions.Any())
            {
                _logger?.LogTrace($"Sending webhook events for subscriptions: " +
                                  $"{string.Join(", ", subscriptions.Select(si => si.Subscription.Name + " (" + si.EventType + ")"))}");
            }

            //TODO: extend webhook request payload with event-specific info
            var eventArgs       = snEvent.NodeEventArgs as NodeEventArgs;
            var previousVersion = eventArgs.GetPreviousVersion();

            var sendingTasks = subscriptions
                               .Select(si => _webHookClient.SendAsync(
                                           si.Subscription.Url,
                                           si.EventType.ToString(),
                                           node.Id,
                                           si.Subscription.Id,
                                           si.Subscription.HttpMethod,
                                           GetPayload(si.Subscription, si.EventType, node, previousVersion),
                                           si.Subscription.HttpHeaders,
                                           cancel));

            //TODO: handle responses: webhook statistics implementation

            await sendingTasks.WhenAll().ConfigureAwait(false);
        }
Ejemplo n.º 6
0
        public IEnumerable <WebHookSubscriptionInfo> GetRelevantSubscriptions(ISnEvent snEvent)
        {
            var node = snEvent.NodeEventArgs.SourceNode;

            if (node == null)
            {
                return(Array.Empty <WebHookSubscriptionInfo>());
            }

            var pe = new PredicationEngine(Content.Create(node));

            // filter the hardcoded subscription list: return the ones that
            // match the current content
            return(Subscriptions.SelectMany(sub =>
            {
                var eventTypes = sub.GetRelevantEventTypes(snEvent);

                // handle multiple relevant event types by adding the subscription multiple times
                return eventTypes.Select(et => new WebHookSubscriptionInfo(sub, et));
            }).Where(si => si != null &&
                     pe.IsTrue(si.Subscription.FilterQuery) &&
                     si.Subscription.Enabled &&
                     si.Subscription.IsValid));
        }
Ejemplo n.º 7
0
 public Task FireEventAsync(ISnEvent snEvent)
 {
     return(FireEventAsync(snEvent, null));
 }
Ejemplo n.º 8
0
 public Task FireEventAsync(ISnEvent snEvent)
 {
     return(Task.CompletedTask);
 }
Ejemplo n.º 9
0
 protected virtual Task SaveEventAsync(ISnEvent snEvent)
 {
     //TODO:event: Not implemented yet
     return(Task.CompletedTask);
 }
Ejemplo n.º 10
0
 public IEnumerable <WebHookSubscriptionInfo> GetRelevantSubscriptions(ISnEvent snEvent)
 {
     return(Array.Empty <WebHookSubscriptionInfo>());
 }