Skip to content

Commit

Permalink
ensure timer is stopped before disposing
Browse files Browse the repository at this point in the history
  • Loading branch information
rkm committed Aug 24, 2023
1 parent 7dbe860 commit 03f0769
Showing 1 changed file with 72 additions and 66 deletions.
138 changes: 72 additions & 66 deletions IsIdentifiable/Reporting/Reports/FailureStoreReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,81 +161,87 @@ public static IEnumerable<Failure> Deserialize(IFileInfo oldFile, Action<int> lo

var failures = new ConcurrentBag<Failure>();

Parallel.ForEach(
reader.GetRecords<FailureStoreReportRecord>(),
new ParallelOptions
{
CancellationToken = token,
},
(FailureStoreReportRecord row) =>
{
if (row.ProblemValue == null)
throw new Exception("ProblemValue was null");
var words = row.PartWords.Split(Separator);
var classes = row.PartClassifications.Split(Separator);
var offsets = row.PartOffsets.Split(Separator);
var parts = words.Select(
(word, index) => new FailurePart(
word,
Enum.TryParse<FailureClassification>(classes[index], true, out var classification) ? classification : throw new Exception($"Invalid failure classification '{classes[index]}'"),
int.TryParse(offsets[index], out var offset) ? offset : throw new Exception($"Invalid offset '{row.PartOffsets}'")
)
).ToList();
if (row.ProblemField != "PixelData")
try
{
Parallel.ForEach(
reader.GetRecords<FailureStoreReportRecord>(),
new ParallelOptions
{
CancellationToken = token,
},
(FailureStoreReportRecord row) =>
{
// Fixes any offsets that have been mangled by file endings etc.
foreach (var part in parts)
if (row.ProblemValue == null)
throw new Exception("ProblemValue was null");
var words = row.PartWords.Split(Separator);
var classes = row.PartClassifications.Split(Separator);
var offsets = row.PartOffsets.Split(Separator);
var parts = words.Select(
(word, index) => new FailurePart(
word,
Enum.TryParse<FailureClassification>(classes[index], true, out var classification) ? classification : throw new Exception($"Invalid failure classification '{classes[index]}'"),
int.TryParse(offsets[index], out var offset) ? offset : throw new Exception($"Invalid offset '{row.PartOffsets}'")
)
).ToList();
if (row.ProblemField != "PixelData")
{
if (row.ProblemValue.Substring(part.Offset, part.Word.Length) == part.Word)
continue;
// Try looking ahead first, then back
var origOffset = part.Offset;
try
{
while (row.ProblemValue.Substring(part.Offset, part.Word.Length) != part.Word)
part.Offset++;
}
catch (ArgumentOutOfRangeException)
// Fixes any offsets that have been mangled by file endings etc.
foreach (var part in parts)
{
part.Offset = origOffset;
while (row.ProblemValue.Substring(part.Offset, part.Word.Length) != part.Word)
part.Offset--;
if (row.ProblemValue.Substring(part.Offset, part.Word.Length) == part.Word)
continue;
// Try looking ahead first, then back
var origOffset = part.Offset;
try
{
while (row.ProblemValue.Substring(part.Offset, part.Word.Length) != part.Word)
part.Offset++;
}
catch (ArgumentOutOfRangeException)
{
part.Offset = origOffset;
while (row.ProblemValue.Substring(part.Offset, part.Word.Length) != part.Word)
part.Offset--;
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.
}
}
/* TEMP - Filter out any FailureParts covered by an PartPatternFilterRule */
var toRemove = new List<FailurePart>();
foreach (var partRule in partRules)
{
if (!string.IsNullOrWhiteSpace(partRule.IfColumn) && !string.Equals(partRule.IfColumn, row.ProblemField, StringComparison.InvariantCultureIgnoreCase))
continue;
/* TEMP - Filter out any FailureParts covered by an PartPatternFilterRule */
var toRemove = new List<FailurePart>();
foreach (var partRule in partRules)
{
if (!string.IsNullOrWhiteSpace(partRule.IfColumn) && !string.Equals(partRule.IfColumn, row.ProblemField, StringComparison.InvariantCultureIgnoreCase))
continue;
foreach (var part in parts.Where(x => partRule.Covers(x, row.ProblemValue)))
toRemove.Add(part);
}
parts = parts.Except(toRemove).ToList();
/* TEMP */
foreach (var part in parts.Where(x => partRule.Covers(x, row.ProblemValue)))
toRemove.Add(part);
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.
parts = parts.Except(toRemove).ToList();
/* TEMP */
if (parts.Any())
failures.Add(new Failure(parts)
{
Resource = row.Resource,
ResourcePrimaryKey = row.ResourcePrimaryKey,
ProblemField = row.ProblemField,
ProblemValue = row.ProblemValue,
});
Interlocked.Increment(ref totalProcessed);
}
);
if (parts.Any())
failures.Add(new Failure(parts)
{
Resource = row.Resource,
ResourcePrimaryKey = row.ResourcePrimaryKey,
ProblemField = row.ProblemField,
ProblemValue = row.ProblemValue,
});
Interlocked.Increment(ref totalProcessed);
}
);
}
finally
{
localTokenSource.Cancel();
timerTask.Wait();
}

localTokenSource.Cancel();
timerTask.Wait();
loadedRows(totalProcessed);

return failures;
Expand Down

0 comments on commit 03f0769

Please sign in to comment.