private async Task MigratePhase2() { Logger.LogInformation("Starting migration phase 2"); IEnumerable <WorkItemMigrationState> successfulWorkItemMigrationStates; // when skip existing config flag is on and this work item was existing, continue to next work item. if (context.Config.SkipExisting) { successfulWorkItemMigrationStates = context.WorkItemsMigrationState.Where(a => a.MigrationState == WorkItemMigrationState.State.Create); } else { // allow any Create, OR Existing with UpdatePhase2 successfulWorkItemMigrationStates = context.WorkItemsMigrationState.Where(a => a.MigrationState == WorkItemMigrationState.State.Create || (a.MigrationState == WorkItemMigrationState.State.Existing && a.Requirement.HasFlag(WorkItemMigrationState.RequirementForExisting.UpdatePhase2))); } var phase2WorkItemsToUpdateCount = successfulWorkItemMigrationStates.Count(); var totalNumberOfBatches = ClientHelpers.GetBatchCount(phase2WorkItemsToUpdateCount, Constants.BatchSize); if (phase2WorkItemsToUpdateCount == 0) { Logger.LogInformation(LogDestination.File, "No work items to process for phase 2"); return; } await successfulWorkItemMigrationStates.Batch(Constants.BatchSize).ForEachAsync(context.Config.Parallelism, async(workItemMigrationStateBatch, batchId) => { Logger.LogTrace(LogDestination.File, $"Reading Phase 2 source and target work items for batch {batchId} of {totalNumberOfBatches}"); // make web call to get source and target work items IList <WorkItem> sourceWorkItemsInBatch = await WorkItemTrackingHelpers.GetWorkItemsAsync(context.SourceClient.WorkItemTrackingHttpClient, workItemMigrationStateBatch.Select(a => a.SourceId).ToList(), expand: WorkItemExpand.All); IList <WorkItem> targetWorkItemsInBatch = await WorkItemTrackingHelpers.GetWorkItemsAsync(context.TargetClient.WorkItemTrackingHttpClient, workItemMigrationStateBatch.Select(a => a.TargetId.Value).ToList(), expand: WorkItemExpand.Relations); IBatchMigrationContext batchContext = new BatchMigrationContext(batchId, workItemMigrationStateBatch); batchContext.SourceWorkItemIdToTargetWorkItemIdMapping = workItemMigrationStateBatch.ToDictionary(key => key.SourceId, value => value.TargetId.Value); foreach (var sourceWorkItem in sourceWorkItemsInBatch) { int targetId = Migrator.GetTargetId(sourceWorkItem.Id.Value, workItemMigrationStateBatch); batchContext.TargetIdToSourceWorkItemMapping.Add(targetId, sourceWorkItem); } Logger.LogTrace(LogDestination.File, $"Generating Phase 2 json patch operations for batch {batchId} of {totalNumberOfBatches}"); var sourceIdToWitBatchRequests = await GenerateWitBatchRequestsForPhase2Batch(batchContext, batchId, workItemMigrationStateBatch, sourceWorkItemsInBatch, targetWorkItemsInBatch); Logger.LogTrace(LogDestination.File, $"Saving Phase 2 json patch operations for batch {batchId} of {totalNumberOfBatches}"); var phase2ApiWrapper = new Phase2ApiWrapper(); await phase2ApiWrapper.ExecuteWitBatchRequests(sourceIdToWitBatchRequests, context, batchContext); Logger.LogTrace(LogDestination.File, $"Completed Phase 2 for batch {batchId} of {totalNumberOfBatches}"); }); Logger.LogInformation("Completed migration phase 2"); }
private async Task MigratePhase3() { IEnumerable <IPhase3Processor> phase3Processors = ClientHelpers.GetProcessorInstances <IPhase3Processor>(context.Config); if (phase3Processors != null && !phase3Processors.Any()) { // nothing to do if no phase 3 processors are enabled return; } // Phase1 or Phase2 have completed, and FailureReason == None IEnumerable <WorkItemMigrationState> successfullyMigratedWorkItemMigrationStates = context.WorkItemsMigrationState.Where(w => (w.MigrationCompleted.HasFlag(WorkItemMigrationState.MigrationCompletionStatus.Phase1) || w.MigrationCompleted.HasFlag(WorkItemMigrationState.MigrationCompletionStatus.Phase2)) && w.FailureReason == FailureReason.None); var phase3WorkItemsToUpdateCount = successfullyMigratedWorkItemMigrationStates.Count(); var totalNumberOfBatches = ClientHelpers.GetBatchCount(phase3WorkItemsToUpdateCount, Constants.BatchSize); if (phase3WorkItemsToUpdateCount == 0) { return; } await successfullyMigratedWorkItemMigrationStates.Batch(Constants.BatchSize).ForEachAsync(context.Config.Parallelism, async(workItemMigrationStateBatch, batchId) => { IBatchMigrationContext batchContext = new BatchMigrationContext(batchId, workItemMigrationStateBatch); IList <(int SourceId, WitBatchRequest WitBatchRequest)> sourceIdToWitBatchRequests = new List <(int SourceId, WitBatchRequest WitBatchRequest)>(); IList <WorkItem> sourceWorkItemsInBatch = await WorkItemTrackingHelpers.GetWorkItemsAsync(context.SourceClient.WorkItemTrackingHttpClient, workItemMigrationStateBatch.Select(a => a.SourceId).ToList(), expand: WorkItemExpand.All); foreach (WorkItem sourceWorkItem in sourceWorkItemsInBatch) { IList <JsonPatchOperation> jsonPatchOperations = new List <JsonPatchOperation>(); foreach (IPhase3Processor processor in phase3Processors) { IEnumerable <JsonPatchOperation> processorJsonPatchOperations = await processor.Process(context, null, sourceWorkItem, null); jsonPatchOperations.AddRange(processorJsonPatchOperations); } if (jsonPatchOperations.Any()) { WitBatchRequest witBatchRequest = GenerateWitBatchRequestFromJsonPatchOperations(jsonPatchOperations, sourceWorkItem.Id.Value); sourceIdToWitBatchRequests.Add((sourceWorkItem.Id.Value, witBatchRequest)); } } var phase3ApiWrapper = new Phase3ApiWrapper(); await phase3ApiWrapper.ExecuteWitBatchRequests(sourceIdToWitBatchRequests, context, batchContext); }); }
public async Task Process(IMigrationContext context) { var workItemsAndStateToMigrate = this.GetWorkItemsAndStateToMigrate(context); var totalNumberOfBatches = ClientHelpers.GetBatchCount(workItemsAndStateToMigrate.Count, Constants.BatchSize); if (!workItemsAndStateToMigrate.Any()) { Logger.LogInformation(LogDestination.File, $"No work items to process for {this.Name}"); return; } Logger.LogInformation(LogDestination.All, $"{this.Name} will process {workItemsAndStateToMigrate.Count} work items on the target"); var preprocessors = ClientHelpers.GetProcessorInstances <IPhase1PreProcessor>(context.Config); foreach (var preprocessor in preprocessors) { await preprocessor.Prepare(context); } await workItemsAndStateToMigrate.Batch(Constants.BatchSize).ForEachAsync(context.Config.Parallelism, async(batchWorkItemsAndState, batchId) => { var batchStopwatch = Stopwatch.StartNew(); Logger.LogInformation(LogDestination.File, $"{this.Name} batch {batchId} of {totalNumberOfBatches}: Starting"); IBatchMigrationContext batchContext = new BatchMigrationContext(batchId, batchWorkItemsAndState); //read the work items var stepStopwatch = Stopwatch.StartNew(); Logger.LogTrace(LogDestination.File, $"{this.Name} batch {batchId} of {totalNumberOfBatches}: Reading source work items"); await Migrator.ReadSourceWorkItems(context, batchWorkItemsAndState.Select(w => w.SourceId), batchContext); Logger.LogTrace(LogDestination.File, $"{this.Name} batch {batchId} of {totalNumberOfBatches}: Completed reading source work items in {stepStopwatch.Elapsed.Seconds}s"); this.PrepareBatchContext(batchContext, batchWorkItemsAndState); foreach (var preprocessor in preprocessors) { stepStopwatch.Restart(); Logger.LogTrace(LogDestination.File, $"{this.Name} batch {batchId} of {totalNumberOfBatches}: Starting {preprocessor.Name}"); await preprocessor.Process(batchContext); Logger.LogTrace(LogDestination.File, $"{this.Name} batch {batchId} of {totalNumberOfBatches}: Completed {preprocessor.Name} in {stepStopwatch.Elapsed.Seconds}s"); } var workItemsToUpdateCount = this.GetWorkItemsToProcessCount(batchContext); Logger.LogInformation(LogDestination.File, $"{this.Name} batch {batchId} of {totalNumberOfBatches}: Number of work items to migrate: {workItemsToUpdateCount}"); //migrate the batch of work items if (workItemsToUpdateCount == 0) { batchStopwatch.Stop(); Logger.LogWarning(LogDestination.File, $"{this.Name} batch {batchId} of {totalNumberOfBatches}: No work items to migrate"); } else { stepStopwatch.Restart(); Logger.LogTrace(LogDestination.File, $"{this.Name} batch {batchId} of {totalNumberOfBatches}: Saving the target work items"); var witBatchRequestGenerator = this.GetWitBatchRequestGenerator(context, batchContext); await witBatchRequestGenerator.Write(); Logger.LogTrace(LogDestination.File, $"{this.Name} batch {batchId} of {totalNumberOfBatches}: Completed saving the target work items in {stepStopwatch.Elapsed.Seconds}s"); batchStopwatch.Stop(); Logger.LogInformation(LogDestination.File, $"{this.Name} batch {batchId} of {totalNumberOfBatches}: Completed in {batchStopwatch.Elapsed.TotalSeconds}s"); } }); }