コード例 #1
0
            // Ownership information is stored as metadata on blobs in Azure Storage.  To list ownership
            // information we list all the blobs in Blob Storage (with their corresponding metadata) and
            // then extract the ownership information from the metadata.

            protected override async Task <IEnumerable <EventProcessorPartitionOwnership> > ListOwnershipAsync(
                CancellationToken cancellationToken = default)
            {
                List <EventProcessorPartitionOwnership> partitonOwnerships =
                    new List <EventProcessorPartitionOwnership>();

                string ownershipBlobsPefix = string.Format(
                    OwnershipPrefixFormat,
                    FullyQualifiedNamespace.ToLowerInvariant(),
                    EventHubName.ToLowerInvariant(),
                    ConsumerGroup.ToLowerInvariant());

                AsyncPageable <BlobItem> blobItems = StorageContainer.GetBlobsAsync(
                    traits: BlobTraits.Metadata,
                    prefix: ownershipBlobsPefix,
                    cancellationToken: cancellationToken);

                await foreach (BlobItem blob in blobItems.ConfigureAwait(false))
                {
                    partitonOwnerships.Add(new EventProcessorPartitionOwnership
                    {
                        ConsumerGroup           = ConsumerGroup,
                        EventHubName            = EventHubName,
                        FullyQualifiedNamespace = FullyQualifiedNamespace,
                        LastModifiedTime        = blob.Properties.LastModified.GetValueOrDefault(),
                        OwnerIdentifier         = blob.Metadata[OwnerIdentifierMetadataKey],
                        PartitionId             = blob.Name.Substring(ownershipBlobsPefix.Length),
                        Version = blob.Properties.ETag.ToString()
                    });
                }

                return(partitonOwnerships);
            }
コード例 #2
0
        public static async Task <TSource?> FirstOrDefaultAsync <TSource>(
            this AsyncPageable <TSource> source,
            Func <TSource, bool> predicate,
            CancellationToken token = default)
            where TSource : notnull
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            token.ThrowIfCancellationRequested();

            await foreach (var item in source.ConfigureAwait(false))
            {
                token.ThrowIfCancellationRequested();

                if (predicate(item))
                {
                    return(item);
                }
            }

            return(default);
コード例 #3
0
        private async Task LoadAsync()
        {
            using var secretLoader = new ParallelSecretLoader(_client);
            var newLoadedSecrets = new Dictionary <string, LoadedSecret>();
            var oldLoadedSecrets = Interlocked.Exchange(ref _loadedSecrets, null);

            if (_fullLoad)
            {
                AsyncPageable <SecretProperties> secretPages = _client.GetPropertiesOfSecretsAsync();
                await foreach (var secret in secretPages.ConfigureAwait(false))
                {
                    if (secret.Enabled != true)
                    {
                        continue;
                    }
                    VerifySecretToload(secretLoader, newLoadedSecrets, oldLoadedSecrets, secret);
                }
            }
            else
            {
                foreach (var key in _uploadKeyList)
                {
                    AsyncPageable <SecretProperties> secretProperties = _client.GetPropertiesOfSecretVersionsAsync(key);
                    var secretList = await secretProperties.ToListAsync();

                    var secret = secretList.OrderByDescending(s => s.UpdatedOn).FirstOrDefault(w => w.Enabled.GetValueOrDefault());
                    if (secret != null)
                    {
                        VerifySecretToload(secretLoader, newLoadedSecrets, oldLoadedSecrets, secret);
                    }
                }

                foreach (var keyValue in _uploadAndMapKeys)
                {
                    AsyncPageable <SecretProperties> secretProperties = _client.GetPropertiesOfSecretVersionsAsync(keyValue.Key);
                    var secretList = await secretProperties.ToListAsync();

                    var secret = secretList.OrderByDescending(s => s.UpdatedOn).FirstOrDefault(w => w.Enabled.GetValueOrDefault());
                    if (secret != null)
                    {
                        VerifySecretToload(secretLoader, newLoadedSecrets, oldLoadedSecrets, secret);
                    }
                }
            }

            var loadedSecrets = await secretLoader.WaitForAllSecrets().ConfigureAwait(false);

            foreach (var secretBundle in loadedSecrets)
            {
                string configName = secretBundle.Value.Name;

                if (!_fullLoad)
                {
                    if (_uploadAndMapKeys.Keys.Contains(configName))
                    {
                        configName = _uploadAndMapKeys[configName];
                    }
                }

                if (!string.IsNullOrWhiteSpace(_configurationSectionPrefix))
                {
                    configName = _configurationSectionPrefix + ConfigurationPath.KeyDelimiter + configName;
                }

                newLoadedSecrets.Add(secretBundle.Value.Name, new LoadedSecret(_keyVaultSecretNameEncoder(configName),
                                                                               secretBundle.Value.Value, secretBundle.Value.Properties.UpdatedOn));
            }

            _loadedSecrets = newLoadedSecrets;

            // Reload is needed if we are loading secrets that were not loaded before or
            // secret that was loaded previously is not available anymore
            if (loadedSecrets.Any() || oldLoadedSecrets?.Any() == true)
            {
                SetData(_loadedSecrets, fireToken: oldLoadedSecrets != null);
            }

            // schedule a polling task only if none exists and a valid delay is specified
            if (_pollingTask == null && _reloadInterval != null)
            {
                _pollingTask = PollForSecretChangesAsync();
            }
        }