public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Instance, Resources.ExportingLogFromAnalysisServicesServer))
            {
                var context = AsAzureClientSession.Instance.Profile.Context;
                AsAzureClientSession.Instance.Login(context, null);
                string accessToken = this.TokenCacheItemProvider.GetTokenFromTokenCache(
                    AsAzureClientSession.TokenCache, context.Account.UniqueId);

                Uri logfileBaseUri =
                    new Uri(string.Format("{0}{1}{2}", Uri.UriSchemeHttps, Uri.SchemeDelimiter, context.Environment.Name));

                UriBuilder resolvedUriBuilder = new UriBuilder(logfileBaseUri);
                resolvedUriBuilder.Host = ClusterResolve(logfileBaseUri, accessToken, serverName);

                var logfileEndpoint = string.Format(
                    (string)context.Environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.LogfileEndpointFormat],
                    serverName);

                this.AsAzureHttpClient.resetHttpClient();
                using (HttpResponseMessage message = AsAzureHttpClient.CallGetAsync(
                           resolvedUriBuilder.Uri,
                           logfileEndpoint,
                           accessToken).ConfigureAwait(false).GetAwaiter().GetResult())
                {
                    message.EnsureSuccessStatusCode();
                    string actionWarning = string.Format(CultureInfo.CurrentCulture, Resources.ExportingLogOverwriteWarning, this.OutputPath);
                    if (AzureSession.Instance.DataStore.FileExists(this.OutputPath) && !this.Force.IsPresent && !ShouldContinue(actionWarning, Resources.Confirm))
                    {
                        return;
                    }
                    AzureSession.Instance.DataStore.WriteFile(this.OutputPath, message.Content.ReadAsStringAsync().Result);
                }
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="databaseName">Database name</param>
        /// <param name="accessToken">Access token</param>
        /// <param name="pollingUrl">URL for polling</param>
        /// <param name="pollingInterval">Polling interval set by the post response</param>
        /// <param name="maxNumberOfAttempts">Max number of attempts for each poll before the attempt is declared a failure</param>
        /// <returns></returns>
        private async Task <ScaleOutServerDatabaseSyncResult> PollSyncStatusWithRetryAsync(string databaseName, string accessToken, Uri pollingUrl, TimeSpan pollingInterval, int maxNumberOfAttempts = 3)
        {
            return(await Task.Run(async() =>
            {
                ScaleOutServerDatabaseSyncResult response = null;
                var syncCompleted = false;
                var retryCount = 0;

                while (!syncCompleted && retryCount < maxNumberOfAttempts)
                {
                    // Wait for specified polling interval other than retries.
                    if (retryCount == 0)
                    {
                        await Task.Delay(pollingInterval);
                    }
                    else
                    {
                        await Task.Delay(DefaultRetryIntervalForPolling);
                    }

                    this.AsAzureHttpClient.resetHttpClient();
                    using (HttpResponseMessage message = await AsAzureHttpClient.CallGetAsync(
                               pollingUrl,
                               string.Empty,
                               accessToken,
                               correlationId))
                    {
                        bool shouldRetry = false;
                        if (message.IsSuccessStatusCode && message.Content != null)
                        {
                            var responseString = await message.Content.ReadAsStringAsync();
                            response = JsonConvert.DeserializeObject <ScaleOutServerDatabaseSyncResult>(responseString);

                            if (response != null)
                            {
                                var state = response.SyncState;
                                if (state == DatabaseSyncState.Completed || state == DatabaseSyncState.Failed)
                                {
                                    syncCompleted = true;
                                }
                                else
                                {
                                    pollingUrl = message.Headers.Location ?? pollingUrl;
                                    pollingInterval = message.Headers.RetryAfter.Delta ?? pollingInterval;
                                }
                            }
                            else
                            {
                                shouldRetry = true;
                            }
                        }
                        else
                        {
                            shouldRetry = true;
                        }

                        if (shouldRetry)
                        {
                            retryCount++;
                            response = new ScaleOutServerDatabaseSyncResult()
                            {
                                Database = databaseName,
                                SyncState = DatabaseSyncState.Invalid
                            };

                            response.Details = string.Format(
                                "Http Error code: {0}. Message: {1}",
                                message.StatusCode.ToString(),
                                message.Content != null ? await message.Content.ReadAsStringAsync() : string.Empty);

                            if (message.StatusCode >= (HttpStatusCode)400 && message.StatusCode <= (HttpStatusCode)499)
                            {
                                break;
                            }
                        }
                        else
                        {
                            retryCount = 0;
                        }
                    }
                }

                return response;
            }));
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="databaseName">Database name</param>
        /// <param name="accessToken">Access token</param>
        /// <param name="pollingUrl">URL for polling</param>
        /// <param name="pollingInterval">Polling interval set by the post response</param>
        /// <param name="maxNumberOfAttempts">Max number of attempts for each poll before the attempt is declared a failure</param>
        /// <returns></returns>
        private async Task <ScaleOutServerDatabaseSyncResult> PollSyncStatusWithRetryAsync(string databaseName, string accessToken, Uri pollingUrl, TimeSpan pollingInterval, int maxNumberOfAttempts = 3)
        {
            return(await Task.Run(async() =>
            {
                ScaleOutServerDatabaseSyncResult response = null;
                var syncCompleted = false;
                do
                {
                    var retryCount = 0;
                    while (retryCount < maxNumberOfAttempts)
                    {
                        // Wait for specified polling interval other than retries.
                        if (retryCount == 0)
                        {
                            // WriteInformation(new InformationRecord(string.Format("Synchronize database {0}. Attempt #{1}. Waiting for {2} seconds to get sync results...", databaseName, retryCount, pollingInterval.TotalSeconds), string.Empty));
                            await Task.Delay(pollingInterval);
                        }
                        else
                        {
                            await Task.Delay(DefaultRetryIntervalForPolling);
                        }

                        this.AsAzureHttpClient.resetHttpClient();
                        using (HttpResponseMessage message = await AsAzureHttpClient.CallGetAsync(
                                   pollingUrl,
                                   string.Empty,
                                   accessToken,
                                   correlationId))
                        {
                            syncCompleted = !message.StatusCode.Equals(HttpStatusCode.SeeOther);
                            if (syncCompleted)
                            {
                                if (message.IsSuccessStatusCode)
                                {
                                    var responseString = await message.Content.ReadAsStringAsync();
                                    response = JsonConvert.DeserializeObject <ScaleOutServerDatabaseSyncResult>(responseString);
                                    break;
                                }
                                else
                                {
                                    retryCount++;
                                    if (response == null)
                                    {
                                        response = new ScaleOutServerDatabaseSyncResult()
                                        {
                                            Database = databaseName,
                                            SyncState = DatabaseSyncState.Invalid
                                        };

                                        response.Details = string.Format(
                                            "Http Error code: {0}. {1}",
                                            message.StatusCode.ToString(),
                                            message.Content != null ? await message.Content.ReadAsStringAsync() : string.Empty);
                                    }

                                    if (message.StatusCode >= (HttpStatusCode)400 && message.StatusCode <= (HttpStatusCode)499)
                                    {
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                pollingUrl = message.Headers.Location;
                                pollingInterval = message.Headers.RetryAfter.Delta ?? pollingInterval;
                            }
                        }
                    }
                }while (!syncCompleted);

                return response;
            }));
        }