protected override async Task <string> EnqueueImplAsync(T data, QueueEntryOptions options)
        {
            if (!await OnEnqueuingAsync(data, options).AnyContext())
            {
                return(null);
            }

            Interlocked.Increment(ref _enqueuedCount);
            var stream = new MemoryStream();

            _serializer.Serialize(data, stream);
            var brokeredMessage = new Message(stream.ToArray());

            brokeredMessage.CorrelationId = options.CorrelationId;
            if (options is AzureServiceBusQueueEntryOptions asbOptions)
            {
                brokeredMessage.SessionId = asbOptions.SessionId;
            }
            foreach (var property in options.Properties)
            {
                brokeredMessage.UserProperties[property.Key] = property.Value;
            }

            await _queueSender.SendAsync(brokeredMessage).AnyContext();

            var entry = new QueueEntry <T>(brokeredMessage.MessageId, brokeredMessage.CorrelationId, data, this, SystemClock.UtcNow, 0);

            foreach (var property in brokeredMessage.UserProperties)
            {
                entry.Properties.Add(property.Key, property.Value);
            }
            await OnEnqueuedAsync(entry).AnyContext();

            return(brokeredMessage.MessageId);
        }
Beispiel #2
0
        protected override async Task <string> EnqueueImplAsync(T data, QueueEntryOptions options)
        {
            string id = Guid.NewGuid().ToString("N");

            _logger.LogTrace("Queue {Name} enqueue item: {Id}", _options.Name, id);

            if (!await OnEnqueuingAsync(data, options).AnyContext())
            {
                return(null);
            }

            var entry = new QueueEntry <T>(id, options?.CorrelationId, data.DeepClone(), this, SystemClock.UtcNow, 0);

            entry.Properties.AddRange(options?.Properties);

            _queue.Enqueue(entry);
            _logger.LogTrace("Enqueue: Set Event");

            _autoResetEvent.Set();
            Interlocked.Increment(ref _enqueuedCount);

            await OnEnqueuedAsync(entry).AnyContext();

            _logger.LogTrace("Enqueue done");

            return(id);
        }
Beispiel #3
0
        protected override async Task <string> EnqueueImplAsync(T data, QueueEntryOptions options)
        {
            string id = Guid.NewGuid().ToString("N");

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Queue {Name} enqueue item: {EntryId}", _options.Name, id);
            }

            bool isTraceLogLevelEnabled = _logger.IsEnabled(LogLevel.Trace);

            if (!await OnEnqueuingAsync(data, options).AnyContext())
            {
                if (isTraceLogLevelEnabled)
                {
                    _logger.LogTrace("Aborting enqueue item: {EntryId}", id);
                }
                return(null);
            }

            var now      = SystemClock.UtcNow;
            var envelope = new RedisPayloadEnvelope <T> {
                Properties    = options.Properties,
                CorrelationId = options.CorrelationId,
                Value         = data
            };
            bool success = await Run.WithRetriesAsync(() => _cache.AddAsync(GetPayloadKey(id), envelope, _payloadTimeToLive), logger : _logger).AnyContext();

            if (!success)
            {
                throw new InvalidOperationException("Attempt to set payload failed.");
            }

            await Run.WithRetriesAsync(() => Task.WhenAll(
                                           _cache.SetAsync(GetEnqueuedTimeKey(id), now.Ticks, _payloadTimeToLive),
                                           Database.ListLeftPushAsync(_queueListName, id)
                                           ), logger : _logger).AnyContext();

            try {
                _autoResetEvent.Set();
                await Run.WithRetriesAsync(() => _subscriber.PublishAsync(GetTopicName(), id), logger : _logger).AnyContext();
            } catch (Exception ex) {
                if (isTraceLogLevelEnabled)
                {
                    _logger.LogTrace(ex, "Error publishing topic message");
                }
            }

            Interlocked.Increment(ref _enqueuedCount);
            var entry = new QueueEntry <T>(id, null, data, this, now, 0);

            await OnEnqueuedAsync(entry).AnyContext();

            if (isTraceLogLevelEnabled)
            {
                _logger.LogTrace("Enqueue done");
            }
            return(id);
        }
Beispiel #4
0
        public async Task <string> EnqueueAsync(T data, QueueEntryOptions options = null)
        {
            await EnsureQueueCreatedAsync().AnyContext();

            LastEnqueueActivity = SystemClock.UtcNow;
            options ??= new QueueEntryOptions();

            return(await EnqueueImplAsync(data, options).AnyContext());
        }
Beispiel #5
0
        protected virtual async Task <bool> OnEnqueuingAsync(T data, QueueEntryOptions options)
        {
            var enqueueing = Enqueuing;

            if (enqueueing == null)
            {
                return(false);
            }

            var args = new EnqueuingEventArgs <T> {
                Queue = this, Data = data, Options = options
            };
            await enqueueing.InvokeAsync(this, args).AnyContext();

            return(!args.Cancel);
        }
        protected override async Task <string> EnqueueImplAsync(T data, QueueEntryOptions options)
        {
            if (!await OnEnqueuingAsync(data, options).AnyContext())
            {
                return(null);
            }

            Interlocked.Increment(ref _enqueuedCount);
            var message = new CloudQueueMessage(_serializer.SerializeToBytes(data));
            await _queueReference.AddMessageAsync(message).AnyContext();

            var entry = new QueueEntry <T>(message.Id, null, data, this, SystemClock.UtcNow, 0);

            await OnEnqueuedAsync(entry).AnyContext();

            return(message.Id);
        }
Beispiel #7
0
        protected virtual async Task <bool> OnEnqueuingAsync(T data, QueueEntryOptions options)
        {
            if (String.IsNullOrEmpty(options.CorrelationId))
            {
                options.CorrelationId = Activity.Current?.Id;
            }

            var enqueueing = Enqueuing;

            if (enqueueing == null)
            {
                return(false);
            }

            var args = new EnqueuingEventArgs <T> {
                Queue = this, Data = data, Options = options
            };
            await enqueueing.InvokeAsync(this, args).AnyContext();

            return(!args.Cancel);
        }
Beispiel #8
0
        protected override async Task <string> EnqueueImplAsync(T data, QueueEntryOptions options)
        {
            if (!await OnEnqueuingAsync(data, options).AnyContext())
            {
                return(null);
            }

            var message = new SendMessageRequest {
                QueueUrl    = _queueUrl,
                MessageBody = _serializer.SerializeToString(data)
            };

            if (!String.IsNullOrEmpty(options?.CorrelationId))
            {
                message.MessageAttributes.Add("CorrelationId", new MessageAttributeValue {
                    DataType    = "String",
                    StringValue = options.CorrelationId
                });
            }

            if (options?.Properties != null)
            {
                foreach (var property in options.Properties)
                {
                    message.MessageAttributes.Add(property.Key, new MessageAttributeValue {
                        DataType    = "String",
                        StringValue = property.Value.ToString() // TODO: Support more than string data types
                    });
                }
            }

            var response = await _client.Value.SendMessageAsync(message).AnyContext();

            Interlocked.Increment(ref _enqueuedCount);
            var entry = new QueueEntry <T>(response.MessageId, options?.CorrelationId, data, this, SystemClock.UtcNow, 0);

            await OnEnqueuedAsync(entry).AnyContext();

            return(response.MessageId);
        }
Beispiel #9
0
 protected abstract Task <string> EnqueueImplAsync(T data, QueueEntryOptions options);