Skip to content

Commit c916b4d

Browse files
committed
Remove PostPluginConfiguration and add an internal plugin for versioning
and tuning enforcement
1 parent e41a3aa commit c916b4d

5 files changed

Lines changed: 104 additions & 37 deletions

File tree

src/Temporalio.Extensions.Aws.Lambda/TemporalLambdaWorker.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,15 @@ private static LambdaWorkerHandlerState PrepareHandlerState(
198198
"WorkerOptions.TaskQueue must be set or TEMPORAL_TASK_QUEUE must be present");
199199
}
200200

201-
var postPluginConfiguration = config.WorkerOptions.PostPluginConfiguration;
202-
config.WorkerOptions.PostPluginConfiguration = options =>
203-
{
204-
postPluginConfiguration?.Invoke(options);
205-
ApplyDeploymentVersion(options, version);
206-
ClearConcurrencyLimitsIfTunerSet(options);
207-
};
208-
config.WorkerOptions.ApplyPostPluginConfiguration();
201+
AppendWorkerPlugin(
202+
config.WorkerOptions,
203+
new InternalWorkerPlugin(
204+
"Temporalio.Extensions.Aws.Lambda",
205+
options =>
206+
{
207+
ApplyDeploymentVersion(options, version);
208+
ClearConcurrencyLimitsIfTunerSet(options);
209+
}));
209210

210211
return new LambdaWorkerHandlerState(
211212
(TemporalClientConnectOptions)config.ClientOptions.Clone(),
@@ -215,6 +216,19 @@ private static LambdaWorkerHandlerState PrepareHandlerState(
215216
handlerOptions);
216217
}
217218

219+
private static void AppendWorkerPlugin(
220+
TemporalWorkerOptions workerOptions,
221+
ITemporalWorkerPlugin plugin)
222+
{
223+
var plugins = new List<ITemporalWorkerPlugin>();
224+
if (workerOptions.Plugins != null)
225+
{
226+
plugins.AddRange(workerOptions.Plugins);
227+
}
228+
plugins.Add(plugin);
229+
workerOptions.Plugins = plugins;
230+
}
231+
218232
private static void ApplyDeploymentVersion(
219233
TemporalWorkerOptions workerOptions,
220234
WorkerDeploymentVersion version)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace Temporalio.Worker
7+
{
8+
/// <summary>
9+
/// Internal pass-through plugin for extensions that need worker plugin ordering.
10+
/// </summary>
11+
internal sealed class InternalWorkerPlugin : ITemporalWorkerPlugin
12+
{
13+
private readonly Action<TemporalWorkerOptions> configureWorker;
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="InternalWorkerPlugin"/> class.
17+
/// </summary>
18+
/// <param name="name">Plugin name reported to the worker.</param>
19+
/// <param name="configureWorker">Worker options configuration callback.</param>
20+
public InternalWorkerPlugin(string name, Action<TemporalWorkerOptions> configureWorker)
21+
{
22+
Name = name ?? throw new ArgumentNullException(nameof(name));
23+
this.configureWorker = configureWorker ??
24+
throw new ArgumentNullException(nameof(configureWorker));
25+
}
26+
27+
/// <inheritdoc />
28+
public string Name { get; }
29+
30+
/// <inheritdoc />
31+
public void ConfigureWorker(TemporalWorkerOptions options) => configureWorker(options);
32+
33+
/// <inheritdoc />
34+
public Task<TResult> RunWorkerAsync<TResult>(
35+
TemporalWorker worker,
36+
Func<TemporalWorker, CancellationToken, Task<TResult>> continuation,
37+
CancellationToken stoppingToken) =>
38+
continuation(worker, stoppingToken);
39+
40+
/// <inheritdoc />
41+
public void ConfigureReplayer(WorkflowReplayerOptions options)
42+
{
43+
_ = options;
44+
}
45+
46+
/// <inheritdoc />
47+
public Task<IEnumerable<WorkflowReplayResult>> ReplayWorkflowsAsync(
48+
WorkflowReplayer replayer,
49+
Func<WorkflowReplayer, CancellationToken, Task<IEnumerable<WorkflowReplayResult>>> continuation,
50+
CancellationToken cancellationToken) =>
51+
continuation(replayer, cancellationToken);
52+
53+
#if NETCOREAPP3_0_OR_GREATER
54+
/// <inheritdoc />
55+
public IAsyncEnumerable<WorkflowReplayResult> ReplayWorkflowsAsync(
56+
WorkflowReplayer replayer,
57+
Func<WorkflowReplayer, IAsyncEnumerable<WorkflowReplayResult>> continuation,
58+
CancellationToken cancellationToken)
59+
{
60+
_ = cancellationToken;
61+
return continuation(replayer);
62+
}
63+
#endif
64+
}
65+
}

src/Temporalio/Worker/TemporalWorker.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public TemporalWorker(IWorkerClient client, TemporalWorkerOptions options)
5757
{
5858
plugin.ConfigureWorker(Options);
5959
}
60-
Options.ApplyPostPluginConfiguration();
6160

6261
// Ensure later accesses use the modified version of options.
6362
options = Options;

src/Temporalio/Worker/TemporalWorkerOptions.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,6 @@ public TemporalWorkerOptions()
376376
internal Func<WorkflowInstanceDetails, IWorkflowInstance> WorkflowInstanceFactory { get; set; } =
377377
DefaultWorkflowInstanceFactory;
378378

379-
/// <summary>
380-
/// Gets or sets a function to run after worker plugins configure options.
381-
/// </summary>
382-
/// <remarks>
383-
/// Don't expose this until there's a use case.
384-
/// </remarks>
385-
internal Action<TemporalWorkerOptions>? PostPluginConfiguration { get; set; }
386-
387379
/// <summary>
388380
/// Add the given delegate with <see cref="ActivityAttribute" /> as an activity. This is
389381
/// usually a method reference.
@@ -525,10 +517,5 @@ internal void OnTaskCompleted(WorkflowInstance instance, Exception? failureExcep
525517
handler(instance, new(instance, failureException));
526518
}
527519
}
528-
529-
/// <summary>
530-
/// Run post-plugin configuration.
531-
/// </summary>
532-
internal void ApplyPostPluginConfiguration() => PostPluginConfiguration?.Invoke(this);
533520
}
534521
}

tests/Temporalio.Tests/Extensions/Aws/Lambda/TemporalLambdaWorkerTests.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public async Task CreateHandler_DefaultsAreAppliedAndUserOverridesWin()
8080
},
8181
CreateWorker = (_, options) =>
8282
{
83-
capturedWorkerOptions = options;
83+
capturedWorkerOptions = SimulateWorkerPluginConfiguration(options);
8484
return new FakeLambdaWorker(_ => Task.CompletedTask);
8585
},
8686
});
@@ -143,7 +143,7 @@ public async Task CreateHandler_DefaultsVersioningBehaviorToAutoUpgrade()
143143
ConnectClientAsync = _ => Task.FromResult<object>(new object()),
144144
CreateWorker = (_, options) =>
145145
{
146-
capturedWorkerOptions = options;
146+
capturedWorkerOptions = SimulateWorkerPluginConfiguration(options);
147147
return new FakeLambdaWorker(_ => Task.CompletedTask);
148148
},
149149
});
@@ -253,7 +253,7 @@ public async Task CreateHandler_ClearsConcurrencyDefaultsWhenTunerSet()
253253
ConnectClientAsync = _ => Task.FromResult<object>(new object()),
254254
CreateWorker = (_, options) =>
255255
{
256-
capturedWorkerOptions = options;
256+
capturedWorkerOptions = SimulateWorkerPluginConfiguration(options);
257257
return new FakeLambdaWorker(_ => Task.CompletedTask);
258258
},
259259
});
@@ -290,12 +290,7 @@ public async Task CreateHandler_ClearsConcurrencyDefaultsWhenPluginSetsTuner()
290290
ConnectClientAsync = _ => Task.FromResult<object>(new object()),
291291
CreateWorker = (_, options) =>
292292
{
293-
foreach (var plugin in options.Plugins ?? Array.Empty<ITemporalWorkerPlugin>())
294-
{
295-
plugin.ConfigureWorker(options);
296-
}
297-
options.ApplyPostPluginConfiguration();
298-
capturedWorkerOptions = options;
293+
capturedWorkerOptions = SimulateWorkerPluginConfiguration(options);
299294
return new FakeLambdaWorker(_ => Task.CompletedTask);
300295
},
301296
});
@@ -327,12 +322,7 @@ public async Task CreateHandler_ReappliesDeploymentVersionAfterPlugins()
327322
ConnectClientAsync = _ => Task.FromResult<object>(new object()),
328323
CreateWorker = (_, options) =>
329324
{
330-
foreach (var plugin in options.Plugins ?? Array.Empty<ITemporalWorkerPlugin>())
331-
{
332-
plugin.ConfigureWorker(options);
333-
}
334-
options.ApplyPostPluginConfiguration();
335-
capturedWorkerOptions = options;
325+
capturedWorkerOptions = SimulateWorkerPluginConfiguration(options);
336326
return new FakeLambdaWorker(_ => Task.CompletedTask);
337327
},
338328
});
@@ -991,6 +981,18 @@ private static ActivityDefinition DummyActivity() =>
991981
0,
992982
_ => Task.CompletedTask);
993983

984+
private static TemporalWorkerOptions SimulateWorkerPluginConfiguration(
985+
TemporalWorkerOptions options)
986+
{
987+
var plugins = new List<ITemporalWorkerPlugin>(
988+
options.Plugins ?? Array.Empty<ITemporalWorkerPlugin>());
989+
foreach (var plugin in plugins)
990+
{
991+
plugin.ConfigureWorker(options);
992+
}
993+
return options;
994+
}
995+
994996
[Workflow]
995997
public sealed class WorkflowWithoutVersioningBehavior
996998
{

0 commit comments

Comments
 (0)