public async Task Upload(object metricsBody, string eventType) { try { var payload = new MetricsMessage { Application = _config.Application, Body = metricsBody, CorrelationId = _correlationId, Environment = _config.Environment, EventType = eventType, }; var serializedPayload = JsonConvert.SerializeObject(payload, _jsonSerializerSettings); var response = await Retry.DoWithRetries <FlurlHttpException, HttpResponseMessage>(nameof(MetricUploader), _logger, async() => await _config.EndpointUrl .WithTimeout(_config.UploadTimeout) .PostAsync(new StringContent(serializedPayload, Encoding.UTF8, "application/json"))); if (!response.IsSuccessStatusCode) { _logger.LogWarning("Successful status code not returned from telemetry upload."); } } catch (Exception ex) { _logger.LogWarning(ex, "Exception occurred during telemetry upload."); } }
private async Task <T> ExecuteQueryWithRetries <T>( string connectionString, Func <NpgsqlConnection, Task <T> > queryFunction, string filePath, string memberName, int lineNumber) { try { return(await Retry.DoWithRetries <NpgsqlException, T>( nameof(NpgsqlDataContext), _logger, async() => { using (var connection = new NpgsqlConnectionWrapper(connectionString)) { return await queryFunction(connection.Connection); } }, 5, 10)); } catch (NpgsqlException ex) { LogError(ex, filePath, memberName, lineNumber); throw; } catch (NpgsqlOperationInProgressException ex) { LogError(ex, filePath, memberName, lineNumber); throw; } }
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); } }
private async Task <WorkbookPublishResult> PublishWorkbook(string projectId, CompletedWorkbookInfo completedWorkbookInfo, TableauServerRestApi restApi, IList <string> tags) { if (!completedWorkbookInfo.GeneratedSuccessfully) { return(new WorkbookPublishResult( 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, }; WorkbookInfo workbookInfo; try { workbookInfo = await Retry.DoWithRetries <RestApiException, WorkbookInfo>( nameof(WorkbookPublisher), _logger, async() => await restApi.PublishWorkbook(publishWorkbookRequest)); _logger.LogDebug("Workbook `{publishedWorkbookName}` was published to Tableau Server as ID `{newWorkbookId}`", publishWorkbookRequest.WorkbookNameOnTableauServer, workbookInfo.Id); } catch (RestApiException ex) { var errorMessage = $"Failed to publish workbook `{completedWorkbookInfo.WorkbookPath}` after multiple retries. Exception was: {ex.Message}"; _logger.LogError(errorMessage); return(new WorkbookPublishResult(completedWorkbookInfo.OriginalWorkbookName, new WorkbookPublishingException(errorMessage))); } if (_publisherSettings.ApplyPluginProvidedTagsToWorkbooks && tags != null && tags.Count > 0) { await AddTagsToWorkbook(workbookInfo, tags, restApi); } return(new WorkbookPublishResult(workbookInfo.Name)); }
public async Task <PublisherResults> PublishWorkbooks( string projectName, IEnumerable <CompletedWorkbookInfo> completedWorkbooks, IEnumerable <string> workbookTags) { var tableauServerInfo = _publisherSettings.TableauServerInfo; try { using var restApi = await Retry.DoWithRetries <RestApiException, TableauServerRestApi>( "WorkbookPublisher -> Init TS API", _logger, () => TableauServerRestApi.InitApi(tableauServerInfo, _loggerFactory.CreateLogger <TableauServerRestApi>()), 60, 120); var projectInfo = await Retry.DoWithRetries <RestApiException, ProjectInfo>( "WorkbookPublisher -> Create Project", _logger, async() => { projectName = await FindUniqueProjectName(projectName, restApi); var projectDescription = GetProjectDescription(); var newProjectInfo = await restApi.CreateProject(projectName, projectDescription, _publisherSettings.ParentProjectInfo.Id, _publisherSettings.ParentProjectInfo.Name); await SetDefaultPermissions(restApi, newProjectInfo); return(newProjectInfo); }); var tags = workbookTags?.ToList(); var publishingResults = completedWorkbooks .Select(cwi => PublishWorkbook(projectInfo.Id, cwi, restApi, tags).Result) // .Result here ensures that we publish one workbook at a time instead of trying to push 10+ of them at the same time .ToList(); return(new PublisherResults(tableauServerInfo.Url, tableauServerInfo.Site, restApi.ContentUrl, projectInfo.Name, publishingResults)); } catch (RestApiException ex) { _logger.LogError("Exception encountered when attempting to sign in or create the project: {exceptionMessage}", ex.Message); return(new PublisherResults(tableauServerInfo.Url, tableauServerInfo.Site, null, projectName, null, ex)); } }