Skip to content

Commit a151f10

Browse files
authored
.NET Purview Middleware: Improve Background Job Runner Injection (#3256)
* Clean up background job dependency injection * Fix xml documentation grammar
1 parent b773830 commit a151f10

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

dotnet/src/Microsoft.Agents.AI.Purview/BackgroundJobRunner.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.Agents.AI.Purview;
1212
/// <summary>
1313
/// Service that runs jobs in background threads.
1414
/// </summary>
15-
internal sealed class BackgroundJobRunner
15+
internal sealed class BackgroundJobRunner : IBackgroundJobRunner
1616
{
1717
private readonly IChannelHandler _channelHandler;
1818
private readonly IPurviewClient _purviewClient;
@@ -70,4 +70,12 @@ private async Task RunJobAsync(BackgroundJobBase job)
7070
break;
7171
}
7272
}
73+
74+
/// <summary>
75+
/// Shutdown the job runners.
76+
/// </summary>
77+
public async Task ShutdownAsync()
78+
{
79+
await this._channelHandler.StopAndWaitForCompletionAsync().ConfigureAwait(false);
80+
}
7381
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.Threading.Tasks;
4+
5+
namespace Microsoft.Agents.AI.Purview;
6+
7+
/// <summary>
8+
/// An interface for a class that manages background jobs.
9+
/// </summary>
10+
internal interface IBackgroundJobRunner
11+
{
12+
/// <summary>
13+
/// Shutdown the background jobs.
14+
/// </summary>
15+
Task ShutdownAsync();
16+
}

dotnet/src/Microsoft.Agents.AI.Purview/PurviewExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private static PurviewWrapper CreateWrapper(TokenCredential tokenCredential, Pur
4141
services.AddSingleton<PurviewWrapper>();
4242
services.AddSingleton(Channel.CreateBounded<BackgroundJobBase>(purviewSettings.PendingBackgroundJobLimit));
4343
services.AddSingleton<IChannelHandler, ChannelHandler>();
44-
services.AddSingleton<BackgroundJobRunner>();
44+
services.AddSingleton<IBackgroundJobRunner, BackgroundJobRunner>();
4545
ServiceProvider serviceProvider = services.BuildServiceProvider();
4646

4747
return serviceProvider.GetRequiredService<PurviewWrapper>();

dotnet/src/Microsoft.Agents.AI.Purview/PurviewWrapper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ internal sealed class PurviewWrapper : IDisposable
1818
private readonly ILogger _logger;
1919
private readonly IScopedContentProcessor _scopedProcessor;
2020
private readonly PurviewSettings _purviewSettings;
21-
private readonly IChannelHandler _channelHandler;
21+
private readonly IBackgroundJobRunner _backgroundJobRunner;
2222

2323
/// <summary>
2424
/// Creates a new <see cref="PurviewWrapper"/> instance.
2525
/// </summary>
2626
/// <param name="scopedProcessor">The scoped processor used to orchestrate the calls to Purview.</param>
2727
/// <param name="purviewSettings">The settings for Purview integration.</param>
2828
/// <param name="logger">The logger used for logging.</param>
29-
/// <param name="channelHandler">The channel handler used to queue background jobs and add job runners.</param>
30-
public PurviewWrapper(IScopedContentProcessor scopedProcessor, PurviewSettings purviewSettings, ILogger logger, IChannelHandler channelHandler)
29+
/// <param name="backgroundJobRunner">The runner used to manage background jobs.</param>
30+
public PurviewWrapper(IScopedContentProcessor scopedProcessor, PurviewSettings purviewSettings, ILogger logger, IBackgroundJobRunner backgroundJobRunner)
3131
{
3232
this._scopedProcessor = scopedProcessor;
3333
this._purviewSettings = purviewSettings;
3434
this._logger = logger;
35-
this._channelHandler = channelHandler;
35+
this._backgroundJobRunner = backgroundJobRunner;
3636
}
3737

3838
private static string GetThreadIdFromAgentThread(AgentThread? thread, IEnumerable<ChatMessage> messages)
@@ -203,7 +203,7 @@ public async Task<AgentResponse> ProcessAgentContentAsync(IEnumerable<ChatMessag
203203
public void Dispose()
204204
{
205205
#pragma warning disable VSTHRD002 // Need to wait for pending jobs to complete.
206-
this._channelHandler.StopAndWaitForCompletionAsync().GetAwaiter().GetResult();
206+
this._backgroundJobRunner.ShutdownAsync().GetAwaiter().GetResult();
207207
#pragma warning restore VSTHRD002 // Need to wait for pending jobs to complete.
208208
}
209209
}

dotnet/tests/Microsoft.Agents.AI.Purview.UnitTests/PurviewWrapperTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ namespace Microsoft.Agents.AI.Purview.UnitTests;
1818
public sealed class PurviewWrapperTests : IDisposable
1919
{
2020
private readonly Mock<IScopedContentProcessor> _mockProcessor;
21-
private readonly IChannelHandler _channelHandler;
21+
private readonly IBackgroundJobRunner _backgroundJobRunner;
2222
private readonly PurviewSettings _settings;
2323
private readonly PurviewWrapper _wrapper;
2424

2525
public PurviewWrapperTests()
2626
{
2727
this._mockProcessor = new Mock<IScopedContentProcessor>();
28-
this._channelHandler = Mock.Of<IChannelHandler>();
2928
this._settings = new PurviewSettings("TestApp")
3029
{
3130
TenantId = "tenant-123",
3231
PurviewAppLocation = new PurviewAppLocation(PurviewLocationType.Application, "app-123"),
3332
BlockedPromptMessage = "Prompt blocked by policy",
3433
BlockedResponseMessage = "Response blocked by policy"
3534
};
36-
this._wrapper = new PurviewWrapper(this._mockProcessor.Object, this._settings, NullLogger.Instance, this._channelHandler);
35+
this._backgroundJobRunner = Mock.Of<IBackgroundJobRunner>();
36+
this._wrapper = new PurviewWrapper(this._mockProcessor.Object, this._settings, NullLogger.Instance, this._backgroundJobRunner);
3737
}
3838

3939
#region ProcessChatContentAsync Tests
@@ -151,7 +151,7 @@ public async Task ProcessChatContentAsync_WithIgnoreExceptions_ContinuesOnPrompt
151151
IgnoreExceptions = true,
152152
PurviewAppLocation = new PurviewAppLocation(PurviewLocationType.Application, "app-123")
153153
};
154-
var wrapper = new PurviewWrapper(this._mockProcessor.Object, settingsWithIgnore, NullLogger.Instance, this._channelHandler);
154+
var wrapper = new PurviewWrapper(this._mockProcessor.Object, settingsWithIgnore, NullLogger.Instance, this._backgroundJobRunner);
155155

156156
var messages = new List<ChatMessage>
157157
{
@@ -371,7 +371,7 @@ public async Task ProcessAgentContentAsync_WithIgnoreExceptions_ContinuesOnError
371371
IgnoreExceptions = true,
372372
PurviewAppLocation = new PurviewAppLocation(PurviewLocationType.Application, "app-123")
373373
};
374-
var wrapper = new PurviewWrapper(this._mockProcessor.Object, settingsWithIgnore, NullLogger.Instance, this._channelHandler);
374+
var wrapper = new PurviewWrapper(this._mockProcessor.Object, settingsWithIgnore, NullLogger.Instance, this._backgroundJobRunner);
375375

376376
var messages = new List<ChatMessage>
377377
{

0 commit comments

Comments
 (0)