Skip to content

Commit

Permalink
Detect host shutdown using CancellationToken. Fixes Azure#4251
Browse files Browse the repository at this point in the history
  • Loading branch information
alrod committed Jun 5, 2019
1 parent 81003ef commit 18244b3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -28,6 +29,7 @@ namespace Microsoft.Azure.WebJobs.Script.WebHost.Middleware
public class FunctionInvocationMiddleware
{
private readonly RequestDelegate _next;
private IApplicationLifetime _applicationLifetime;

public FunctionInvocationMiddleware(RequestDelegate next)
{
Expand All @@ -50,6 +52,8 @@ public async Task Invoke(HttpContext context)
await _next(context);
}

_applicationLifetime = context.RequestServices.GetService<IApplicationLifetime>();

IFunctionExecutionFeature functionExecution = context.Features.Get<IFunctionExecutionFeature>();
if (functionExecution != null && !context.Response.HasStarted())
{
Expand Down Expand Up @@ -139,8 +143,8 @@ private async Task<IActionResult> GetResultAsync(HttpContext context, IFunctionE

using (logger.BeginScope(scopeState))
{
// TODO: Flow cancellation token from caller
await functionExecution.ExecuteAsync(context.Request, CancellationToken.None);
CancellationToken cancellationToken = _applicationLifetime != null ? _applicationLifetime.ApplicationStopping : CancellationToken.None;
await functionExecution.ExecuteAsync(context.Request, cancellationToken);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ await TestHelpers.RunWithTimeoutAsync(async () =>

await AwaitHostStateAsync(ScriptHostState.Running);

// need to reinitialize TestFunctionHost to reset IApplicationLifetime
await _fixture.InitializeAsync();

// verify functions can be invoked
await InvokeAndValidateHttpTrigger(functionName);

Expand Down

0 comments on commit 18244b3

Please sign in to comment.