Skip to content

Commit c8b596d

Browse files
authored
Clean up jobs after the function execution (#171)
1 parent be85cc9 commit c8b596d

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/DependencyManagement/DependencyManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ internal void InstallFunctionAppDependencies(PowerShell pwsh, ILogger logger)
174174
finally
175175
{
176176
// Clean up
177-
pwsh.AddCommand("Microsoft.PowerShell.Core\\Remove-Module")
177+
pwsh.AddCommand(Utils.RemoveModuleCmdletInfo)
178178
.AddParameter("Name", "PackageManagement, PowerShellGet")
179179
.AddParameter("Force", true)
180180
.AddParameter("ErrorAction", "SilentlyContinue")

src/PowerShell/PowerShellManager.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ internal void InvokeProfile(string profilePath)
9999
try
100100
{
101101
// Import-Module on a .ps1 file will evaluate the script in the global scope.
102-
_pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
102+
_pwsh.AddCommand(Utils.ImportModuleCmdletInfo)
103103
.AddParameter("Name", profilePath)
104104
.AddParameter("PassThru", true)
105-
.AddCommand("Microsoft.PowerShell.Core\\Remove-Module")
105+
.AddCommand(Utils.RemoveModuleCmdletInfo)
106106
.AddParameter("Force", true)
107107
.AddParameter("ErrorAction", "SilentlyContinue")
108108
.InvokeAndClearCommands();
@@ -144,7 +144,7 @@ internal Hashtable InvokeFunction(
144144
{
145145
// If an entry point is defined, we import the script module.
146146
moduleName = Path.GetFileNameWithoutExtension(scriptPath);
147-
_pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
147+
_pwsh.AddCommand(Utils.ImportModuleCmdletInfo)
148148
.AddParameter("Name", scriptPath)
149149
.InvokeAndClearCommands();
150150

@@ -206,12 +206,19 @@ private void ResetRunspace(string moduleName)
206206
if (!string.IsNullOrEmpty(moduleName))
207207
{
208208
// If the function had an entry point, this will remove the module that was loaded
209-
_pwsh.AddCommand("Microsoft.PowerShell.Core\\Remove-Module")
209+
_pwsh.AddCommand(Utils.RemoveModuleCmdletInfo)
210210
.AddParameter("Name", moduleName)
211211
.AddParameter("Force", true)
212212
.AddParameter("ErrorAction", "SilentlyContinue")
213213
.InvokeAndClearCommands();
214214
}
215+
216+
// Clean up jobs started during the function execution.
217+
_pwsh.AddCommand(Utils.GetJobCmdletInfo)
218+
.AddCommand(Utils.RemoveJobCmdletInfo)
219+
.AddParameter("Force", true)
220+
.AddParameter("ErrorAction", "SilentlyContinue")
221+
.InvokeAndClearCommands();
215222
}
216223
}
217224
}

src/Utility/Utils.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77
using System.IO;
88
using System.Management.Automation;
99
using System.Text;
10+
using Microsoft.PowerShell.Commands;
1011

1112
namespace Microsoft.Azure.Functions.PowerShellWorker.Utility
1213
{
1314
internal class Utils
1415
{
16+
internal static CmdletInfo ImportModuleCmdletInfo = new CmdletInfo("Import-Module", typeof(ImportModuleCommand));
17+
internal static CmdletInfo RemoveModuleCmdletInfo = new CmdletInfo("Remove-Module", typeof(RemoveModuleCommand));
18+
internal static CmdletInfo GetJobCmdletInfo = new CmdletInfo("Get-Job", typeof(GetJobCommand));
19+
internal static CmdletInfo RemoveJobCmdletInfo = new CmdletInfo("Remove-Job", typeof(RemoveJobCommand));
20+
1521
/// <summary>
1622
/// Helper method to do additional transformation on the input value based on the type constraints specified in the script.
1723
/// </summary>

0 commit comments

Comments
 (0)