Skip to content

Commit f0bf2ee

Browse files
committed
Refactoring of Inspector to use Parallel.ForEach to run all inspections and proper logging of cancellation exceptions when running the inspection.
1 parent d4cd583 commit f0bf2ee

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

Rubberduck.Inspections/Inspector.cs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace Rubberduck.Inspections
2121
{
2222
public class Inspector : IInspector
2323
{
24+
private const int _maxDegreeOfInspectionParallelism = -1;
25+
2426
private readonly IGeneralConfigService _configService;
2527
private readonly List<IInspection> _inspections;
2628
private readonly int AGGREGATION_THRESHOLD = 128;
@@ -90,29 +92,22 @@ public async Task<IEnumerable<IInspectionResult>> FindIssuesAsync(RubberduckPars
9092
}
9193
}
9294

93-
var inspections = _inspections.Where(inspection => inspection.Severity != CodeInspectionSeverity.DoNotShow)
94-
.Select(inspection =>
95-
Task.Run(() =>
96-
{
97-
token.ThrowIfCancellationRequested();
98-
try
99-
{
100-
var inspectionResults = inspection.GetInspectionResults();
101-
102-
foreach (var inspectionResult in inspectionResults)
103-
{
104-
allIssues.Add(inspectionResult);
105-
}
106-
}
107-
catch(Exception e)
108-
{
109-
LogManager.GetCurrentClassLogger().Warn(e);
110-
}
111-
}, token)).ToList();
95+
var inspectionsToRun = _inspections.Where(inspection => inspection.Severity != CodeInspectionSeverity.DoNotShow);
11296

11397
try
11498
{
115-
await Task.WhenAll(inspections);
99+
await Task.Run(() => RunInspectionsInParallel(inspectionsToRun, allIssues, token));
100+
}
101+
catch (AggregateException exception)
102+
{
103+
if (exception.Flatten().InnerExceptions.All(ex => ex is OperationCanceledException))
104+
{
105+
LogManager.GetCurrentClassLogger().Debug("Inspections got canceled.");
106+
}
107+
else
108+
{
109+
LogManager.GetCurrentClassLogger().Error(exception);
110+
}
116111
}
117112
catch (Exception e)
118113
{
@@ -131,6 +126,38 @@ public async Task<IEnumerable<IInspectionResult>> FindIssuesAsync(RubberduckPars
131126
return results;
132127
}
133128

129+
private static void RunInspectionsInParallel(IEnumerable<IInspection> inspectionsToRun,
130+
ConcurrentBag<IInspectionResult> allIssues, CancellationToken token)
131+
{
132+
var options = new ParallelOptions
133+
{
134+
CancellationToken = token,
135+
MaxDegreeOfParallelism = _maxDegreeOfInspectionParallelism
136+
};
137+
138+
Parallel.ForEach(inspectionsToRun,
139+
options,
140+
inspection => RunInspection(inspection, allIssues)
141+
);
142+
}
143+
144+
private static void RunInspection(IInspection inspection, ConcurrentBag<IInspectionResult> allIssues)
145+
{
146+
try
147+
{
148+
var inspectionResults = inspection.GetInspectionResults();
149+
150+
foreach (var inspectionResult in inspectionResults)
151+
{
152+
allIssues.Add(inspectionResult);
153+
}
154+
}
155+
catch (Exception e)
156+
{
157+
LogManager.GetCurrentClassLogger().Warn(e);
158+
}
159+
}
160+
134161
private void WalkTrees(CodeInspectionSettings settings, RubberduckParserState state, IEnumerable<IParseTreeInspection> inspections, ParsePass pass)
135162
{
136163
var listeners = inspections

0 commit comments

Comments
 (0)