Esempio n. 1
0
        public static bool Exists(this IStorageQueue queue)
        {
            if (queue == null)
            {
                throw new ArgumentNullException("queue");
            }

            return(queue.ExistsAsync(CancellationToken.None).GetAwaiter().GetResult());
        }
        // Test that the credentials are valid and classify the account.Type as one of StorageAccountTypes
        private static async Task ValidateCredentialsAsyncCore(IStorageAccount account, CancellationToken cancellationToken)
        {
            // Verify the credentials are correct.
            // Have to actually ping a storage operation.
            IStorageBlobClient client = account.CreateBlobClient();

            try
            {
                // This can hang for a long time if the account name is wrong.
                // If will fail fast if the password is incorrect.
                await client.GetServicePropertiesAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e)
            {
                var storageException  = e as StorageException;
                var isDevStoreAccount = GetIsDevStoreAccountFromCloudStorageAccount(account.SdkObject);

                if (storageException?.RequestInformation?.HttpStatusCode == 400 &&
                    storageException?.RequestInformation?.ExtendedErrorInformation?.ErrorCode == "InvalidQueryParameterValue")
                {
                    // Premium storage accounts do not support the GetServicePropertiesAsync call, and respond with a 400 'InvalidQueryParameterValue'.
                    // If we see this error response classify the account as a premium account
                    account.Type = StorageAccountType.Premium;
                    return;
                }
                else if (isDevStoreAccount)
                {
                    // If using the storage emulator, it might not be running
                    throw new InvalidOperationException(Constants.CheckAzureStorageEmulatorMessage, e);
                }
                else
                {
                    // If not a recognized error, the credentials are invalid
                    string message = String.Format(CultureInfo.CurrentCulture,
                                                   "Invalid storage account '{0}'. Please make sure your credentials are correct.",
                                                   account.Credentials.AccountName);
                    throw new InvalidOperationException(message, e);
                }
            }

            IStorageQueueClient queueClient = account.CreateQueueClient();
            IStorageQueue       queue       = queueClient.GetQueueReference("name");

            try
            {
                await queue.ExistsAsync(cancellationToken);
            }
            catch (StorageException exception) when(IsBlobOnlyStorageException(exception))
            {
                account.Type = StorageAccountType.BlobOnly;
            }
        }
        private async Task ValidateCredentialsAsyncCore(IStorageAccount account,
                                                        bool isPrimaryAccount, CancellationToken cancellationToken)
        {
            // Verify the credentials are correct.
            // Have to actually ping a storage operation.
            IStorageBlobClient client = account.CreateBlobClient();

            try
            {
                // This can hang for a long time if the account name is wrong.
                // If will fail fast if the password is incorrect.
                await client.GetServicePropertiesAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch
            {
                string message = String.Format(CultureInfo.CurrentCulture,
                                               "Invalid storage account '{0}'. Please make sure your credentials are correct.",
                                               account.Credentials.AccountName);
                throw new InvalidOperationException(message);
            }

            if (isPrimaryAccount)
            {
                // Primary storage accounts require Queues
                IStorageQueueClient queueClient = account.CreateQueueClient();
                IStorageQueue       queue       = queueClient.GetQueueReference("name");
                try
                {
                    await queue.ExistsAsync(cancellationToken);

                    _primaryCredentials = account.Credentials;
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (StorageException exception)
                {
                    WebException webException = exception.GetBaseException() as WebException;
                    if (webException != null && webException.Status == WebExceptionStatus.NameResolutionFailure)
                    {
                        string message = String.Format(CultureInfo.CurrentCulture,
                                                       "Invalid storage account '{0}'. Primary storage accounts must be general "
                                                       + "purpose accounts and not restricted blob storage accounts.", account.Credentials.AccountName);
                        throw new InvalidOperationException(message);
                    }
                    throw;
                }
            }
        }
        public async Task <TaskSeriesCommandResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            lock (_stopWaitingTaskSourceLock)
            {
                if (_stopWaitingTaskSource != null)
                {
                    _stopWaitingTaskSource.TrySetResult(null);
                }

                _stopWaitingTaskSource = new TaskCompletionSource <object>();
            }

            if (!await _queue.ExistsAsync(cancellationToken))
            {
                // Back off when no message is available.
                return(CreateBackoffResult());
            }

            // What if job takes longer. Call CloudQueue.UpdateMessage
            TimeSpan visibilityTimeout = TimeSpan.FromMinutes(10); // long enough to process the job
            IEnumerable <IStorageQueueMessage> batch;

            try
            {
                batch = await _queue.GetMessagesAsync(_queueProcessor.BatchSize,
                                                      visibilityTimeout,
                                                      options : null,
                                                      operationContext : null,
                                                      cancellationToken : cancellationToken);
            }
            catch (StorageException exception)
            {
                if (exception.IsNotFoundQueueNotFound() ||
                    exception.IsConflictQueueBeingDeletedOrDisabled() ||
                    exception.IsServerSideError())
                {
                    // Back off when no message is available.
                    return(CreateBackoffResult());
                }
                else
                {
                    throw;
                }
            }

            if (batch == null)
            {
                return(CreateBackoffResult());
            }

            bool foundMessage = false;

            foreach (IStorageQueueMessage message in batch)
            {
                if (message == null)
                {
                    continue;
                }

                foundMessage = true;

                // Note: Capturing the cancellationToken passed here on a task that continues to run is a slight abuse
                // of the cancellation token contract. However, the timer implementation would not dispose of the
                // cancellation token source until it has stopped and perhaps also disposed, and we wait for all
                // outstanding tasks to complete before stopping the timer.
                Task task = ProcessMessageAsync(message, visibilityTimeout, cancellationToken);

                // Having both WaitForNewBatchThreshold and this method mutate _processing is safe because the timer
                // contract is serial: it only calls ExecuteAsync once the wait expires (and the wait won't expire until
                // WaitForNewBatchThreshold has finished mutating _processing).
                _processing.Add(task);
            }

            // Back off when no message was found.
            if (!foundMessage)
            {
                return(CreateBackoffResult());
            }

            _foundMessageSinceLastDelay = true;
            return(CreateSucceededResult());
        }
Esempio n. 5
0
        // Test that the credentials are valid and classify the account.Type as one of StorageAccountTypes
        private static async Task ValidateCredentialsAsyncCore(IStorageAccount account, CancellationToken cancellationToken)
        {
            // Verify the credentials are correct.
            // Have to actually ping a storage operation.
            IStorageBlobClient client = account.CreateBlobClient();

            try
            {
                // This can hang for a long time if the account name is wrong.
                // If will fail fast if the password is incorrect.
                await client.GetServicePropertiesAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e)
            {
                var storageException = e as StorageException;
                if (storageException?.RequestInformation?.HttpStatusCode == 400 &&
                    storageException?.RequestInformation?.ExtendedErrorInformation?.ErrorCode == "InvalidQueryParameterValue")
                {
                    // Premium storage accounts do not support the GetServicePropertiesAsync call, and respond with a 400 'InvalidQueryParameterValue'.
                    // If we see this error response classify the account as a premium account
                    account.Type = StorageAccountType.Premium;
                    return;
                }
                else
                {
                    // If not a recognized error, the credentials are invalid
                    string message = String.Format(CultureInfo.CurrentCulture,
                                                   "Invalid storage account '{0}'. Please make sure your credentials are correct.",
                                                   account.Credentials.AccountName);
                    throw new InvalidOperationException(message);
                }
            }

            IStorageQueueClient queueClient = account.CreateQueueClient();
            IStorageQueue       queue       = queueClient.GetQueueReference("name");

            try
            {
                await queue.ExistsAsync(cancellationToken);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (StorageException exception)
            {
                WebException webException = exception.GetBaseException() as WebException;
                if (webException?.Status == WebExceptionStatus.NameResolutionFailure)
                {
                    // Blob-only storage accounts do not support services other than Blob.
                    // If we see a name resolution failure on the queue endpoint classify as a blob-only account
                    account.Type = StorageAccountType.BlobOnly;
                    return;
                }
                throw;
            }
        }