Skip to content

Commit

Permalink
refactor(WorkItemCloneCommand.cs): add exception handling for cache l…
Browse files Browse the repository at this point in the history
…oading to prevent crashes

feat(WorkItemCloneCommand.cs): add cache stale check to ensure data freshness
fix(WorkItemCloneCommand.cs): correct task count display off-by-one error in Stage 6 description
  • Loading branch information
MrHinsh committed Jul 17, 2024
1 parent 2e89eeb commit c003a57
Showing 1 changed file with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.VisualStudio.Services.CircuitBreaker;
using Newtonsoft.Json.Linq;
using System.Diagnostics.Eventing.Reader;
using AzureDevOps.WorkItemClone.Repositories;

namespace AzureDevOps.WorkItemClone.ConsoleUI.Commands
{
Expand Down Expand Up @@ -97,27 +96,40 @@ await AnsiConsole.Progress()
if (System.IO.File.Exists(cacheTemplateWorkItemsFile))
{
// load Cache
templateWorkItems = JsonConvert.DeserializeObject<CashedWorkItems>(System.IO.File.ReadAllText(cacheTemplateWorkItemsFile));
//Test Cache date
QueryResults changedWorkItems = await templateApi.GetWiqlQueryResults("Select [System.Id] From WorkItems Where [System.TeamProject] = '@project' AND [System.Parent] = @id AND [System.ChangedDate] > '@changeddate' order by [System.CreatedDate] desc", new Dictionary<string, string>() { { "@id", config.templateParentId.ToString() }, { "@changeddate", templateWorkItems.queryDatetime.AddDays(-1).ToString("yyyy-MM-dd") } });
if (changedWorkItems.workItems.Length == 0)
try
{
AnsiConsole.WriteLine($"Stage 1: Checked template for changes. None Detected. Loading Cache");

// Load from Cache

task1.Increment(1);
task1.Description = task1.Description + " (cache)";
await Task.Delay(250);
task1.StopTask();
//////////////////////
task2.Increment(templateWorkItems.workitems.Count());
task2.Description = task2.Description + " (cache)";
AnsiConsole.WriteLine($"Stage 2: Loaded {templateWorkItems.workitems.Count()} work items from cache.");
} else
templateWorkItems = JsonConvert.DeserializeObject<CashedWorkItems>(System.IO.File.ReadAllText(cacheTemplateWorkItemsFile));
}
catch (Exception ex)
{
templateWorkItems = null;
// failed to load
AnsiConsole.WriteLine($"Cache is moldy, reloading..");
}
if (templateWorkItems != null)
{
//Test Cache date
QueryResults changedWorkItems = await templateApi.GetWiqlQueryResults("Select [System.Id] From WorkItems Where [System.TeamProject] = '@project' AND [System.Parent] = @id AND [System.ChangedDate] > '@changeddate' order by [System.CreatedDate] desc", new Dictionary<string, string>() { { "@id", config.templateParentId.ToString() }, { "@changeddate", templateWorkItems.queryDatetime.AddDays(-1).ToString("yyyy-MM-dd") } });
if (changedWorkItems.workItems.Length == 0)
{
AnsiConsole.WriteLine($"Stage 1: Checked template for changes. None Detected. Loading Cache");

// Load from Cache

task1.Increment(1);
task1.Description = task1.Description + " (cache)";
await Task.Delay(250);
task1.StopTask();
//////////////////////
task2.Increment(templateWorkItems.workitems.Count());
task2.Description = task2.Description + " (cache)";
AnsiConsole.WriteLine($"Stage 2: Loaded {templateWorkItems.workitems.Count()} work items from cache.");
}
else
{
AnsiConsole.WriteLine($"Cache is stale, reloading..");
templateWorkItems = null;
}
}
}

if (templateWorkItems == null)
Expand All @@ -126,10 +138,9 @@ await AnsiConsole.Progress()
// --------------------------------------------------------------
// Task 1: query for template work items
task1.StartTask();

templateWorkItems = new CashedWorkItems() { workitems = new List<WorkItemFull>(), queryDatetime = DateTime.Now };
//AnsiConsole.WriteLine("Stage 1: Executing items from Query");
QueryResults templateWorkItemLight;
DateTime queryDatetime = DateTime.Now;
templateWorkItemLight = await templateApi.GetWiqlQueryResults("Select [System.Id] From WorkItems Where [System.TeamProject] = '@project' AND [System.Parent] = @id order by [System.CreatedDate] desc", new Dictionary<string, string>() { { "@id", config.templateParentId.ToString() } });
AnsiConsole.WriteLine($"Stage 1: Query returned {templateWorkItemLight.workItems.Count()} items id's from the template.");
task1.Increment(1);
Expand All @@ -138,9 +149,7 @@ await AnsiConsole.Progress()
// Task 2: getting work items and their full data
task2.MaxValue = templateWorkItemLight.workItems.Count();
task2.StartTask();
await Task.Delay(250);
//AnsiConsole.WriteLine($"Stage 2: Starting process of {task2.MaxValue} work items to get their full data ");
templateWorkItems.workitems = new List<WorkItemFull>();
await Task.Delay(250);
//AnsiConsole.WriteLine($"Stage 2: Loading {fakeItemsFromTemplateQuery.workItems.Count()} work items from template.");
await foreach (var workItem in templateApi.GetWorkItemsFullAsync(templateWorkItemLight.workItems))
{
Expand Down Expand Up @@ -255,7 +264,7 @@ await AnsiConsole.Progress()
//AnsiConsole.WriteLine($"Stage 6: Processing {witb.guid} for output of {witb.relations.Count - 1} relations");
task6.Increment(1);
taskCount++;
task6.Description = $"[bold]Stage 6[/]: Create Work Items ({taskCount}/{buildItems.Count()} c:{result.created}, s:{result.skipped}, f:{result.failed})";
task6.Description = $"[bold]Stage 6[/]: Create Work Items ({taskCount-1}/{buildItems.Count()} c:{result.created}, s:{result.skipped}, f:{result.failed})";
switch (result.status)
{
case "created":
Expand Down

0 comments on commit c003a57

Please sign in to comment.