Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample import command that uses threading for Sitecore PowerShell Extensions.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Kamsar.WebConsole;
using Rainbow.Model;
using Rainbow.Storage.Sc.Deserialization;
using Sitecore.Data;
using Sitecore.Data.Engines;
using Sitecore.Data.Events;
using Sitecore.SecurityModel;
using Unicorn.Deserialization;
using Unicorn.Logging;
namespace Unicorn.PowerShell
{
[Cmdlet("Import", "RainbowItemBulk")]
public class ImportRainbowYamlBulkCommand : YamlCommandBase
{
protected override void ProcessRecord()
{
if (Item == null) return;
var cancellationToken = new CancellationToken();
var itemsToInstall = new BlockingCollection<IItemData>();
foreach (var newItem in Item)
{
itemsToInstall.Add(newItem, cancellationToken);
}
var threads = 8;
var running = new List<Task>();
for (var i = 0; i < threads; i++)
{
running.Add(Task.Run(() => { ItemInstaller(itemsToInstall, cancellationToken); }, cancellationToken));
}
foreach (var t in running)
{
t.Wait(cancellationToken);
}
itemsToInstall.CompleteAdding();
}
[Parameter(ValueFromPipeline = true)]
public IItemData[] Item { get; set; }
private void ItemInstaller(BlockingCollection<IItemData> itemsToInstall, CancellationToken cancellationToken)
{
Thread.CurrentThread.Priority = ThreadPriority.Lowest;
using (new BulkUpdateContext())
using (new EventDisabler())
using (new SecurityDisabler())
using (new SyncOperationContext())
{
while (itemsToInstall.Any())
{
if (itemsToInstall.TryTake(out var item, int.MaxValue, cancellationToken))
{
ProcessItem(item);
}
}
}
}
private void ProcessItem(IItemData item)
{
if (item == null) return;
var console = new PowershellProgressStatus(Host, "Deserialize Item");
var consoleLogger = new WebConsoleLogger(console, MessageType.Debug);
var deserializer = new DefaultDeserializer(false, new DefaultDeserializerLogger(consoleLogger), CreateFieldFilter());
//consoleLogger.Info(item.Path);
deserializer.Deserialize(item, null);
}
}
}
$directory = "$($SitecoreTempFolder)\items"
<#
if(Test-Path -Path $directory) {
Remove-Item -Path $directory -Recurse
}
New-Item -Path $directory -ItemType Directory
$items = Get-ChildItem -Path "master:" -ID "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"
foreach($item in $items) {
$yaml = $item | ConvertTo-RainbowYaml
$file = New-Item -Path (Join-Path -Path $directory -ChildPath "$($item.Name).yml")
[System.IO.File]::WriteAllText($file, $yaml)
}
#>
Write-Host "Cleaning up old items"
Get-ChildItem -Path "master:" -ID "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}" | Remove-Item -Permanently
Write-Host "Reading file data"
$yamlItems = [System.Collections.ArrayList]@()
foreach($file in Get-ChildItem -Path $directory) {
$yaml = [System.IO.File]::ReadAllText($file.FullName)
$yamlItems.Add($yaml) > $null
}
Write-Host "Item count $($items.Count)"
Write-Host "Importing items"
$watch = [System.Diagnostics.Stopwatch]::StartNew()
$rainbowItems = [System.Collections.ArrayList]@()
<#
foreach($item in $items) {
$yaml = $item | ConvertTo-RainbowYaml
$rainbowItem = ConvertFrom-RainbowYaml -Yaml $yaml
$rainbowItems.Add($rainbowItem) > $null
}#>
foreach($yamlItem in $yamlItems) {
$rainbowItem = ConvertFrom-RainbowYaml -Yaml $yamlItem
$rainbowItems.Add($rainbowItem) > $null
}
Import-RainbowItemBulk -Item $rainbowItems > $null
$watch.Stop()
Write-Host "Import-RainbowItemBulk"
$watch.ElapsedMilliseconds / 1000
Write-Host "Cleaning up old items"
Get-ChildItem -Path "master:" -ID "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}" | Remove-Item -Permanently
Write-Host "Importing items"
$watch = [System.Diagnostics.Stopwatch]::StartNew()
foreach($yamlItem in $yamlItems) {
$rainbowItem = ConvertFrom-RainbowYaml -Yaml $yamlItem
Import-RainbowItem -Item $rainbowItem > $null
}
$watch.Stop()
Write-Host "Import-RainbowItem"
$watch.ElapsedMilliseconds / 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment