Skip to content

Commit 90a2950

Browse files
authored
Decoupling worker behavior (#11479)
1 parent 7a8b864 commit 90a2950

File tree

139 files changed

+930
-483
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+930
-483
lines changed

WebJobs.Script.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
4-
VisualStudioVersion = 17.0.31717.71
4+
VisualStudioVersion = 17.14.36623.8 d17.14
55
MinimumVisualStudioVersion = 15.0.0.0
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{16351B76-87CA-4A8C-80A1-3DD83A0C4AA6}"
77
EndProject

src/WebJobs.Script.Grpc/Channel/GrpcWorkerChannelFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
@@ -17,7 +17,7 @@
1717

1818
namespace Microsoft.Azure.WebJobs.Script.Grpc
1919
{
20-
public class GrpcWorkerChannelFactory : IRpcWorkerChannelFactory
20+
internal class GrpcWorkerChannelFactory : IRpcWorkerChannelFactory
2121
{
2222
private readonly ILoggerFactory _loggerFactory = null;
2323
private readonly IRpcWorkerProcessFactory _rpcWorkerProcessFactory = null;

src/WebJobs.Script.Grpc/Eventing/GrpcEvent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using Microsoft.Azure.WebJobs.Script.Eventing;
55
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
66

77
namespace Microsoft.Azure.WebJobs.Script.Grpc.Eventing
88
{
9-
public class GrpcEvent : ScriptEvent
9+
internal class GrpcEvent : ScriptEvent
1010
{
1111
internal GrpcEvent(string workerId, StreamingMessage message, MessageOrigin origin = MessageOrigin.Host)
1212
: base(message.ContentCase.ToString(), EventSources.Rpc)

src/WebJobs.Script.Grpc/Eventing/InboundGrpcEvent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
55

66
namespace Microsoft.Azure.WebJobs.Script.Grpc.Eventing
77
{
8-
public class InboundGrpcEvent : GrpcEvent
8+
internal class InboundGrpcEvent : GrpcEvent
99
{
1010
public InboundGrpcEvent(string workerId, StreamingMessage message) : base(workerId, message, MessageOrigin.Worker)
1111
{

src/WebJobs.Script.Grpc/Eventing/OutboundGrpcEvent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
55

66
namespace Microsoft.Azure.WebJobs.Script.Grpc.Eventing
77
{
8-
public class OutboundGrpcEvent : GrpcEvent
8+
internal class OutboundGrpcEvent : GrpcEvent
99
{
1010
public OutboundGrpcEvent(string workerId, StreamingMessage message) : base(workerId, message, MessageOrigin.Host)
1111
{

src/WebJobs.Script/Workers/FunctionInvocationDispatcherFactory.cs renamed to src/WebJobs.Script.Grpc/FunctionInvocationDispatcherFactory.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
@@ -15,7 +15,7 @@
1515

1616
namespace Microsoft.Azure.WebJobs.Script.Workers
1717
{
18-
internal class FunctionInvocationDispatcherFactory : IFunctionInvocationDispatcherFactory
18+
internal class FunctionInvocationDispatcherFactory : IFunctionInvocationDispatcherFactory, IDisposable
1919
{
2020
private readonly IFunctionInvocationDispatcher _functionDispatcher;
2121

@@ -65,5 +65,11 @@ public FunctionInvocationDispatcherFactory(IOptions<ScriptJobHostOptions> script
6565
}
6666

6767
public IFunctionInvocationDispatcher GetFunctionDispatcher() => _functionDispatcher;
68+
69+
public void Dispose()
70+
{
71+
// This is a "provider" rather than a "factory". We are responsible for disposing.
72+
_functionDispatcher.Dispose();
73+
}
6874
}
6975
}

src/WebJobs.Script/Workers/FunctionInvocationDispatcherShutdownManager.cs renamed to src/WebJobs.Script.Grpc/FunctionInvocationDispatcherShutdownManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System.Threading;

src/WebJobs.Script/Workers/FunctionInvocationDispatcherState.cs renamed to src/WebJobs.Script.Grpc/FunctionInvocationDispatcherState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
namespace Microsoft.Azure.WebJobs.Script.Workers
55
{
6-
public enum FunctionInvocationDispatcherState
6+
internal enum FunctionInvocationDispatcherState
77
{
88
/// <summary>
99
/// The FunctionDispatcher has not yet been created
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Azure.WebJobs.Script.Workers.Http;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Options;
8+
9+
namespace Microsoft.Azure.WebJobs.Script.Rpc.Hosting
10+
{
11+
internal sealed class HttpScriptHostRecycleOptionsSetup : IPostConfigureOptions<ScriptHostRecycleOptions>
12+
{
13+
private readonly IOptions<HttpWorkerOptions> _httpWorkerOptions;
14+
private readonly IConfiguration _configuration;
15+
16+
public HttpScriptHostRecycleOptionsSetup(
17+
IOptions<HttpWorkerOptions> httpWorkerOptions, IConfiguration configuration)
18+
{
19+
_httpWorkerOptions = httpWorkerOptions ?? throw new ArgumentNullException(nameof(httpWorkerOptions));
20+
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
21+
}
22+
23+
public void PostConfigure(string name, ScriptHostRecycleOptions options)
24+
{
25+
// Enforcing sequential host restarts when a user-specified custom handler port is configured to prevent multiple processes from attempting to bind to the same port concurrently.
26+
if (_httpWorkerOptions.Value.IsPortManuallySet)
27+
{
28+
options.SequentialHostRestartRequired = true;
29+
}
30+
}
31+
}
32+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using Microsoft.Azure.WebJobs.Host.Scale;
7+
using Microsoft.Azure.WebJobs.Script;
8+
using Microsoft.Azure.WebJobs.Script.Config;
9+
using Microsoft.Azure.WebJobs.Script.Description;
10+
using Microsoft.Azure.WebJobs.Script.Host;
11+
using Microsoft.Azure.WebJobs.Script.Rpc;
12+
using Microsoft.Azure.WebJobs.Script.Rpc.Configuration;
13+
using Microsoft.Azure.WebJobs.Script.Rpc.Hosting;
14+
using Microsoft.Azure.WebJobs.Script.Scale;
15+
using Microsoft.Azure.WebJobs.Script.WebHost;
16+
using Microsoft.Azure.WebJobs.Script.Workers;
17+
using Microsoft.Azure.WebJobs.Script.Workers.Http;
18+
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
19+
using Microsoft.Extensions.DependencyInjection.Extensions;
20+
using Microsoft.Extensions.Hosting;
21+
using Microsoft.Extensions.Options;
22+
23+
namespace Microsoft.Extensions.DependencyInjection;
24+
25+
public static class RpcServiceCollectionExtensions
26+
{
27+
public static IServiceCollection AddRpcScriptHostServices(this IServiceCollection services)
28+
{
29+
ArgumentNullException.ThrowIfNull(services);
30+
31+
// HTTP Worker
32+
services.AddSingleton<IHttpWorkerProcessFactory, HttpWorkerProcessFactory>();
33+
services.AddSingleton<IHttpWorkerChannelFactory, HttpWorkerChannelFactory>();
34+
services.AddSingleton<IHttpWorkerService, DefaultHttpWorkerService>();
35+
services.TryAddEnumerable(ServiceDescriptor.Singleton<IFunctionProvider, HttpWorkerFunctionProvider>());
36+
37+
//Worker Function Invocation dispatcher
38+
services.AddSingleton<IFunctionInvocationDispatcherFactory, FunctionInvocationDispatcherFactory>();
39+
40+
// Rpc Worker
41+
services.AddSingleton<IJobHostRpcWorkerChannelManager, JobHostRpcWorkerChannelManager>();
42+
services.AddSingleton<IRpcFunctionInvocationDispatcherLoadBalancer, RpcFunctionInvocationDispatcherLoadBalancer>();
43+
44+
services.AddSingleton<IHostedService, WorkerConcurrencyManager>();
45+
46+
// Configuration
47+
services.AddSingleton<IPostConfigureOptions<ScriptHostRecycleOptions>, HttpScriptHostRecycleOptionsSetup>();
48+
services.ConfigureOptions<HttpWorkerOptionsSetup>();
49+
services.ConfigureOptions<RpcFunctionMetadataOptionsSetup>();
50+
services.ConfigureOptions<RpcScriptHostRecycleOptionsSetup>();
51+
52+
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, FunctionInvocationDispatcherShutdownManager>());
53+
54+
services.AddSingleton<IWorkerFunctionDescriptorProviderFactory, RpcWorkerFunctionDescriptorProviderFactory>();
55+
services.AddSingleton<IScriptHostLifecycleService, RpcScriptHostLifecycleService>();
56+
services.AddSingleton<IScriptHostWorkerManager, RpcScriptHostWorkerManager>();
57+
58+
return services;
59+
}
60+
61+
public static IServiceCollection AddCommonRpcServices(this IServiceCollection services)
62+
{
63+
ArgumentNullException.ThrowIfNull(services);
64+
65+
// Add Language Worker Service
66+
services.AddSingleton<IRpcWorkerProcessFactory, RpcWorkerProcessFactory>();
67+
68+
services.AddSingleton<IWorkerProcessFactory, DefaultWorkerProcessFactory>();
69+
services.TryAddSingleton<IWebHostRpcWorkerChannelManager, WebHostRpcWorkerChannelManager>();
70+
services.TryAddSingleton<IWorkerFunctionMetadataProvider, WorkerFunctionMetadataProvider>();
71+
services.AddSingleton<IScriptHostWorkerManager, RpcScriptHostWorkerManager>();
72+
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConcurrencyThrottleProvider, WorkerChannelThrottleProvider>());
73+
services.AddSingleton<IWebHostWorkerManager, RpcWebHostWorkerManager>();
74+
75+
services.AddManagedHostedService<RpcInitializationService>();
76+
77+
AddProcessRegistry(services);
78+
79+
return services;
80+
}
81+
82+
private static void AddProcessRegistry(IServiceCollection services)
83+
{
84+
// W3WP already manages job objects
85+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
86+
&& !ScriptSettingsManager.Instance.IsAppServiceEnvironment)
87+
{
88+
services.AddSingleton<IProcessRegistry, JobObjectRegistry>();
89+
}
90+
else
91+
{
92+
services.AddSingleton<IProcessRegistry, EmptyProcessRegistry>();
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)