// <Data /> // Import Data public void ExecuteRecipeStep(RecipeContext recipeContext) { if (!String.Equals(recipeContext.RecipeStep.Name, "Data", StringComparison.OrdinalIgnoreCase)) { return; } var importContentSession = new ImportContentSession(_orchardServices.ContentManager); // Populate local dictionary with elements and their ids var elementDictionary = CreateElementDictionary(recipeContext.RecipeStep.Step); //Populate import session with all identities to be imported foreach (var identity in elementDictionary.Keys) { importContentSession.Set(identity, elementDictionary[identity].Name.LocalName); } //Determine if the import is to be batched in multiple transactions var startIndex = 0; int batchSize = GetBatchSizeForDataStep(recipeContext.RecipeStep.Step); //Run the import try { while (startIndex < elementDictionary.Count) { importContentSession.InitializeBatch(startIndex, batchSize); //the session determines which items are included in the current batch //so that dependencies can be managed within the same transaction var nextIdentity = importContentSession.GetNextInBatch(); while (nextIdentity != null) { if (!string.IsNullOrEmpty(recipeContext.ExecutionId) && elementDictionary[nextIdentity.ToString()].HasAttributes) { var itemId = elementDictionary[nextIdentity.ToString()].FirstAttribute.Value; _recipeJournal.WriteJournalEntry(recipeContext.ExecutionId, T("Data: Importing {0}.", itemId).Text); } _orchardServices.ContentManager.Import( elementDictionary[nextIdentity.ToString()], importContentSession); nextIdentity = importContentSession.GetNextInBatch(); } startIndex += batchSize; //Create a new transaction for each batch if (startIndex < elementDictionary.Count) { _transactionManager.RequireNew(); } } } catch (Exception) { //Ensure a failed batch is rolled back _transactionManager.Cancel(); throw; } recipeContext.Executed = true; }
public void ItemsSetAndBatchInitialisedReturnsBatchedItems() { var importContentSession = new ImportContentSession(_contentManager.Object); importContentSession.Set("/Id=One", "TestType"); importContentSession.Set("/Id=Two", "TestType"); importContentSession.Set("/Id=Three", "TestType"); importContentSession.Set("/Id=Four", "TestType"); importContentSession.Set("/Id=Five", "TestType"); importContentSession.InitializeBatch(1, 2); var comparer = new ContentIdentity.ContentIdentityEqualityComparer(); Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Two"))); Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Three"))); Assert.That(importContentSession.GetNextInBatch(), Is.Null); importContentSession.InitializeBatch(2, 5); //item with "/Id=Three" should not be returned twice in the same session Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Four"))); Assert.That(comparer.Equals(importContentSession.GetNextInBatch(), new ContentIdentity("/Id=Five"))); Assert.That(importContentSession.GetNextInBatch(), Is.Null); }
public void GetNextInBatchReturnsNullWhenInitializedButNoItemsSet() { var importContentSession = new ImportContentSession(_contentManager.Object); importContentSession.InitializeBatch(0, 20); Assert.That(importContentSession.GetNextInBatch(), Is.Null); }
public void GetNextInBatchInitialisedTwoBatchesReturnsItemsOnceEach() { var session = new ImportContentSession(_contentManager.Object); session.Set(_testItemIdentity1.ToString(), "TestContentType"); session.Set(_testItemIdentity2.ToString(), "TestContentType"); session.Set(_testItemIdentity3.ToString(), "TestContentType"); session.Set(_testItemIdentity4.ToString(), "TestContentType"); session.Set(_testItemIdentity5.ToString(), "TestContentType"); session.InitializeBatch(0, 2); var firstIdentity = session.GetNextInBatch(); //get later item as dependency var dependencyItem = session.Get(_testItemIdentity5.ToString(), VersionOptions.Latest); var dependencyIdentity = session.GetNextInBatch(); var secondIdentity = session.GetNextInBatch(); var afterBatch1 = session.GetNextInBatch(); session.InitializeBatch(2, 2); var thirdIdentity = session.GetNextInBatch(); var fourthdentity = session.GetNextInBatch(); var afterBatch2 = session.GetNextInBatch(); session.InitializeBatch(4, 2); var fifthIdentity = session.GetNextInBatch(); var afterBatch3 = session.GetNextInBatch(); var comparer = new ContentIdentity.ContentIdentityEqualityComparer(); Assert.That(comparer.Equals(_testItemIdentity1, firstIdentity)); Assert.That(comparer.Equals(_testItemIdentity5, dependencyIdentity)); Assert.That(comparer.Equals(_testItemIdentity2, secondIdentity)); Assert.That(afterBatch1, Is.Null); Assert.That(comparer.Equals(_testItemIdentity3, thirdIdentity)); Assert.That(comparer.Equals(_testItemIdentity4, fourthdentity)); Assert.That(afterBatch2, Is.Null); Assert.That(fifthIdentity, Is.Null); //already processed as dependency Assert.That(afterBatch3, Is.Null); }
public void GetNextInBatchInitialisedWithOneItemReturnsOneItemThenNull() { var session = new ImportContentSession(_contentManager.Object); session.Set(_testItemIdentity1.ToString(), "TestContentType"); session.InitializeBatch(0, 1); var firstIdentity = session.GetNextInBatch(); var secondIdentity = session.GetNextInBatch(); var comparer = new ContentIdentity.ContentIdentityEqualityComparer(); Assert.That(comparer.Equals(_testItemIdentity1, firstIdentity)); Assert.That(secondIdentity, Is.Null); }
private void BatchedInvoke(RecipeExecutionContext context, string batchLabel, Action<string, string, XElement, ImportContentSession, IDictionary<string, XElement>> contentItemAction) { var importContentSession = new ImportContentSession(_orchardServices.ContentManager); // Populate local dictionary with elements and their ids. var elementDictionary = CreateElementDictionary(context.RecipeStep.Step); // Populate import session with all identities to be imported. foreach (var identity in elementDictionary.Keys) { importContentSession.Set(identity, elementDictionary[identity].Name.LocalName); } // Determine if the import is to be batched in multiple transactions. var batchSize = GetBatchSizeForDataStep(context.RecipeStep.Step); var startIndex = 0; var itemIndex = 0; Logger.Debug("Using batch size {0} for '{1}'.", batchSize, batchLabel); try { while (startIndex < elementDictionary.Count) { Logger.Debug("Batch '{0}' execution starting at index {1}.", batchLabel, startIndex); importContentSession.InitializeBatch(startIndex, batchSize); // The session determines which items are included in the current batch // so that dependencies can be managed within the same transaction. var nextIdentity = importContentSession.GetNextInBatch(); while (nextIdentity != null) { var itemId = ""; var nextIdentityValue = nextIdentity.ToString(); if (elementDictionary[nextIdentityValue].HasAttributes) { itemId = elementDictionary[nextIdentityValue].FirstAttribute.Value; } Logger.Information("Handling content item '{0}' (item {1}/{2} of '{3}').", itemId, itemIndex + 1, elementDictionary.Count, batchLabel); try { contentItemAction(itemId, nextIdentityValue, elementDictionary[nextIdentityValue], importContentSession, elementDictionary); } catch (Exception ex) { Logger.Error(ex, "Error while handling content item '{0}' (item {1}/{2} of '{3}').", itemId, itemIndex + 1, elementDictionary.Count, batchLabel); throw; } itemIndex++; nextIdentity = importContentSession.GetNextInBatch(); } startIndex += batchSize; // Create a new transaction for each batch. if (startIndex < elementDictionary.Count) { _transactionManager.RequireNew(); } Logger.Debug("Finished batch '{0}' starting at index {1}.", batchLabel, startIndex); } } catch (Exception) { // Ensure a failed batch is rolled back. _transactionManager.Cancel(); throw; } }