Пример #1
0
        public IEnumerable <ISubscriptionHandler> GetHandlers(ModifiedFileModel modifiedFile)
        {
            List <ISubscriptionHandler> subscriptionHandlers = new List <ISubscriptionHandler>();
            HandlerResolver             resolver             = new HandlerResolver(this);

            foreach (Subscription subscription in GetSubscriptions(modifiedFile))
            {
                try
                {
                    var parameterSubstitutions = new Dictionary <string, string>
                    {
                        { "<trigger-repo>", modifiedFile.RepoName },
                        { "<trigger-branch>", modifiedFile.BranchName },
                        { "<trigger-path>", modifiedFile.FullPath },
                        { "<trigger-commit>", modifiedFile.CommitId }
                    };

                    ISubscriptionHandler subscriptionHandler = resolver.Resolve(subscription, parameterSubstitutions);
                    subscriptionHandlers.Add(subscriptionHandler);
                }
                catch (Exception e)
                {
                    Trace.TraceError(e.Message);
                }
            }

            return(subscriptionHandlers);
        }
        public void SubscriptionsJsonShouldHaveValidHandlers()
        {
            SubscriptionsModel subscriptionsModel = InitializeSubscriptionsModel();
            HandlerResolver    resolver           = new HandlerResolver(subscriptionsModel);

            foreach (Subscription subscription in subscriptionsModel.Subscriptions)
            {
                resolver.Resolve(subscription);
            }
        }
            public void ReturnNullWhenHandlerDoesNotMatch()
            {
                var sut            = new HandlerResolver();
                var handlerMock    = new Mock <IHandlerAsync <TestMessage> >();
                var handlerWrapper = new HandlerWrapper <TestMessage>(handlerMock.Object);

                sut.Add(handlerWrapper);

                var actual = sut.Resolve <AnotherTestMessage>();

                actual.Should().BeNull();
            }
            public void ReturnHandler()
            {
                var sut            = new HandlerResolver();
                var handlerMock    = new Mock <IHandlerAsync <TestMessage> >();
                var handlerWrapper = new HandlerWrapper <TestMessage>(handlerMock.Object);

                sut.Add(handlerWrapper);

                var actual = sut.Resolve <TestMessage>();

                actual.Should().Be(handlerMock.Object);
            }
Пример #5
0
        public IEnumerable <ISubscriptionHandler> GetHandlers(ModifiedFileModel modifiedFile)
        {
            List <ISubscriptionHandler> subscriptionHandlers = new List <ISubscriptionHandler>();
            HandlerResolver             resolver             = new HandlerResolver(this);

            foreach (Subscription subscription in GetSubscriptions(modifiedFile))
            {
                try
                {
                    ISubscriptionHandler subscriptionHandler = resolver.Resolve(subscription);
                    subscriptionHandlers.Add(subscriptionHandler);
                }
                catch (Exception e)
                {
                    Trace.TraceError(e.Message);
                }
            }

            return(subscriptionHandlers);
        }
        private async Task RunProcessLoop(CancellationToken tokenSourceToken)
        {
            _logger.Debug($"Starting listening for messages on '{_queue.Name}'");
            try
            {
                while (!tokenSourceToken.IsCancellationRequested)
                {
                    do
                    {
                        MessageAndMetadata messageAndMetadata = await _queue.Dequeue();

                        if (messageAndMetadata == null)
                        {
                            break;
                        }

                        try
                        {
                            foreach (var handler in _handlers.Resolve(messageAndMetadata.Message))
                            {
                                await handler(messageAndMetadata.Message);
                            }
                        }
                        catch (Exception e)
                        {
                            var metadata = GetMetadataForFailedHandling(messageAndMetadata.Metadata);
                            _logger.Warn(e, $"Failed handling message try {metadata.Tries}.");
                            await _queue.Enqueue(messageAndMetadata.Message, metadata);
                        }
                    } while (!tokenSourceToken.IsCancellationRequested);

                    try { await Task.Delay(1000, tokenSourceToken); }catch (OperationCanceledException) { }
                }
            }
            catch (Exception e)
            {
                _logger.Error(e, "Exception in processing loop");
            }
        }