Skip to content

Commit 65230ea

Browse files
authored
Bring back the code that shares output binding metadata with the helper module (Azure#161)
When script from the helper module is running, it can use `[FuncMetadata]::GetOutputBindingInfo([Runspace]::DefaultRunspace.InstanceId)` to get the output binding info about the function that is currently running. Hence, `Push-OutputBinidng` can do smart things based on the `-Name` with this change.
1 parent ae05997 commit 65230ea

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

build.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ if ($Bootstrap.IsPresent) {
4242
# Clean step
4343
if($Clean.IsPresent) {
4444
Push-Location $PSScriptRoot
45-
git clean -fdx
45+
git clean -fdX
4646
Pop-Location
4747
}
4848

src/PowerShell/PowerShellManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ internal class PowerShellManager
2323
private readonly ILogger _logger;
2424
private readonly PowerShell _pwsh;
2525

26+
/// <summary>
27+
/// Gets the Runspace InstanceId.
28+
/// </summary>
29+
internal Guid InstanceId => _pwsh.Runspace.InstanceId;
30+
2631
/// <summary>
2732
/// Gets the associated logger.
2833
/// </summary>

src/PowerShell/PowerShellManagerPool.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ internal PowerShellManager CheckoutIdleWorker(StreamingMessage request, AzFuncti
9090
}
9191
}
9292

93+
// Register the function with the Runspace before returning the idle PowerShellManager.
94+
FunctionMetadata.RegisterFunctionMetadata(psManager.InstanceId, functionInfo);
9395
psManager.Logger.SetContext(requestId, invocationId);
9496
return psManager;
9597
}
@@ -101,6 +103,8 @@ internal void ReclaimUsedWorker(PowerShellManager psManager)
101103
{
102104
if (psManager != null)
103105
{
106+
// Unregister the Runspace before reclaiming the used PowerShellManager.
107+
FunctionMetadata.UnregisterFunctionMetadata(psManager.InstanceId);
104108
psManager.Logger.ResetContext();
105109
_pool.Add(psManager);
106110
}

src/Public/FunctionMetadata.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Collections.Concurrent;
8+
using System.Collections.ObjectModel;
9+
10+
namespace Microsoft.Azure.Functions.PowerShellWorker
11+
{
12+
/// <summary>
13+
/// Function metadata for the PowerShellWorker module to query.
14+
/// </summary>
15+
public static class FunctionMetadata
16+
{
17+
internal static ConcurrentDictionary<Guid, ReadOnlyDictionary<string, ReadOnlyBindingInfo>> OutputBindingCache
18+
= new ConcurrentDictionary<Guid, ReadOnlyDictionary<string, ReadOnlyBindingInfo>>();
19+
20+
/// <summary>
21+
/// Get the binding metadata for the given Runspace instance id.
22+
/// </summary>
23+
public static ReadOnlyDictionary<string, ReadOnlyBindingInfo> GetOutputBindingInfo(Guid runspaceInstanceId)
24+
{
25+
ReadOnlyDictionary<string, ReadOnlyBindingInfo> outputBindings = null;
26+
OutputBindingCache.TryGetValue(runspaceInstanceId, out outputBindings);
27+
return outputBindings;
28+
}
29+
30+
/// <summary>
31+
/// Helper method to set the output binding metadata for the function that is about to run.
32+
/// </summary>
33+
internal static void RegisterFunctionMetadata(Guid instanceId, AzFunctionInfo functionInfo)
34+
{
35+
var outputBindings = functionInfo.OutputBindings;
36+
OutputBindingCache.AddOrUpdate(instanceId, outputBindings, (key, value) => outputBindings);
37+
}
38+
39+
/// <summary>
40+
/// Helper method to clear the output binding metadata for the function that has done running.
41+
/// </summary>
42+
internal static void UnregisterFunctionMetadata(Guid instanceId)
43+
{
44+
OutputBindingCache.TryRemove(instanceId, out _);
45+
}
46+
}
47+
}

test/Unit/PowerShell/PowerShellManagerTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ public void ModulePathShouldBeSetCorrectly()
152152
Assert.Equal(expectedPath, Environment.GetEnvironmentVariable("PSModulePath"));
153153
}
154154

155+
[Fact]
156+
public void RegisterAndUnregisterFunctionMetadataShouldWork()
157+
{
158+
string path = Path.Join(TestUtils.FunctionDirectory, "testBasicFunction.ps1");
159+
var functionInfo = TestUtils.NewAzFunctionInfo(path, string.Empty);
160+
161+
Assert.Empty(FunctionMetadata.OutputBindingCache);
162+
FunctionMetadata.RegisterFunctionMetadata(_testManager.InstanceId, functionInfo);
163+
Assert.Single(FunctionMetadata.OutputBindingCache);
164+
FunctionMetadata.UnregisterFunctionMetadata(_testManager.InstanceId);
165+
Assert.Empty(FunctionMetadata.OutputBindingCache);
166+
}
167+
155168
[Fact]
156169
public void ProfileShouldWork()
157170
{

0 commit comments

Comments
 (0)