-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathDefaultDurableTaskWorkerBuilder.cs
More file actions
79 lines (67 loc) · 2.91 KB
/
Copy pathDefaultDurableTaskWorkerBuilder.cs
File metadata and controls
79 lines (67 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.DurableTask.Worker.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Microsoft.DurableTask.Worker;
/// <summary>
/// The default builder for durable task.
/// </summary>
public class DefaultDurableTaskWorkerBuilder : IDurableTaskWorkerBuilder
{
Type? buildTarget;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultDurableTaskWorkerBuilder" /> class.
/// </summary>
/// <param name="services">The service collection for this builder.</param>
/// <param name="name">The name for this builder.</param>
public DefaultDurableTaskWorkerBuilder(string? name, IServiceCollection services)
{
this.Name = name ?? Extensions.Options.Options.DefaultName;
this.Services = Check.NotNull(services);
}
/// <inheritdoc/>
public string Name { get; }
/// <inheritdoc/>
public IServiceCollection Services { get; }
/// <inheritdoc/>
public Type? BuildTarget
{
get => this.buildTarget;
set
{
if (value is not null)
{
Check.ConcreteType<DurableTaskWorker>(value);
}
this.buildTarget = value;
}
}
/// <inheritdoc/>
public IHostedService Build(IServiceProvider serviceProvider)
{
const string error = "No valid DurableTask worker target was registered. Ensure a valid worker has been"
+ " configured via 'UseBuildTarget(Type target)'. An example of a valid worker is '.UseGrpc()'.";
Verify.NotNull(this.buildTarget, error);
DurableTaskRegistry registry = serviceProvider.GetOptions<DurableTaskRegistry>(this.Name);
DurableTaskWorkerOptions workerOptions = serviceProvider.GetOptions<DurableTaskWorkerOptions>(this.Name);
ILoggerFactory? loggerFactory = serviceProvider.GetService<ILoggerFactory>();
if (loggerFactory is not null && workerOptions.Versioning is { } versioning)
{
ILogger workerLogger = Logs.CreateWorkerLogger(loggerFactory);
if (versioning.OrchestratorUnversionedFallback == DurableTaskWorkerOptions.UnversionedFallbackMode.CatchAll)
{
workerLogger.OrchestratorUnversionedFallbackEnabled(this.Name);
}
if (versioning.ActivityUnversionedFallback == DurableTaskWorkerOptions.UnversionedFallbackMode.CatchAll)
{
workerLogger.ActivityUnversionedFallbackEnabled(this.Name);
}
}
// Note: Modifying any logic in this section could introduce breaking changes.
// Do not alter the input parameter.
return (IHostedService)ActivatorUtilities.CreateInstance(
serviceProvider, this.buildTarget, this.Name, registry.BuildFactory(workerOptions, loggerFactory));
}
}