async Task Run <T, I>(CancellationToken token) where I : AbstractIndexCreationTask, new()
        {
            var config   = rawEndpointFactory.CreateFailedAuditsSender("ImportFailedAudits");
            var endpoint = await RawEndpoint.Start(config).ConfigureAwait(false);

            await auditIngestor.Initialize(endpoint).ConfigureAwait(false);

            var succeeded = 0;
            var failed    = 0;

            using (var session = store.OpenAsyncSession())
            {
                var query = session.Query <T, I>();
                using (var ie = await session.Advanced.StreamAsync(query, token)
                                .ConfigureAwait(false))
                {
                    while (!token.IsCancellationRequested && await ie.MoveNextAsync().ConfigureAwait(false))
                    {
                        FailedTransportMessage dto = ((dynamic)ie.Current.Document).Message;
                        try
                        {
                            var messageContext       = new MessageContext(dto.Id, dto.Headers, dto.Body, EmptyTransaction, EmptyTokenSource, EmptyContextBag);
                            var taskCompletionSource = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
                            messageContext.SetTaskCompletionSource(taskCompletionSource);

                            await auditIngestor.Ingest(new List <MessageContext> {
                                messageContext
                            }).ConfigureAwait(false);

                            await taskCompletionSource.Task.ConfigureAwait(false);

                            await store.AsyncDatabaseCommands.DeleteAsync(ie.Current.Key, null, token)
                            .ConfigureAwait(false);

                            succeeded++;
                            if (Logger.IsDebugEnabled)
                            {
                                Logger.Debug($"Successfully re-imported failed audit message {dto.Id}.");
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            // no-op
                        }
                        catch (Exception e)
                        {
                            Logger.Error($"Error while attempting to re-import failed audit message {dto.Id}.", e);
                            failed++;
                        }
                    }
                }
            }

            Logger.Info($"Done re-importing failed audits. Successfully re-imported {succeeded} messages. Failed re-importing {failed} messages.");

            if (failed > 0)
            {
                Logger.Warn($"{failed} messages could not be re-imported. This could indicate a problem with the data. Contact Particular support if you need help with recovering the messages.");
            }
        }
    public async Task <IStartableRawEndpoint> Create()
    {
        startable = await RawEndpoint.Create(config);

        config = null;
        return(this);
    }
Ejemplo n.º 3
0
    static async Task Main()
    {
        Console.Title = "Case00041163.OccasionalSubscriber";

        var endpointConfiguration = RawEndpointConfiguration.Create(Console.Title, (context, messages) => Task.CompletedTask, "error");

        endpointConfiguration.UseTransport <MsmqTransport>();

        var endpointInstance = await RawEndpoint.Start(endpointConfiguration)
                               .ConfigureAwait(false);

        var subscriptionMessage = ControlMessageFactory.Create(MessageIntentEnum.Subscribe);

        subscriptionMessage.Headers[Headers.SubscriptionMessageType]    = typeof(MyEvent).AssemblyQualifiedName;
        subscriptionMessage.Headers[Headers.ReplyToAddress]             = "Case00041163.OccasionalSubscriber@FAKEPC";
        subscriptionMessage.Headers[Headers.SubscriberTransportAddress] = "Case00041163.OccasionalSubscriber@FAKEPC";
        subscriptionMessage.Headers[Headers.SubscriberEndpoint]         = "Case00041163.OccasionalSubscriber";
        subscriptionMessage.Headers[Headers.TimeSent]           = DateTimeExtensions.ToWireFormattedString(DateTime.UtcNow);
        subscriptionMessage.Headers[Headers.NServiceBusVersion] = "5.6.4";

        var operation = new TransportOperation(subscriptionMessage, new UnicastAddressTag("Case00041163.Publisher"));

        await endpointInstance.Dispatch(new TransportOperations(operation), new TransportTransaction(), new ContextBag()).ConfigureAwait(false);

        await endpointInstance.Stop().ConfigureAwait(false);
    }
Ejemplo n.º 4
0
        public virtual async Task Run(string forwardingBatchId, Predicate <MessageContext> filter, CancellationToken cancellationToken, int?expectedMessageCount)
        {
            IReceivingRawEndpoint         processor    = null;
            CancellationTokenRegistration?registration = null;

            try
            {
                shouldProcess      = filter;
                targetMessageCount = expectedMessageCount;
                actualMessageCount = 0;

                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("Starting receiver");
                }

                var config = createEndpointConfiguration();
                syncEvent            = new TaskCompletionSource <bool>();
                stopCompletionSource = new TaskCompletionSource <bool>();
                registration         = cancellationToken.Register(() => { Task.Run(() => syncEvent.TrySetResult(true), CancellationToken.None).Ignore(); });

                processor = await RawEndpoint.Start(config).ConfigureAwait(false);

                Log.Info($"Forwarder for batch {forwardingBatchId} started receiving messages from {processor.TransportAddress}.");

                if (!expectedMessageCount.HasValue)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug("Running in timeout mode. Starting timer.");
                    }

                    timer.Change(TimeSpan.FromSeconds(45), Timeout.InfiniteTimeSpan);
                }
            }
            finally
            {
                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat($"Waiting for forwarder for batch {forwardingBatchId} to finish.");
                }

                await syncEvent.Task.ConfigureAwait(false);

                registration?.Dispose();
                if (processor != null)
                {
                    await processor.Stop().ConfigureAwait(false);
                }

                Log.Info($"Forwarder for batch {forwardingBatchId} finished forwarding all messages.");

                await Task.Run(() => stopCompletionSource.TrySetResult(true), CancellationToken.None).ConfigureAwait(false);
            }

            if (endedPrematurely || cancellationToken.IsCancellationRequested)
            {
                throw new Exception("We are in the process of shutting down. Safe to ignore.");
            }
        }
        static async Task <IRawEndpointInstance> StartMonitor()
        {
            var queueMonitorContext = new DefaultMetricsContext("QueueLengthMonitor");

            new MetricsConfig(queueMonitorContext)
            .WithHttpEndpoint("http://localhost:7777/QueueLengthMonitor/")
            .WithReporting(r =>
            {
                r.WithReport(
                    new PerformanceCounterReporter(x => new CounterInstanceName("Queue Length", x.MetricName)),
                    TimeSpan.FromSeconds(5), Filter.New.WhereContext(c => c == "QueueLengthMonitor" || c == "QueueState"));
            });

            var monitor = new Monitor(queueMonitorContext, new QueueLength());
            var config  = RawEndpointConfiguration.Create("QueueLengthMonitor", monitor.OnMessage);

            config.LimitMessageProcessingConcurrencyTo(1);
            config.UseTransport <MsmqTransport>();
            //config.UseTransport<RabbitMQTransport>().ConnectionString("host=localhost");
            config.SendFailedMessagesTo("error");

            var endpoint = await RawEndpoint.Start(config);

            return(endpoint);
        }
Ejemplo n.º 6
0
        public async Task EnsureStarted(CancellationToken cancellationToken)
        {
            try
            {
                await startStopSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

                if (ingestionEndpoint != null)
                {
                    return; //Already started
                }

                var rawConfiguration = rawEndpointFactory.CreateRawEndpointConfiguration(
                    errorQueue,
                    (messageContext, dispatcher) => errorIngestor.Ingest(messageContext));

                rawConfiguration.Settings.Set("onCriticalErrorAction", (Func <ICriticalErrorContext, Task>)OnCriticalErrorAction);

                rawConfiguration.CustomErrorHandlingPolicy(new ErrorIngestionFaultPolicy(importFailuresHandler));

                var startableRaw = await RawEndpoint.Create(rawConfiguration).ConfigureAwait(false);

                await errorIngestor.Initialize(startableRaw).ConfigureAwait(false);

                ingestionEndpoint = await startableRaw.Start()
                                    .ConfigureAwait(false);
            }
            finally
            {
                startStopSemaphore.Release();
            }
        }
Ejemplo n.º 7
0
        public async Task Run(string username)
        {
            var transportSettings      = MapSettings(settings);
            var transportCustomization = settings.LoadTransportCustomization();
            var factory = new RawEndpointFactory(settings, transportSettings, transportCustomization);

            var config = factory.CreateAuditIngestor(settings.AuditQueue, (context, dispatcher) => Task.CompletedTask);

            if (settings.SkipQueueCreation)
            {
                log.Info("Skipping queue creation");
            }
            else
            {
                var additionalQueues = new List <string>
                {
                    $"{settings.ServiceName}.Errors"
                };
                if (settings.ForwardAuditMessages && settings.AuditLogQueue != null)
                {
                    additionalQueues.Add(settings.AuditLogQueue);
                }
                config.AutoCreateQueues(additionalQueues.ToArray(), username);
            }

            //No need to start the raw endpoint to create queues
            await RawEndpoint.Create(config).ConfigureAwait(false);
        }
Ejemplo n.º 8
0
        internal static async Task <(IReceivingRawEndpoint, StateStore)> SetupEndpoint(Action <Guid, Message, Message[]> messageProcessed)
        {
            var storageTable = await PrepareStorageTable();

            var stateStore     = new StateStore(storageTable);
            var handlerInvoker = new HandlerInvoker(stateStore);

            var endpointConfiguration = RawEndpointConfiguration.Create(
                endpointName: EndpointName,
                onMessage: async(c, d) =>
            {
                var message = Serializer.Deserialize(c.Body, c.Headers);

                var outputMessages = await handlerInvoker.Process(message);

                var runId = Guid.Parse(c.Headers["Message.RunId"]);

                messageProcessed(runId, message, outputMessages);

                await d.Send(outputMessages, runId);
            },
                poisonMessageQueue: "error");

            endpointConfiguration.UseTransport <LearningTransport>()
            .Transactions(TransportTransactionMode.ReceiveOnly);

            var defaultFactory = LogManager.Use <DefaultFactory>();

            defaultFactory.Level(LogLevel.Debug);

            var endpoint = await RawEndpoint.Start(endpointConfiguration);

            return(endpoint, stateStore);
        }
Ejemplo n.º 9
0
        public async Task EnsureStarted(CancellationToken cancellationToken)
        {
            try
            {
                await startStopSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

                if (ingestionEndpoint != null)
                {
                    return; //Already started
                }

                var rawConfiguration = rawEndpointFactory.CreateAuditIngestor(inputEndpoint, onMessage);

                rawConfiguration.Settings.Set("onCriticalErrorAction", (Func <ICriticalErrorContext, Task>)OnCriticalErrorAction);

                rawConfiguration.CustomErrorHandlingPolicy(errorHandlingPolicy);

                var startableRaw = await RawEndpoint.Create(rawConfiguration).ConfigureAwait(false);

                await initialize(startableRaw).ConfigureAwait(false);

                ingestionEndpoint = await startableRaw.Start()
                                    .ConfigureAwait(false);
            }
            finally
            {
                startStopSemaphore.Release();
            }
        }
    RawEndpointConfiguration PrepareConfig(string inputQueue, string poisonMessageQueueName, Action <TransportExtensions <T> > transportCustomization,
                                           Func <MessageContext, IDispatchMessages, Task> onMessage, PoisonMessageHandling poisonMessageHandling, int?maximumConcurrency,
                                           int immediateRetries, int delayedRetries, int circuitBreakerThreshold, bool autoCreateQueue, string autoCreateQueueIdentity)
    {
        var circuitBreaker = new RepeatedFailuresCircuitBreaker(inputQueue, circuitBreakerThreshold, e =>
        {
            logger.Error($"Persistent error while processing messages in {inputQueue}. Entering throttled mode.", e);
            Task.Run(async() =>
            {
                await transitionSemaphore.WaitAsync().ConfigureAwait(false);
                try
                {
                    var oldEndpoint     = endpoint;
                    var throttledConfig = PrepareThrottledConfig(inputQueue, poisonMessageQueueName, transportCustomization, onMessage, poisonMessageHandling, maximumConcurrency, immediateRetries, circuitBreakerThreshold, delayedRetries);
                    var newEndpoint     = await RawEndpoint.Start(throttledConfig).ConfigureAwait(false);
                    endpoint            = newEndpoint;
                    await oldEndpoint.Stop().ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    logger.Error("Error when entering throttled mode", ex);
                }
                finally
                {
                    transitionSemaphore.Release();
                }
            });
        });
        var regularConfig = RawEndpointConfiguration.Create(inputQueue, async(context, dispatcher) =>
        {
            await onMessage(context, dispatcher).ConfigureAwait(false);
            circuitBreaker.Success();
        }, poisonMessageQueueName);

        regularConfig.CustomErrorHandlingPolicy(new RegularModePolicy(inputQueue, circuitBreaker, poisonMessageHandling, immediateRetries, delayedRetries));

        Task onCriticalError(ICriticalErrorContext context)
        {
            logger.Fatal($"The receiver for queue {inputQueue} has encountered a severe error that is likely related to the connectivity with the broker or the broker itself.");
            return(Task.CompletedTask);
        }

        regularConfig.Settings.Set("onCriticalErrorAction", (Func <ICriticalErrorContext, Task>)onCriticalError);

        var transport = regularConfig.UseTransport <T>();

        transportCustomization(transport);
        if (autoCreateQueue)
        {
            regularConfig.AutoCreateQueue(autoCreateQueueIdentity);
        }
        if (maximumConcurrency.HasValue)
        {
            regularConfig.LimitMessageProcessingConcurrencyTo(maximumConcurrency.Value);
        }
        return(regularConfig);
    }
Ejemplo n.º 11
0
        public virtual async Task Run(Predicate <MessageContext> filter, CancellationToken cancellationToken, int?expectedMessageCount = null)
        {
            IReceivingRawEndpoint         processor    = null;
            CancellationTokenRegistration?registration = null;

            try
            {
                Log.DebugFormat("Started. Expectected message count {0}", expectedMessageCount);

                if (expectedMessageCount.HasValue && expectedMessageCount.Value == 0)
                {
                    return;
                }

                shouldProcess      = filter;
                targetMessageCount = expectedMessageCount;
                actualMessageCount = 0;
                Log.DebugFormat("Starting receiver");

                var config = createEndpointConfiguration();
                syncEvent            = new TaskCompletionSource <bool>();
                stopCompletionSource = new TaskCompletionSource <bool>();
                registration         = cancellationToken.Register(() => { Task.Run(() => syncEvent.TrySetResult(true), CancellationToken.None).Ignore(); });

                processor = await RawEndpoint.Start(config).ConfigureAwait(false);

                if (!expectedMessageCount.HasValue)
                {
                    Log.Debug("Running in timeout mode. Starting timer");
                    timer.Change(TimeSpan.FromSeconds(45), Timeout.InfiniteTimeSpan);
                }

                Log.InfoFormat("{0} started", GetType().Name);
            }
            finally
            {
                Log.DebugFormat("Waiting for {0} finish", GetType().Name);
                await syncEvent.Task.ConfigureAwait(false);

                registration?.Dispose();
                if (processor != null)
                {
                    await processor.Stop().ConfigureAwait(false);
                }

                await Task.Run(() => stopCompletionSource.TrySetResult(true), CancellationToken.None).ConfigureAwait(false);

                Log.DebugFormat("{0} finished", GetType().Name);
            }

            if (endedPrematurelly || cancellationToken.IsCancellationRequested)
            {
                throw new Exception("We are in the process of shutting down. Safe to ignore.");
            }
        }
        /// <summary>
        /// Starts the endpoint.
        /// </summary>
        public async Task Start()
        {
            var outConfig = RawEndpointConfiguration.Create(storageQueueName, OnOutgoingMessage, poisonMessageQueue);

            outConfig.LimitMessageProcessingConcurrencyTo(1);
            transportCustomization(outConfig.UseTransport <T>());
            outConfig.CustomErrorHandlingPolicy(new RetryForeverPolicy());
            outConfig.AutoCreateQueue();

            outEndpoint = await RawEndpoint.Start(outConfig).ConfigureAwait(false);
        }
Ejemplo n.º 13
0
        public override async Task Start(CancellationToken token)
        {
            var config = RawEndpointConfiguration.Create("poison", OnMessage, "poison");

            config.AutoCreateQueue();
            config.CustomErrorHandlingPolicy(new IgnoreErrorsPolicy());
            var transport = config.UseTransport <TestTransport>();

            transportConfiguration(transport);

            endpoint = await RawEndpoint.Start(config);
        }
Ejemplo n.º 14
0
        static async Task Main(string[] args)
        {
            var senderConfig = RawEndpointConfiguration.Create("DummyHandler", OnMessage, "error");
            var transport    = senderConfig.UseTransport <AzureServiceBusTransport>();

            transport.ConnectionString(Environment.GetEnvironmentVariable("AzureServiceBus_ConnectionString"));
            senderConfig.LimitMessageProcessingConcurrencyTo(10);

            var sender = await RawEndpoint.Start(senderConfig).ConfigureAwait(false);

            Console.ReadLine();
        }
    RawEndpointConfiguration PrepareThrottledConfig(string inputQueue, string poisonMessageQueueName, Action <TransportExtensions <T> > transportCustomization,
                                                    Func <MessageContext, IDispatchMessages, Task> onMessage, PoisonMessageHandling poisonMessageHandling, int?maximumConcurrency,
                                                    int immediateRetries, int delayedRetries, int circuitBreakerThreshold)
    {
        var switchedBack    = false;
        var throttledConfig = RawEndpointConfiguration.Create(inputQueue, async(context, dispatcher) =>
        {
            await onMessage(context, dispatcher);
            if (switchedBack)
            {
                return;
            }
            await transitionSemaphore.WaitAsync().ConfigureAwait(false);
            Task.Run(async() =>
            {
                logger.Info("Exiting throttled mode.");
                try
                {
                    var oldEndpoint   = endpoint;
                    var regularConfig = PrepareConfig(inputQueue, poisonMessageQueueName, transportCustomization, onMessage, poisonMessageHandling, maximumConcurrency, immediateRetries, delayedRetries, circuitBreakerThreshold, false, null);
                    var newEndpoint   = await RawEndpoint.Start(regularConfig).ConfigureAwait(false);
                    endpoint          = newEndpoint;
                    await oldEndpoint.Stop().ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    logger.Error("Error when exiting throttled mode", e);
                }
                finally
                {
                    transitionSemaphore.Release();
                }
            }).Ignore();
            switchedBack = true;
        }, poisonMessageQueueName);

        throttledConfig.CustomErrorHandlingPolicy(new ThrottledModePolicy(inputQueue, immediateRetries));

        Task onCriticalError(ICriticalErrorContext context)
        {
            logger.Fatal($"The receiver for queue {inputQueue} has encountered a severe error that is likely related to the connectivity with the broker or the broker itself.");
            return(Task.CompletedTask);
        }

        throttledConfig.Settings.Set("onCriticalErrorAction", (Func <ICriticalErrorContext, Task>)onCriticalError);

        var transport = throttledConfig.UseTransport <T>();

        transportCustomization(transport);
        throttledConfig.LimitMessageProcessingConcurrencyTo(1);
        return(throttledConfig);
    }
Ejemplo n.º 16
0
    public async Task Start()
    {
        leftStartable = await RawEndpoint.Create(leftConfig).ConfigureAwait(false);

        rightStartable = await RawEndpoint.Create(rightConfig).ConfigureAwait(false);

        leftSubscribeRouter  = CreateSubscribeRouter(leftStartable.Settings.Get <TransportInfrastructure>());
        rightSubscribeRouter = CreateSubscribeRouter(rightStartable.Settings.Get <TransportInfrastructure>());

        leftEndpoint = await leftStartable.Start().ConfigureAwait(false);

        rightEndpoint = await rightStartable.Start().ConfigureAwait(false);
    }
Ejemplo n.º 17
0
 public static async Task InitServerEndpoint(string endpointName)
 {
     if (serverEndpoint == null)
     {
         var senderConfig = RawEndpointConfiguration.Create(
             endpointName: endpointName,
             onMessage: OnMessage,
             poisonMessageQueue: "error");
         senderConfig.UseTransport <MsmqTransport>();
         serverEndpoint = await RawEndpoint.Start(senderConfig)
                          .ConfigureAwait(false);
     }
 }
Ejemplo n.º 18
0
 public async static Task InitSubTwoEndpoint(string endpointName, Func <MessageContext, IDispatchMessages, Task> func)
 {
     if (subscriber2Endpoint == null)
     {
         var senderConfig = RawEndpointConfiguration.Create(
             endpointName: endpointName,
             onMessage: func,
             poisonMessageQueue: "error");
         senderConfig.UseTransport <MsmqTransport>();
         senderConfig.AutoCreateQueue();
         subscriber2Endpoint = await RawEndpoint.Start(senderConfig)
                               .ConfigureAwait(false);
     }
 }
Ejemplo n.º 19
0
        public override async Task Start(CancellationToken token)
        {
            var startable = await RawEndpoint.Create(config).ConfigureAwait(false);

            if (!sendOnly)
            {
                endpoint = await startable.Start().ConfigureAwait(false);
            }
            else
            {
                endpoint = startable;
            }

            await onStarting(endpoint).ConfigureAwait(false);
        }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var endpointConfiguration = RawEndpointConfiguration.Create(attribute.QueueName, OnMessage, poisonMessageQueue);

            if (Directory.Exists(nsbLogPath))
            {
                LogManager.Use <DefaultFactory>().Directory(nsbLogPath);
            }

            var connectionString = AmbientConnectionStringProvider.Instance.GetConnectionString(attribute.Connection);

            endpointConfiguration.UseTransport <AzureStorageQueueTransport>().ConnectionString(connectionString);

            endpointConfiguration.DefaultErrorHandlingPolicy(poisonMessageQueue, immediateRetryCount);

            endpoint = await RawEndpoint.Start(endpointConfiguration).ConfigureAwait(false);
        }
Ejemplo n.º 21
0
    static async Task <List <MessageContext> > RunResubscriberOnce(params MessageContext[] input)
    {
        await Cleanup("ResubscriberTest", "ResubscriberTest.Resubscriber", "poison").ConfigureAwait(false);

        var done           = new TaskCompletionSource <bool>();
        var output         = new List <MessageContext>();
        var listenerConfig = RawEndpointConfiguration.Create("ResubscriberTest", (context, dispatcher) =>
        {
            if (context.Headers[Headers.SubscriptionMessageType] == "stop")
            {
                done.SetResult(true);
            }
            else
            {
                output.Add(context);
            }

            return(Task.CompletedTask);
        }, "poison");

        listenerConfig.UseTransport <MsmqTransport>();
        listenerConfig.LimitMessageProcessingConcurrencyTo(1);
        listenerConfig.AutoCreateQueue();

        var listener = await RawEndpoint.Start(listenerConfig);

        var resubscriber = await Resubscriber <MsmqTransport> .Create("ResubscriberTest", TimeSpan.FromSeconds(3), t => { });

        foreach (var messageContext in input)
        {
            await resubscriber.InterceptMessageForwarding("ResubscriberTest", messageContext, NoopDispatch, NoopForward).ConfigureAwait(false);
        }

        await resubscriber.InterceptMessageForwarding("ResubscriberTest", CreateTerminatingContext(), NoopDispatch, NoopForward);

        await resubscriber.Start();

        await done.Task;

        await resubscriber.Stop();

        await listener.Stop();

        return(output);
    }
Ejemplo n.º 22
0
        internal async Task Start(Dictionary <string, QueueInfo> queues, string errorQueue)
        {
            instances = new List <IRawEndpointInstance>();
            var barrier = new TaskCompletionSource <bool>();

            foreach (var queueInfo in queues)
            {
                var instance = await RawEndpoint.Start(CreateEndpointConfig(queueInfo, barrier.Task, errorQueue)).ConfigureAwait(false);

                instances.Add(instance);

                foreach (var catalog in queueInfo.Value.Catalogs)
                {
                    endpointsByCatalog.Add(catalog, instance);
                }
            }

            barrier.SetResult(true);
        }
        public async Task <IEndpointInstance> StartOutbox()
        {
            var destinationEndpoint = await RawEndpoint.Start(_destinationEndpointConfiguration).ConfigureAwait(false);

            // Setup the message forwarder
            var forwarder = new MessageForwarder(_destinationEndpointName, destinationEndpoint);

            _onMessage = forwarder.OnMessage;

            var forwarderEndpoint = await RawEndpoint.Start(_forwarderEndpointConfiguration).ConfigureAwait(false);

            foreach (var configAction in _outboxEndpointConfigurationActions)
            {
                configAction.Invoke(_outboxEndpointConfiguration);
            }

            _unicastRoutingTable.AddOrReplaceRoutes("EndpointConfiguration", _configRouteTableEntries);

            var outboxEndpoint = await Endpoint.Start(_outboxEndpointConfiguration).ConfigureAwait(false);

            return(new WebOutboxEndpoint(outboxEndpoint, forwarderEndpoint, destinationEndpoint));
        }
Ejemplo n.º 24
0
    static async Task Start()
    {
        #region Configuration

        var senderConfig = RawEndpointConfiguration.Create(
            endpointName: "EndpointName",
            onMessage: OnMessage,
            poisonMessageQueue: "error");
        senderConfig.UseTransport <MsmqTransport>();

        var sender = await RawEndpoint.Start(senderConfig)
                     .ConfigureAwait(false);

        #endregion

        #region Sending

        var body    = Serialize();
        var headers = new Dictionary <string, string>
        {
            ["SomeHeader"] = "SomeValue"
        };
        var request = new OutgoingMessage(
            messageId: Guid.NewGuid().ToString(),
            headers: headers,
            body: body);

        var operation = new TransportOperation(
            request,
            new UnicastAddressTag("Receiver"));

        await sender.SendRaw(
            outgoingMessages : new TransportOperations(operation),
            transaction : new TransportTransaction(),
            context : new ContextBag())
        .ConfigureAwait(false);

        #endregion
    }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var nameShortener            = new RuleNameShortener();
            var endpointConfigurationRaw = RawEndpointConfiguration.Create(_attribute.Endpoint, OnMessage, _poisonMessageQueue);

            if (_nServiceBusOptions.EndpointConfiguration != null)
            {
                endpointConfigurationRaw = _nServiceBusOptions.EndpointConfiguration.Invoke(endpointConfigurationRaw);
            }
            else
            {
                var tokenProvider = TokenProvider.CreateManagedIdentityTokenProvider();

                endpointConfigurationRaw.UseTransport <AzureServiceBusTransport>()
                .RuleNameShortener(nameShortener.Shorten)
                .CustomTokenProvider(tokenProvider)
                .ConnectionString(_attribute.Connection)
                .Transactions(TransportTransactionMode.ReceiveOnly);
            }

            if (!string.IsNullOrEmpty(EnvironmentVariables.NServiceBusLicense))
            {
                endpointConfigurationRaw.UseLicense(EnvironmentVariables.NServiceBusLicense);
            }
            endpointConfigurationRaw.DefaultErrorHandlingPolicy(_poisonMessageQueue, ImmediateRetryCount);
            endpointConfigurationRaw.AutoCreateQueue();

            _endpoint = await RawEndpoint.Start(endpointConfigurationRaw).ConfigureAwait(false);

            if (_nServiceBusOptions.OnStarted != null)
            {
                _nServiceBusOptions.OnStarted.Invoke(_endpoint);
            }

            await _endpoint.SubscriptionManager.Subscribe(_parameter.ParameterType, new ContextBag());
        }
Ejemplo n.º 26
0
 public AuditsEngine(QueueInfrastructure infrastructure)
 {
     endpoint = new RawEndpoint(infrastructure, OnMessage, "audit");
 }
Ejemplo n.º 27
0
        public static async Task Main(string[] args)
        {
            var nhConfig = new Configuration();

            nhConfig.SetProperty(Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider");
            nhConfig.SetProperty(Environment.ConnectionDriver, "NHibernate.Driver.Sql2008ClientDriver");
            nhConfig.SetProperty(Environment.Dialect, "NHibernate.Dialect.MsSql2008Dialect");
            nhConfig.SetProperty(Environment.ConnectionString, System.Environment.GetEnvironmentVariable("SQLServerConnectionString"));

            var mapper = new ModelMapper();

            mapper.AddMapping <TimeoutEntityMap>();
            var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();

            nhConfig.AddMapping(mappings);

            var sessionFactory =
                nhConfig.BuildSessionFactory();

            var senderConfig = RawEndpointConfiguration.CreateSendOnly("DUMMY");
            var transport    = senderConfig.UseTransport <SqsTransport>();

            transport.UnrestrictedDurationDelayedDelivery();

            var sender = await RawEndpoint.Start(senderConfig)
                         .ConfigureAwait(false);

            var ignoreMachineName = true; // Must be true for Sqs
            var now = DateTime.UtcNow;

            using (var session = sessionFactory.OpenStatelessSession())
            {
                var timeoutEntities = await session.Query <TimeoutEntity>().Take(100).ToListAsync();

                var transportOperations = new List <TransportOperation>();

                foreach (var timeout in timeoutEntities)
                {
                    await Console.Out.WriteLineAsync($"Id:{timeout.Id} Time:{timeout.Time:s} Destination:{timeout.Destination} {timeout.Endpoint}").ConfigureAwait(false);

                    var body        = timeout.State;
                    var headers     = ConvertStringToDictionary(timeout.Headers);
                    var timestamp   = timeout.Time;
                    var destination = timeout.Destination;
                    var id          = headers["NServiceBus.MessageId"];

                    if (ignoreMachineName)
                    {
                        var at = destination.LastIndexOf("@", StringComparison.InvariantCulture);

                        if (at != -1)
                        {
                            destination = destination.Substring(0, at);
                        }
                    }

                    var age = now - timestamp;

                    var request = new OutgoingMessage(
                        messageId: timeout.Id.ToString(),
                        headers: headers,
                        body: body
                        );

                    var operation = new TransportOperation(
                        request,
                        new UnicastAddressTag(destination)
                        , DispatchConsistency.Default, new List <DeliveryConstraint>
                    {
                        new DoNotDeliverBefore(timestamp)
                    });

                    if (age.Ticks > 0)
                    {
                        await Console.Out.WriteLineAsync($"Warning: Message {id} was scheduled for {timestamp} which passed {age} ago.")
                        .ConfigureAwait(false);
                    }

                    transportOperations.Add(operation);
                }
                await sender.Dispatch(
                    outgoingMessages : new TransportOperations(transportOperations.ToArray()),
                    transaction : new TransportTransaction(),
                    context : new ContextBag()
                    )
                .ConfigureAwait(false);
            }
        }
Ejemplo n.º 28
0
        public async Task Run(CancellationToken token)
        {
            var config   = rawEndpointFactory.CreateSendOnlyRawEndpointConfiguration("ImportFailedErrors");
            var endpoint = await RawEndpoint.Start(config).ConfigureAwait(false);

            await errorIngestor.Initialize(endpoint).ConfigureAwait(false);

            try
            {
                var succeeded = 0;
                var failed    = 0;
                using (var session = store.OpenAsyncSession())
                {
                    var query = session.Query <FailedErrorImport, FailedErrorImportIndex>();
                    using (var ie = await session.Advanced.StreamAsync(query, token)
                                    .ConfigureAwait(false))
                    {
                        while (!token.IsCancellationRequested && await ie.MoveNextAsync().ConfigureAwait(false))
                        {
                            FailedTransportMessage dto = ((dynamic)ie.Current.Document).Message;
                            try
                            {
                                var messageContext = new MessageContext(dto.Id, dto.Headers, dto.Body, EmptyTransaction, EmptyTokenSource, EmptyContextBag);

                                await errorIngestor.Ingest(messageContext).ConfigureAwait(false);

                                await store.AsyncDatabaseCommands.DeleteAsync(ie.Current.Key, null, token)
                                .ConfigureAwait(false);

                                succeeded++;

                                if (Logger.IsDebugEnabled)
                                {
                                    Logger.Debug($"Successfully re-imported failed error message {dto.Id}.");
                                }
                            }
                            catch (OperationCanceledException)
                            {
                                //  no-op
                            }
                            catch (Exception e)
                            {
                                Logger.Error($"Error while attempting to re-import failed error message {dto.Id}.", e);
                                failed++;
                            }
                        }
                    }
                }

                Logger.Info($"Done re-importing failed errors. Successfully re-imported {succeeded} messages. Failed re-importing {failed} messages.");

                if (failed > 0)
                {
                    Logger.Warn($"{failed} messages could not be re-imported. This could indicate a problem with the data. Contact Particular support if you need help with recovering the messages.");
                }
            }
            finally
            {
                await endpoint.Stop().ConfigureAwait(false);
            }
        }
Ejemplo n.º 29
0
        public Task CreateQueue()
        {
            var config = createEndpointConfiguration();

            return(RawEndpoint.Create(config));
        }
        public async Task Run(string username)
        {
            var transportSettings      = MapSettings(settings);
            var transportCustomization = settings.LoadTransportCustomization();
            var factory = new RawEndpointFactory(settings, transportSettings, transportCustomization);

            var config = factory.CreateAuditIngestor(settings.AuditQueue, (context, dispatcher) => Task.CompletedTask);

            if (settings.SkipQueueCreation)
            {
                log.Info("Skipping queue creation");
            }
            else
            {
                var additionalQueues = new List <string>
                {
                    $"{settings.ServiceName}.Errors"
                };
                if (settings.ForwardAuditMessages && settings.AuditLogQueue != null)
                {
                    additionalQueues.Add(settings.AuditLogQueue);
                }
                config.AutoCreateQueues(additionalQueues.ToArray(), username);
            }

            //No need to start the raw endpoint to create queues
            await RawEndpoint.Create(config).ConfigureAwait(false);

            var configuration   = new EndpointConfiguration(settings.ServiceName);
            var assemblyScanner = configuration.AssemblyScanner();

            assemblyScanner.ExcludeAssemblies("ServiceControl.Plugin");
            if (excludeAssemblies != null)
            {
                assemblyScanner.ExcludeAssemblies(excludeAssemblies);
            }

            configuration.EnableInstallers(username);

            if (settings.SkipQueueCreation)
            {
                log.Info("Skipping queue creation");
                configuration.DoNotCreateQueues();
            }

            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterInstance(transportSettings).SingleInstance();

            var loggingSettings = new LoggingSettings(settings.ServiceName);

            containerBuilder.RegisterInstance(loggingSettings).SingleInstance();
            var documentStore = new EmbeddableDocumentStore();

            containerBuilder.RegisterInstance(documentStore).As <IDocumentStore>().ExternallyOwned();
            containerBuilder.RegisterInstance(settings).SingleInstance();
            containerBuilder.RegisterType <MigrateKnownEndpoints>().As <INeedToInstallSomething>();

            using (documentStore)
                using (var container = containerBuilder.Build())
                {
                    await NServiceBusFactory.Create(settings, transportCustomization, transportSettings, loggingSettings, container, ctx => { }, documentStore, configuration, false)
                    .ConfigureAwait(false);
                }
        }