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);
        }
Example #2
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.");
            }
        }
Example #3
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);
        }
        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.");
            }
        }
Example #5
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);
    }
    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);
    }
Example #7
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);
        }
Example #9
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);
        }
Example #10
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);
    }
Example #12
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);
     }
 }
Example #13
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);
     }
 }
        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);
        }
Example #15
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);
    }
Example #16
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));
        }
Example #18
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());
        }
Example #20
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);
            }
        }
Example #21
0
        private static async Task Main(string[] args)
        {
            var maxConcurrency = 10;

            var channel = Channel.CreateBounded <MessageContext>(new BoundedChannelOptions(maxConcurrency)
            {
                SingleReader = true,
                SingleWriter = false,
                AllowSynchronousContinuations = false,
                FullMode = BoundedChannelFullMode.Wait
            });

            var senderConfig =
                RawEndpointConfiguration.Create("Sender", (ctx, disp) => OnMessage(ctx, disp, channel), "error");

            senderConfig.AutoCreateQueue();
            var transport = senderConfig.UseTransport <RabbitMQTransport>();

            transport.UseConventionalRoutingTopology();
            transport.ConnectionString("host=localhost");
            senderConfig.LimitMessageProcessingConcurrencyTo(maxConcurrency);

            async Task ReadChannel(Channel <MessageContext> channel)
            {
                var contexts = new List <MessageContext>(maxConcurrency);

                await Console.Error.WriteLineAsync("Start reading...");

                while (await channel.Reader.WaitToReadAsync().ConfigureAwait(false))
                {
                    try
                    {
                        await Console.Error.WriteLineAsync("Start TryRead...");

                        while (channel.Reader.TryRead(out var context))
                        {
                            contexts.Add(context);
                        }

                        await Console.Error.WriteLineAsync($"Got pushed {contexts.Count} contexts.");

                        foreach (var context in contexts)
                        {
                            await Console.Error.WriteAsync(".");

                            context.Extensions.Get <TaskCompletionSource <bool> >().TrySetResult(true);
                        }
                    }
                    finally
                    {
                        contexts.Clear();
                    }

                    await Console.Error.WriteLineAsync("Done TryRead...");
                }

                await Console.Error.WriteLineAsync("Done reading...");
            }

            var readerTask = Task.Run(() => ReadChannel(channel));

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

            var cts = new CancellationTokenSource();

            async Task ProduceMessages(IReceivingRawEndpoint sender, CancellationToken cancellationToken)
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    var transportOperations = new TransportOperation[15];
                    for (var i = 0; i < 15; i++)
                    {
                        var headers = new Dictionary <string, string> {
                            ["SomeHeader"] = "SomeValue"
                        };
                        var request = new OutgoingMessage(Guid.NewGuid().ToString(), headers, Array.Empty <byte>());

                        transportOperations[i] = new TransportOperation(request, new UnicastAddressTag("Sender"));
                    }

                    await sender.Dispatch(
                        new TransportOperations(transportOperations),     //Can have multiple sends in one batch
                        new TransportTransaction(),
                        new ContextBag())
                    .ConfigureAwait(false);

                    try
                    {
                        await Task.Delay(100, cts.Token);
                    }
                    catch (OperationCanceledException)
                    {
                        // ignored
                    }
                }

                await Console.Error.WriteLineAsync("Stopped sending");
            }

            var senderTask = Task.Run(() => ProduceMessages(sender, cts.Token), CancellationToken.None);

            await Console.Error.WriteLineAsync("Press any key to stop");

            await Console.In.ReadLineAsync();

            cts.CancelAfter(TimeSpan.FromSeconds(15));
            await sender.StopReceiving();

            channel.Writer.Complete();
            await readerTask;

            await senderTask;
        }
Example #22
0
 public Task Start()
 {
     return(endpoint.Start());
 }
Example #23
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);
            }
        }