Skip to content

Commit 6eea6da

Browse files
brettsamkshyju
andauthored
First pieces of native placeholder support (#9145)
Co-authored-by: Shyju Krishnankutty <[email protected]>
1 parent 6b3e194 commit 6eea6da

17 files changed

+92
-37
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,11 @@ internal FunctionEnvironmentReloadRequest GetFunctionEnvironmentReloadRequest(ID
589589
request.EnvironmentVariables.Add(entry.Key.ToString(), entry.Value.ToString());
590590
}
591591
}
592-
request.EnvironmentVariables.Add(WorkerConstants.FunctionsWorkerDirectorySettingName, _workerConfig.Description.WorkerDirectory);
593-
request.FunctionAppDirectory = _applicationHostOptions.CurrentValue.ScriptPath;
592+
593+
string scriptRoot = _applicationHostOptions.CurrentValue.ScriptPath;
594+
request.EnvironmentVariables.TryAdd(WorkerConstants.FunctionsWorkerDirectorySettingName, _workerConfig.Description.WorkerDirectory);
595+
request.EnvironmentVariables.TryAdd(WorkerConstants.FunctionsApplicationDirectorySettingName, scriptRoot);
596+
request.FunctionAppDirectory = scriptRoot;
594597

595598
return request;
596599
}

src/WebJobs.Script/Environment/EnvironmentExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public static bool IsPlaceholderModeEnabled(this IEnvironment environment)
3636
return environment.GetEnvironmentVariable(AzureWebsitePlaceholderMode) == "1";
3737
}
3838

39+
public static bool UsePlaceholderDotNetIsolated(this IEnvironment environment)
40+
{
41+
return environment.GetEnvironmentVariable(AzureWebsiteUsePlaceholderDotNetIsolated) == "1";
42+
}
43+
3944
public static bool IsLegacyPlaceholderTemplateSite(this IEnvironment environment)
4045
{
4146
string siteName = environment.GetEnvironmentVariable(AzureWebsiteName);

src/WebJobs.Script/Environment/EnvironmentSettingNames.cs

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

4-
using System.Dynamic;
5-
64
namespace Microsoft.Azure.WebJobs.Script
75
{
86
public static class EnvironmentSettingNames
@@ -15,6 +13,7 @@ public static class EnvironmentSettingNames
1513
public const string AzureWebsiteSku = "WEBSITE_SKU";
1614
public const string RemoteDebuggingPort = "REMOTEDEBUGGINGPORT";
1715
public const string AzureWebsitePlaceholderMode = "WEBSITE_PLACEHOLDER_MODE";
16+
public const string AzureWebsiteUsePlaceholderDotNetIsolated = "WEBSITE_USE_PLACEHOLDER_DOTNETISOLATED";
1817
public const string InitializedFromPlaceholder = "INITIALIZED_FROM_PLACEHOLDER";
1918
public const string AzureWebsiteHomePath = "HOME";
2019
public const string AzureWebJobsScriptRoot = "AzureWebJobsScriptRoot";
@@ -123,7 +122,7 @@ public static class EnvironmentSettingNames
123122
public const string CorsAllowedOrigins = "CORS_ALLOWED_ORIGINS";
124123
public const string CorsSupportCredentials = "CORS_SUPPORT_CREDENTIALS";
125124

126-
// EasyAuth settings
125+
// EasyAuth settings
127126
public const string EasyAuthClientId = "WEBSITE_AUTH_CLIENT_ID";
128127
public const string EasyAuthSigningKey = "WEBSITE_AUTH_SIGNING_KEY";
129128

src/WebJobs.Script/Host/FunctionMetadataManager.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Microsoft.Azure.WebJobs.Script.Workers.Http;
1515
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
1616
using Microsoft.Extensions.DependencyInjection;
17-
using Microsoft.Extensions.Hosting;
1817
using Microsoft.Extensions.Logging;
1918
using Microsoft.Extensions.Options;
2019

@@ -51,9 +50,12 @@ public FunctionMetadataManager(IOptions<ScriptJobHostOptions> scriptOptions, IFu
5150

5251
// Every time script host is re-intializing, we also need to re-initialize
5352
// services that change with the scope of the script host.
54-
scriptHostManager.HostInitializing += (s, e) =>
53+
scriptHostManager.ActiveHostChanged += (s, e) =>
5554
{
56-
InitializeServices();
55+
if (e.NewHost is not null)
56+
{
57+
InitializeServices();
58+
}
5759
};
5860
}
5961

src/WebJobs.Script/WebJobs.Script.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="..\..\build\common.props" />
33
<PropertyGroup>
44
<PackageId>Microsoft.Azure.WebJobs.Script</PackageId>
@@ -47,6 +47,7 @@
4747
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.21.0" />
4848
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer" Version="2.21.0" />
4949
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" Version="2.21.0" />
50+
<PackageReference Include="Microsoft.Azure.Functions.DotNetIsolatedNativeHost" Version="1.0.0-preview6" />
5051
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.36" />
5152
<PackageReference Include="Microsoft.Azure.WebJobs.Host.Storage" Version="5.0.0-beta.2-11957" />
5253
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.1.1" />

src/WebJobs.Script/Workers/ProcessManagement/DefaultWorkerProcessFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public virtual Process CreateWorkerProcess(WorkerContext context)
5757
foreach (var envVar in processEnvVariables)
5858
{
5959
startInfo.EnvironmentVariables[envVar.Key] = envVar.Value;
60+
startInfo.FileName = startInfo.FileName.Replace($"%{envVar.Key}%", envVar.Value);
6061
startInfo.Arguments = startInfo.Arguments.Replace($"%{envVar.Key}%", envVar.Value);
6162
}
6263
startInfo.Arguments = SanitizeExpandedArgument(startInfo.Arguments);

src/WebJobs.Script/Workers/ProcessManagement/WorkerDescription.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@ public abstract class WorkerDescription
1919
public string DefaultExecutablePath { get; set; }
2020

2121
/// <summary>
22-
/// Gets or sets the default path to the worker
22+
/// Gets or sets the default path to the worker.
2323
/// </summary>
2424
public string DefaultWorkerPath { get; set; }
2525

2626
/// <summary>
27-
/// Gets or sets the default base directory for the worker
27+
/// Gets or sets the default base directory for the worker.
2828
/// </summary>
2929
public string WorkerDirectory { get; set; }
3030

3131
/// <summary>
32-
/// Gets or sets the command line args to pass to the worker. Will be appended after DefaultExecutablePath but before DefaultWorkerPath
32+
/// Gets or sets the command line args to pass to the worker. Will be appended after DefaultExecutablePath but before DefaultWorkerPath.
3333
/// </summary>
3434
public IList<string> Arguments { get; set; }
3535

3636
/// <summary>
37-
/// Gets or sets the command line args to pass to the worker. Will be appended after DefaultWorkerPath
37+
/// Gets or sets the command line args to pass to the worker. Will be appended after DefaultWorkerPath.
3838
/// </summary>
3939
public IList<string> WorkerArguments { get; set; }
4040

4141
/// <summary>
4242
/// Gets or sets a value indicating whether to use stderror stream for all console logs. Default false.
43-
/// If set to false, Logs from stderr are written with info level by default. If Message contains keywords: error, servere then logs are written with error level.
44-
/// If set to true, Logs from stdout stream will be written with info level and Logs from stderr steam will be written with error level
43+
/// If set to false, Logs from stderr are written with info level by default. If Message contains keywords: error, severe then logs are written with error level.
44+
/// If set to true, Logs from stdout stream will be written with info level and Logs from stderr steam will be written with error level.
4545
/// </summary>
4646
public abstract bool UseStdErrorStreamForErrorsOnly { get; set; }
4747

src/WebJobs.Script/Workers/Rpc/RpcWorkerProcess.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ internal override Process CreateWorkerProcess()
5555
{
5656
var workerContext = new RpcWorkerContext(Guid.NewGuid().ToString(), RpcWorkerConstants.DefaultMaxMessageLengthBytes, _workerId, _workerProcessArguments, _scriptRootPath, _serverUri);
5757
workerContext.EnvironmentVariables.Add(WorkerConstants.FunctionsWorkerDirectorySettingName, _workerDirectory);
58+
workerContext.EnvironmentVariables.Add(WorkerConstants.FunctionsApplicationDirectorySettingName, _scriptRootPath);
5859
foreach (var pair in _hostingConfigOptions.Value.Features)
5960
{
6061
workerContext.EnvironmentVariables[pair.Key] = pair.Value;

src/WebJobs.Script/Workers/Rpc/WebHostRpcWorkerChannelManager.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public async Task SpecializeAsync()
124124

125125
if (_workerRuntime != null && rpcWorkerChannel != null)
126126
{
127-
if (UsePlaceholderChannel(_workerRuntime))
127+
if (UsePlaceholderChannel(rpcWorkerChannel))
128128
{
129129
_logger.LogDebug("Loading environment variables for runtime: {runtime}", _workerRuntime);
130130
await rpcWorkerChannel.SendFunctionEnvironmentReloadRequest();
@@ -156,8 +156,10 @@ public async Task WorkerWarmupAsync()
156156
}
157157
}
158158

159-
private bool UsePlaceholderChannel(string workerRuntime)
159+
private bool UsePlaceholderChannel(IRpcWorkerChannel channel)
160160
{
161+
string workerRuntime = channel?.WorkerConfig?.Description?.Language;
162+
161163
if (string.IsNullOrEmpty(workerRuntime))
162164
{
163165
return false;
@@ -170,6 +172,26 @@ private bool UsePlaceholderChannel(string workerRuntime)
170172
return false;
171173
}
172174

175+
if (workerRuntime.Equals(RpcWorkerConstants.DotNetIsolatedLanguageWorkerName, StringComparison.OrdinalIgnoreCase))
176+
{
177+
bool placeholderEnabled = _environment.UsePlaceholderDotNetIsolated();
178+
_logger.LogDebug("UsePlaceholderDotNetIsolated: {placeholderEnabled}", placeholderEnabled);
179+
180+
if (!placeholderEnabled)
181+
{
182+
return false;
183+
}
184+
185+
// Do not specialize if the placeholder is 6.0 but the site is 7.0 (for example).
186+
var currentWorkerRuntimeVersion = _environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeVersionSettingName);
187+
channel.WorkerProcess.Process.StartInfo.Environment.TryGetValue(RpcWorkerConstants.FunctionWorkerRuntimeVersionSettingName, out string placeholderWorkerRuntimeVersion);
188+
bool versionMatches = string.Equals(currentWorkerRuntimeVersion, placeholderWorkerRuntimeVersion, StringComparison.OrdinalIgnoreCase);
189+
_logger.LogDebug("Placeholder runtime version: '{placeholderWorkerRuntimeVersion}'. Site runtime version: '{currentWorkerRuntimeVersion}'. Match: {versionMatches}",
190+
placeholderWorkerRuntimeVersion, currentWorkerRuntimeVersion, versionMatches);
191+
192+
return versionMatches;
193+
}
194+
173195
// Special case: node and PowerShell apps must be read-only to use the placeholder mode channel
174196
// Also cannot use placeholder worker that is targeting ~3 but has backwards compatibility with V2 enabled
175197
// TODO: Remove special casing when resolving https://github.com/Azure/azure-functions-host/issues/4534

src/WebJobs.Script/Workers/WorkerConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static class WorkerConstants
1919

2020
// Environment variables names
2121
public const string FunctionsWorkerDirectorySettingName = "FUNCTIONS_WORKER_DIRECTORY";
22+
public const string FunctionsApplicationDirectorySettingName = "FUNCTIONS_APPLICATION_DIRECTORY";
2223

2324
// Worker description constants
2425
public const string WorkerDescriptionDefaultExecutablePath = "defaultExecutablePath";

0 commit comments

Comments
 (0)