Ejemplo n.º 1
0
        public async Task <IStreamMetadata> GetAsync(
            StreamId streamId,
            CancellationToken cancellationToken)
        {
            try
            {
                var metadata = await containerProvider
                               .GetStreamContainer()
                               .ReadItemAsync <StreamMetadata>(
                    StreamMetadata.StreamMetadataId,
                    new PartitionKey(streamId.Value),
                    cancellationToken: cancellationToken)
                               .ConfigureAwait(false);

                metadata.Resource.ETag = metadata.ETag;

                return(metadata.Resource);
            }
            catch (CosmosException ex)
                when(ex.StatusCode == HttpStatusCode.NotFound)
                {
                    return(new StreamMetadata(
                               StreamMetadata.StreamMetadataId,
                               streamId.Value,
                               streamId,
                               StreamVersion.StartOfStream,
                               StreamState.New,
                               timeProvider.GetDateTime()));
                }
        }
Ejemplo n.º 2
0
        public IStreamSubscription Create(
            ConsumerGroup consumerGroup,
            SubscriptionStartOptions startOptions,
            ProcessEventsHandler eventsHandler)
        {
            var builder = containerProvider
                          .GetStreamContainer()
                          .GetChangeFeedProcessorBuilder <EventDocument>(
                GetProcessorName(consumerGroup),
                (c, ct) => eventsHandler(c.Where(ExcludeMetaDataChanges).ToArray(), ct))
                          .WithLeaseContainer(containerProvider.GetSubscriptionContainer())
                          .WithMaxItems(100)
                          .WithPollInterval(TimeSpan.FromMilliseconds(1000));

            if (startOptions == SubscriptionStartOptions.FromBegining)
            {
                // Instruct processor to start from beginning.
                // see https://docs.microsoft.com/en-us/azure/cosmos-db/change-feed-processor#reading-from-the-beginning
                builder.WithStartTime(DateTime.MinValue.ToUniversalTime());
            }

            if (!string.IsNullOrEmpty(consumerGroup.Instance))
            {
                builder.WithInstanceName(consumerGroup.Instance);
            }

            return(new CosmosSubscriptionProcessor(
                       telemetry,
                       builder.Build(),
                       consumerGroup));
        }
        public async Task <IStreamMetadata> WriteAsync(
            StreamBatch batch,
            CancellationToken cancellationToken)
        {
            var pk = new PartitionKey(batch.Metadata.StreamId.Value);
            var tx = containerProvider
                     .GetStreamContainer()
                     .CreateTransactionalBatch(pk);

            tx.UpsertItem(
                batch.Metadata,
                new TransactionalBatchItemRequestOptions {
                IfMatchEtag = batch.Metadata.ETag
            });

            foreach (var document in batch.Documents)
            {
                tx.CreateItem(
                    document,
                    new TransactionalBatchItemRequestOptions {
                    EnableContentResponseOnWrite = false
                });
            }

            if (IsEmptyStream(batch.Metadata))
            {
                await containerProvider
                .GetIndexContainer()
                .UpsertItemAsync(
                    new StreamIndex
                {
                    StreamId  = batch.Metadata.StreamId.Value,
                    Timestamp = dateTimeProvider.GetDateTime(),
                    IsActive  = true,
                },
                    new PartitionKey(nameof(StreamIndex)),
                    new ItemRequestOptions {
                    EnableContentResponseOnWrite = false
                },
                    cancellationToken)
                .ConfigureAwait(false);
            }

            using var batchResponse = await tx
                                      .ExecuteAsync(cancellationToken)
                                      .ConfigureAwait(false);

            EnsureSuccess(batchResponse, batch.Metadata);

            return(GetMetadataFromResponse(batchResponse));
        }