/// <summary> /// Cancel task from external repository. /// </summary> /// <param name="externalRepositoryId"></param> public void CancelTask(int externalRepositoryId) { m_importManager.CancelTask(externalRepositoryId); }
public async Task ImportAsync(int externalRepositoryId, CancellationToken cancellationToken) { var externalRepository = m_externalRepositoryManager.GetExternalRepository(externalRepositoryId); var progressInfo = new RepositoryImportProgressInfo(externalRepositoryId, externalRepository.Name); m_importManager.ActualProgress.TryAdd(externalRepositoryId, progressInfo); var latestImportHistory = m_importHistoryManager.GetLatestSuccessfulImportHistory(externalRepositoryId); var importHistoryId = m_importHistoryManager.CreateImportHistory(externalRepositoryId, m_importManager.UserId); ImportPipeline importPipeline = null; try { m_projectImportManagers.TryGetValue(externalRepository.ExternalRepositoryType.Name, out var importManager); if (importManager == null) { throw new ImportFailedException(MainServiceErrorCode.RepositoryImportManagerNotFound, $"The import manager was not found for repository type {externalRepository.ExternalRepositoryType.Name}.", externalRepository.ExternalRepositoryType.Name); } importPipeline = m_importPipelineDirector.Build(m_importPipelineBuilder, externalRepository, importHistoryId, progressInfo, cancellationToken); await importManager.ImportFromResource(externalRepository.Configuration, importPipeline.BufferBlock, progressInfo, latestImportHistory?.Date, cancellationToken); importPipeline.BufferBlock.Complete(); importPipeline.LastBlock.Completion.Wait(cancellationToken); } catch (OperationCanceledException) { progressInfo.FaultedMessage = MainServiceErrorCode.RepositoryImportCancelled; progressInfo.FaultedMessageParams = new object[] { externalRepository.Name }; } catch (AggregateException e) { var errorMessages = new StringBuilder(); var exceptions = e.InnerExceptions; for (var i = 1; i <= exceptions.Count; i++) { errorMessages.AppendLine($"Error #{i}: {exceptions[i].Message}"); } if (m_logger.IsErrorEnabled()) { m_logger.LogError(e, errorMessages.ToString()); } progressInfo.FaultedMessage = MainServiceErrorCode.RepositoryImportFailed; progressInfo.FaultedMessageParams = new object[] { externalRepository.Name }; } catch (ImportFailedException e) { if (m_logger.IsErrorEnabled()) { m_logger.LogError(e, e.Message); } progressInfo.FaultedMessage = e.Code; progressInfo.FaultedMessageParams = e.CodeParams; } catch (Exception e) // Catch every exception otherwise, ProjectImportBackgroundService will be stopped { if (m_logger.IsErrorEnabled()) { m_logger.LogError(e, e.Message); } progressInfo.FaultedMessage = MainServiceErrorCode.RepositoryImportFailed; progressInfo.FaultedMessageParams = new object[] { externalRepository.Name }; } finally { var importHistory = m_importHistoryManager.GetImportHistory(importHistoryId); m_importManager.CancellationTokens.TryGetValue(externalRepositoryId, out var cancellationTokenSource); if (cancellationTokenSource != null && cancellationTokenSource.IsCancellationRequested) { progressInfo.FaultedMessage = MainServiceErrorCode.RepositoryImportCancelled; progressInfo.FaultedMessageParams = new object[] { externalRepository.Name }; importHistory.Message = progressInfo.FaultedMessage; importHistory.Status = ImportStatusEnum.Failed; } else if (!string.IsNullOrEmpty(progressInfo.FaultedMessage)) { importHistory.Message = progressInfo.FaultedMessage; importHistory.Status = ImportStatusEnum.Failed; m_importManager.CancelTask(externalRepositoryId); } else if (progressInfo.FailedProjectsCount > 0) { importHistory.Status = ImportStatusEnum.CompletedWithWarnings; } else { importHistory.Status = ImportStatusEnum.Completed; } progressInfo.IsCompleted = true; m_importHistoryManager.UpdateImportHistory(importHistory); if (!string.IsNullOrEmpty(progressInfo.FaultedMessage)) { importPipeline?.LastBlock.Completion.Wait(cancellationToken); } } }