Skip to content

Commit a68bf2e

Browse files
Call EnableDrainModeAsync on ApplicationStopping (#7262)
Co-authored-by: Pragna Gopa <[email protected]>
1 parent a90271a commit a68bf2e

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

release_notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
-->
55
- Added a feature flag to opt out of the default behavior where the host sets the environment name to `Development` when running in debug mode. To disable the behavior, set the app setting: `AzureWebJobsFeatureFlags` to `DisableDevModeInDebug`
66
- Reorder CORS and EasyAuth middleware to prevent EasyAuth from blocking CORS requests (#7315)
7-
87
- Updated PowerShell Worker (PS7) to [3.0.833](https://github.com/Azure/azure-functions-powershell-worker/releases/tag/v3.0.833)
8+
- Call EnableDrainModeAsync on ApplicationStopping (#7262)
99

1010
**Release sprint:** Sprint 100
1111
[ [bugs](https://github.com/Azure/azure-functions-host/issues?q=is%3Aissue+milestone%3A%22Functions+Sprint+100%22+label%3Abug+is%3Aclosed) | [features](https://github.com/Azure/azure-functions-host/issues?q=is%3Aissue+milestone%3A%22Functions+Sprint+100%22+label%3Afeature+is%3Aclosed) ]

src/WebJobs.Script.WebHost/WebJobsScriptHostService.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
1212
using Microsoft.ApplicationInsights.Extensibility;
1313
using Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId;
14+
using Microsoft.Azure.WebJobs.Host;
1415
using Microsoft.Azure.WebJobs.Logging;
1516
using Microsoft.Azure.WebJobs.Script.Configuration;
1617
using Microsoft.Azure.WebJobs.Script.Diagnostics;
@@ -693,6 +694,17 @@ private void RegisterApplicationLifetimeEvents()
693694
_applicationLifetime.ApplicationStopping.Register(() =>
694695
{
695696
Interlocked.Exchange(ref _applicationStopping, 1);
697+
if (_environment.DrainOnApplicationStoppingEnabled())
698+
{
699+
var drainModeManager = _host?.Services.GetService<IDrainModeManager>();
700+
if (drainModeManager != null)
701+
{
702+
_logger.LogDebug("Application Stopping: initiate drain mode");
703+
drainModeManager.EnableDrainModeAsync(CancellationToken.None);
704+
// Workaround until https://github.com/Azure/azure-functions-host/issues/7188 is addressed.
705+
Thread.Sleep(TimeSpan.FromMinutes(10));
706+
}
707+
}
696708
});
697709

698710
_applicationLifetime.ApplicationStopped.Register(() =>

src/WebJobs.Script/Environment/EnvironmentExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,5 +447,11 @@ public static bool SupportsAzureFileShareMount(this IEnvironment environment)
447447
return string.Equals(environment.GetFunctionsWorkerRuntime(), RpcWorkerConstants.PowerShellLanguageWorkerName,
448448
StringComparison.OrdinalIgnoreCase);
449449
}
450+
451+
public static bool DrainOnApplicationStoppingEnabled(this IEnvironment environment)
452+
{
453+
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(KubernetesServiceHost)) ||
454+
(bool.TryParse(environment.GetEnvironmentVariable(DrainOnApplicationStopping), out bool v) && v);
455+
}
450456
}
451457
}

src/WebJobs.Script/Environment/EnvironmentSettingNames.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,7 @@ public static class EnvironmentSettingNames
120120
public const string AntaresComputerName = "COMPUTERNAME";
121121

122122
public const string AppKind = "APP_KIND";
123+
124+
public const string DrainOnApplicationStopping = "FUNCTIONS_ENABLE_DRAIN_ON_APP_STOPPING";
123125
}
124126
}

test/WebJobs.Script.Tests/Extensions/EnvironmentExtensionsTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,5 +219,23 @@ public void Returns_SupportsAzureFileShareMount(string workerRuntime, bool useLo
219219
environment.SetEnvironmentVariable(FunctionWorkerRuntime, workerRuntime);
220220
Assert.Equal(supportsAzureFileShareMount, environment.SupportsAzureFileShareMount());
221221
}
222+
223+
[Theory]
224+
[InlineData(null, null, false)]
225+
[InlineData("", null, false)]
226+
[InlineData("", "", false)]
227+
[InlineData("", "false", false)]
228+
[InlineData("", "true", true)]
229+
[InlineData("10.0.0.1", "", true)]
230+
[InlineData("10.0.0.1", "true", true)]
231+
[InlineData("10.0.0.1", "false", true)]
232+
[InlineData("10.0.0.1", null, true)]
233+
public void IsDrainOnApplicationStopping_ReturnsExpectedResult(string serviceHostValue, string drainOnStoppingValue, bool expected)
234+
{
235+
var environment = new TestEnvironment();
236+
environment.SetEnvironmentVariable(KubernetesServiceHost, serviceHostValue);
237+
environment.SetEnvironmentVariable(DrainOnApplicationStopping, drainOnStoppingValue);
238+
Assert.Equal(expected, environment.DrainOnApplicationStoppingEnabled());
239+
}
222240
}
223241
}

0 commit comments

Comments
 (0)