Skip to content

Commit a2e689b

Browse files
author
Kapil Borle
committed
Use runspacepool in analysis service
1 parent c69cdee commit a2e689b

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/PowerShellEditorServices/Analysis/AnalysisService.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class AnalysisService : IDisposable
2525
{
2626
#region Private Fields
2727

28-
private Runspace analysisRunspace;
28+
private RunspacePool analysisRunspacePool;
2929
private PSModuleInfo scriptAnalyzerModuleInfo;
3030
private string[] activeRules;
3131
private string settingsPath;
@@ -101,9 +101,19 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
101101
try
102102
{
103103
this.SettingsPath = settingsPath;
104-
this.analysisRunspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault2());
105-
this.analysisRunspace.ThreadOptions = PSThreadOptions.ReuseThread;
106-
this.analysisRunspace.Open();
104+
var sessionState = InitialSessionState.CreateDefault2();
105+
106+
// import PSScriptAnalyzer in all runspaces
107+
sessionState.ImportPSModule(new string[] { "PSScriptAnalyzer" });
108+
109+
// runspacepool takes care of queuing commands for us so we do not
110+
// need to worry about executing concurrent commands
111+
this.analysisRunspacePool = RunspaceFactory.CreateRunspacePool(sessionState);
112+
113+
// one runspace for code formatting and the other for markers
114+
this.analysisRunspacePool.SetMaxRunspaces(2);
115+
this.analysisRunspacePool.ThreadOptions = PSThreadOptions.ReuseThread;
116+
this.analysisRunspacePool.Open();
107117
ActiveRules = IncludedRules.ToArray();
108118
InitializePSScriptAnalyzer();
109119
}
@@ -186,11 +196,11 @@ public Hashtable GetPSSASettingsHashtable(IDictionary<string, Hashtable> ruleSet
186196
/// </summary>
187197
public void Dispose()
188198
{
189-
if (this.analysisRunspace != null)
199+
if (this.analysisRunspacePool != null)
190200
{
191-
this.analysisRunspace.Close();
192-
this.analysisRunspace.Dispose();
193-
this.analysisRunspace = null;
201+
this.analysisRunspacePool.Close();
202+
this.analysisRunspacePool.Dispose();
203+
this.analysisRunspacePool = null;
194204
}
195205
}
196206

@@ -305,7 +315,12 @@ private void EnumeratePSScriptAnalyzerRules()
305315
private void InitializePSScriptAnalyzer()
306316
{
307317
FindPSScriptAnalyzer();
318+
319+
// this import is redundant if we are importing the
320+
// module while creating the runspace, but it helps
321+
// us log the import related messages.
308322
ImportPSScriptAnalyzer();
323+
309324
EnumeratePSScriptAnalyzerRules();
310325
}
311326

@@ -381,7 +396,7 @@ private async Task<IEnumerable<PSObject>> InvokePowerShellAsync(string command,
381396
{
382397
using (var powerShell = System.Management.Automation.PowerShell.Create())
383398
{
384-
powerShell.Runspace = this.analysisRunspace;
399+
powerShell.RunspacePool = this.analysisRunspacePool;
385400
powerShell.AddCommand(command);
386401
foreach (var kvp in paramArgMap)
387402
{

0 commit comments

Comments
 (0)