public LightweightWorkerQueue(Endpoint endpoint, ITransportLogger logger,
                                      IHandlerPipeline pipeline, AdvancedSettings settings)
        {
            _logger   = logger;
            _settings = settings;
            Pipeline  = pipeline;

            _scheduler = new InMemoryScheduledJobProcessor(this);

            endpoint.ExecutionOptions.CancellationToken = settings.Cancellation;

            _receiver = new ActionBlock <Envelope>(async envelope =>
            {
                try
                {
                    if (envelope.ContentType.IsEmpty())
                    {
                        envelope.ContentType = "application/json";
                    }

                    await Pipeline.Invoke(envelope);
                }
                catch (Exception e)
                {
                    // This *should* never happen, but of course it will
                    logger.LogException(e);
                }
            }, endpoint.ExecutionOptions);
        }
Beispiel #2
0
        public DurableWorkerQueue(Endpoint endpoint, IHandlerPipeline pipeline,
                                  AdvancedSettings settings, IEnvelopePersistence persistence, ITransportLogger logger)
        {
            _settings    = settings;
            _persistence = persistence;
            _logger      = logger;

            endpoint.ExecutionOptions.CancellationToken = settings.Cancellation;

            _receiver = new ActionBlock <Envelope>(async envelope =>
            {
                try
                {
                    envelope.ContentType = envelope.ContentType ?? "application/json";

                    await pipeline.Invoke(envelope, this);
                }
                catch (Exception e)
                {
                    // This *should* never happen, but of course it will
                    logger.LogException(e);
                }
            }, endpoint.ExecutionOptions);

            _policy = Policy
                      .Handle <Exception>()
                      .WaitAndRetryForeverAsync(i => (i * 100).Milliseconds()
                                                , (e, timeSpan) => {
                _logger.LogException(e);
            });
        }
Beispiel #3
0
        private Task receive(Envelope envelope)
        {
            envelope.Callback    = _provider.BuildCallback(envelope, this);
            envelope.ContentType = envelope.ContentType ?? "application/json";

            return(_pipeline.Invoke(envelope));
        }
Beispiel #4
0
        public Task Receive(byte[] data, IDictionary <string, string> headers, IMessageCallback callback)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (headers == null)
            {
                throw new ArgumentNullException(nameof(headers));
            }
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            var envelope = new Envelope(data, headers, callback)
            {
                ReceivedAt = _address
            };

            // Do keep this here then.
            envelope.ContentType = envelope.ContentType ?? _node.DefaultContentType ?? _graph.DefaultContentType;

            return(_pipeline.Invoke(envelope, _node));
        }
Beispiel #5
0
        private async Task ConsumeAsync(IHandlerPipeline pipeline)
        {
            await foreach (Message message in _consumer.Messages(_cancellation))
            {
                Envelope envelope;

                try
                {
                    envelope = _protocol.ReadEnvelope(new DotPulsarMessage(message.Data, message.Properties));
                }
                catch (Exception ex)
                {
                    _logger.LogException(ex, message: $"Error trying to map an incoming Pulsar {_endpoint.Topic} Topic message to an Envelope. See the Dead Letter Queue");
                    continue;
                }

                try
                {
                    IChannelCallback channelCallback = new DotPulsarChannelCallback(message.MessageId, _consumer);
                    await pipeline.Invoke(envelope, channelCallback);
                }
                catch (Exception e)
                {
                    // DotPulsar currently doesn't support Nack so will likely just keep retrying message for now.
                    _logger.LogException(e, envelope.Id, "Error trying to receive a message from " + Address);
                }
            }
        }
Beispiel #6
0
        protected override Task send(Envelope envelope)
        {
            var callback = new StubMessageCallback(this);

            Callbacks.Add(callback);

            envelope.Callback = callback;

            return(_pipeline.Invoke(envelope));
        }
Beispiel #7
0
        public Task Send(Envelope envelope, Uri destination)
        {
            StubChannel tempQualifier = Channels[destination];
            var         callback      = new StubMessageCallback(tempQualifier);

            tempQualifier.Callbacks.Add(callback);
            envelope.Callback = callback;

            return(_pipeline.Invoke(envelope));
        }
Beispiel #8
0
        public Task Receive(byte[] data, IDictionary <string, string> headers, IMessageCallback callback)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (headers == null)
            {
                throw new ArgumentNullException(nameof(headers));
            }
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            var envelope = new Envelope(data, callback)
            {
                ReceivedAt = _address
            };

            envelope.ContentType = envelope.ContentType ?? "application/json";

            return(_pipeline.Invoke(envelope));
        }
Beispiel #9
0
        private Task handleJson(string socketId, string json)
        {
            if (_pipeline == null)
            {
                return(Task.CompletedTask);
            }

            var message  = JsonSerialization.DeserializeMessage(json);
            var envelope = new Envelope(message)
            {
                Callback = new WebSocketCallback(_retries)
            };

            return(_pipeline.Invoke(envelope));
        }
        private async Task ConsumeAsync(IHandlerPipeline pipeline)
        {
            while (!_cancellation.IsCancellationRequested)
            {
                ConsumeResult <byte[], byte[]> message;
                try
                {
                    message = await Task.Run(() => _consumer.Consume(), _cancellation);
                }
                catch (Confluent.Kafka.ConsumeException cex)
                {
                    if (cex.Error.Code == ErrorCode.PolicyViolation)
                    {
                        throw;
                    }

                    continue;
                }
                catch (Exception ex)
                {
                    _logger.LogException(ex, message: $"Error consuming message from Kafka topic {_endpoint.TopicName}");
                    continue;
                }

                Envelope envelope;

                try
                {
                    envelope = _protocol.ReadEnvelope(message.Message);
                }
                catch (Exception ex)
                {
                    _logger.LogException(ex, message: $"Error trying to map an incoming Kafka {_endpoint.TopicName} Topic message to an Envelope. See the Dead Letter Queue");
                    continue;
                }

                try
                {
                    await pipeline.Invoke(envelope, new KafkaChannelCallback(message, _consumer));
                }
                catch (Exception e)
                {
                    // TODO -- Got to either discard this or defer it back to the queue
                    _logger.LogException(e, envelope.Id, "Error trying to receive a message from " + Address);
                }
            }
        }
Beispiel #11
0
        public Task EnqueueOutgoing(Envelope envelope)
        {
            envelope.ReceivedAt = Destination;
            envelope.ReplyUri   = envelope.ReplyUri ?? DefaultReplyUri;

            var callback = new StubMessageCallback(this);

            Callbacks.Add(callback);

            _stubTransport.Callbacks.Add(callback);

            Sent.Add(envelope);


            envelope.Callback = callback;

            envelope.ReceivedAt = Destination;

            return(_pipeline.Invoke(envelope));
        }
        protected override void executeEnvelope(ulong deliveryTag, Envelope envelope)
        {
            var callback = new RabbitMqChannelCallback(_channel, deliveryTag, _sender);

            _pipeline.Invoke(envelope, callback).ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    _logger.LogException(t.Exception, envelope.Id, "Failure to receive an incoming message");
                    try
                    {
                        _channel.BasicNack(deliveryTag, false, true);
                    }
                    catch (Exception e)
                    {
                        _logger.LogException(e, envelope.CorrelationId, "Error when trying to Nack a Rabbit MQ message that failed in the HandlerPipeline");
                    }
                }
            });
        }
Beispiel #13
0
        public void AddQueue(string queueName, int parallelization)
        {
            var options = new ExecutionDataflowBlockOptions
            {
                MaxDegreeOfParallelism = parallelization,
                CancellationToken      = _cancellationToken
            };

            if (!_receivers.ContainsKey(queueName))
            {
                var receiver = new ActionBlock <Envelope>(envelope =>
                {
                    envelope.ContentType = envelope.ContentType ?? "application/json";

                    return(_pipeline.Invoke(envelope));
                }, options);

                _receivers.Add(queueName, receiver);
            }
        }
Beispiel #14
0
        public DurableWorkerQueue(Endpoint endpoint, IHandlerPipeline pipeline,
                                  AdvancedSettings settings, IEnvelopePersistence persistence, ITransportLogger logger)
        {
            _settings    = settings;
            _persistence = persistence;
            _logger      = logger;

            endpoint.ExecutionOptions.CancellationToken = settings.Cancellation;

            _receiver = new ActionBlock <Envelope>(async envelope =>
            {
                try
                {
                    envelope.ContentType = envelope.ContentType ?? "application/json";

                    await pipeline.Invoke(envelope);
                }
                catch (Exception e)
                {
                    // This *should* never happen, but of course it will
                    logger.LogException(e);
                }
            }, endpoint.ExecutionOptions);
        }
Beispiel #15
0
 public void Retry(Envelope envelope)
 {
     _pipeline.Invoke(envelope, this);
 }
Beispiel #16
0
 public Task Enqueue(Envelope envelope)
 {
     Sent.Add(envelope);
     return(_pipeline?.Invoke(envelope) ?? Task.CompletedTask);
 }
Beispiel #17
0
        public Task Retry()
        {
            _outstanding.Clear();

            return(_pipeline.Invoke(Envelope));
        }
Beispiel #18
0
 public Task Send(Envelope envelope)
 {
     Sent.Add(envelope);
     return(_pipeline?.Invoke(envelope, new StubChannelCallback(this, envelope)) ?? Task.CompletedTask);
 }