예제 #1
0
 private async Task AddTagsToWorkbook(WorkbookPublishResult workbookInfo, IList <string> tagsToAdd, TableauServerRestApi restApi)
 {
     try
     {
         await Retry.DoWithRetries <RestApiException, IList <TagInfo> >(
             nameof(WorkbookPublisher),
             _logger,
             async() => await restApi.AddTagsToWorkbook(workbookInfo.PublishedWorkbookId, tagsToAdd));
     }
     catch (RestApiException ex)
     {
         _logger.LogWarning(
             "Failed to add tags to workbook '{workbookName}' ({workbookId}). Exception message: {exceptionAddingTags}",
             workbookInfo.PublishedWorkbookName ?? "(null)",
             workbookInfo.PublishedWorkbookId ?? "(null)",
             ex.Message);
     }
 }
예제 #2
0
        private async Task <WorkbookPublishResult> PublishWorkbook(string projectId, CompletedWorkbookInfo completedWorkbookInfo, TableauServerRestApi restApi, IList <string> tags)
        {
            if (!completedWorkbookInfo.GeneratedSuccessfully)
            {
                return(WorkbookPublishResult.Fail(
                           completedWorkbookInfo.OriginalWorkbookName,
                           new WorkbookPublishingException($"Workbook {completedWorkbookInfo.OriginalWorkbookName} was not generated successfully. Skipping publishing", completedWorkbookInfo.Exception)));
            }

            var publishWorkbookRequest = new PublishWorkbookRequest(
                completedWorkbookInfo.WorkbookPath,
                projectId,
                completedWorkbookInfo.FinalWorkbookName,
                overwriteExistingWorkbook: true)
            {
                Credentials = _dbCreds,
            };

            WorkbookPublishResult workbookPublishResult;

            try
            {
                var retryOnExceptionPolicy = Policy
                                             .Handle <RestApiException>()
                                             .WaitAndRetryAsync(
                    new []
                {
                    TimeSpan.FromSeconds(30),
                    TimeSpan.FromSeconds(60),
                },
                    (exception, timeSpan, retryCount, context) =>
                {
                    _logger.LogDebug("Got an exception trying to publish workbook `{failedWorkbookName}`. This is retry number {retryCount}. Exception was: `{exceptionMessage}`", completedWorkbookInfo.FinalWorkbookName ?? "null", retryCount, exception?.Message ?? "null");
                });

                var handleExceptionPolicy = Policy <WorkbookPublishResult>
                                            .Handle <RestApiException>(ex => ex.IsTimeoutException)
                                            .FallbackAsync(WorkbookPublishResult.Timeout(completedWorkbookInfo.FinalWorkbookName));

                workbookPublishResult = await retryOnExceptionPolicy
                                        .WrapAsync(handleExceptionPolicy)
                                        .ExecuteAsync(async() =>
                {
                    var workbookInfo = await restApi.PublishWorkbook(publishWorkbookRequest);
                    return(WorkbookPublishResult.Success(completedWorkbookInfo.FinalWorkbookName, workbookInfo));
                });
            }
            catch (RestApiException ex)
            {
                var errorMessage = $"Failed to publish workbook `{completedWorkbookInfo.WorkbookPath}` after multiple retries. Exception was: {ex.Message}";
                _logger.LogError(errorMessage);
                return(WorkbookPublishResult.Fail(completedWorkbookInfo.OriginalWorkbookName, new WorkbookPublishingException(errorMessage)));
            }

            if (workbookPublishResult.PublishState == WorkbookPublishResult.WorkbookPublishState.Success &&
                _publisherSettings.ApplyPluginProvidedTagsToWorkbooks &&
                tags != null &&
                tags.Count > 0)
            {
                await AddTagsToWorkbook(workbookPublishResult, tags, restApi);
            }

            if (workbookPublishResult.PublishState == WorkbookPublishResult.WorkbookPublishState.Success)
            {
                _logger.LogDebug("Workbook `{publishedWorkbookName}` was published to Tableau Server as ID `{newWorkbookId}`", publishWorkbookRequest.WorkbookNameOnTableauServer, workbookPublishResult.PublishedWorkbookId);
            }
            else
            {
                _logger.LogDebug("Publishing attempt for workbook `{originalWorkbookName}` returned non-successful publishing status. Status: {publishingStatus}", publishWorkbookRequest.WorkbookNameOnTableauServer, workbookPublishResult.PublishState.ToString());
            }

            return(workbookPublishResult);
        }