Skip to content

Commit

Permalink
Function Filter changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewc committed Aug 11, 2017
1 parent a11ef41 commit 7617c92
Show file tree
Hide file tree
Showing 36 changed files with 576 additions and 1,035 deletions.

This file was deleted.

150 changes: 82 additions & 68 deletions src/Microsoft.Azure.WebJobs.Host/Executors/FunctionExecutor.cs

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/Microsoft.Azure.WebJobs.Host/Executors/FunctionInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class FunctionInstance : IFunctionInstance
private readonly IBindingSource _bindingSource;
private readonly IFunctionInvoker _invoker;
private readonly FunctionDescriptor _functionDescriptor;
private object _jobInstance;

public FunctionInstance(Guid id, Guid? parentId, ExecutionReason reason, IBindingSource bindingSource,
IFunctionInvoker invoker, FunctionDescriptor functionDescriptor)
Expand Down Expand Up @@ -51,6 +52,18 @@ public IFunctionInvoker Invoker
get { return _invoker; }
}

public object JobInstance
{
get
{
if (_jobInstance == null)
{
_jobInstance = Invoker.CreateInstance();
}
return _jobInstance;
}
}

public FunctionDescriptor FunctionDescriptor
{
get { return _functionDescriptor; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ internal interface IFunctionInstance

IBindingSource BindingSource { get; }

object JobInstance { get; }

IFunctionInvoker Invoker { get; }

FunctionDescriptor FunctionDescriptor { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Azure.WebJobs.Host.Blobs;
using Microsoft.Azure.WebJobs.Host.Config;
using Microsoft.Azure.WebJobs.Host.Filters;
using Microsoft.Azure.WebJobs.Host.Indexers;
using Microsoft.Azure.WebJobs.Host.Listeners;
using Microsoft.Azure.WebJobs.Host.Loggers;
Expand Down Expand Up @@ -251,7 +252,9 @@ public static async Task<JobHostContext> CreateJobHostContextAsync(

if (functionExecutor == null)
{
functionExecutor = new FunctionExecutor(functionInstanceLogger, functionOutputLogger, exceptionHandler, trace, host, functionEventCollector, loggerFactory);
var extensionRegistry = config.GetService<IExtensionRegistry>();

functionExecutor = new FunctionExecutor(functionInstanceLogger, functionOutputLogger, exceptionHandler, trace, extensionRegistry, functionEventCollector, loggerFactory);
services.AddService(functionExecutor);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Host.Filters
{
/// <summary>
/// Base context class for <see cref="IFunctionExceptionFilter.OnExceptionAsync(FunctionExceptionContext, System.Threading.CancellationToken)"/>.
/// </summary>
public class FunctionExceptionContext : FunctionFilterContext
{
/// <summary>
/// Constructs a new instance
/// </summary>
/// <param name="functionInstanceId">The instance ID for the function invocation.</param>
/// <param name="functionName">The name of the function.</param>
/// <param name="logger"><see cref="ILogger"/> that can be used by the filter to log information.</param>
/// <param name="exception">The function exception.</param>
public FunctionExceptionContext(Guid functionInstanceId, string functionName, ILogger logger, Exception exception)
: base(functionInstanceId, functionName, logger)
{
Exception = exception;
}

/// <summary>
/// Gets the <see cref="Exception"/> that caused the function invocation to fail.
/// </summary>
public Exception Exception { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.WebJobs.Host.Filters
{
/// <summary>
/// Base class for declarative function exception filters.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public abstract class FunctionExceptionFilterAttribute : Attribute, IFunctionExceptionFilter
{
/// <inheritdoc/>
public abstract Task OnExceptionAsync(FunctionExceptionContext exceptionContext, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Host.Filters
{
/// <summary>
/// Context class for <see cref="IFunctionInvocationFilter.OnExecutedAsync(FunctionExecutedContext, System.Threading.CancellationToken)"/>>.
/// </summary>
public class FunctionExecutedContext : FunctionInvocationContext
{
/// <summary>
/// Constructs a new instance.
/// </summary>
/// <param name="arguments">The function arguments.</param>
/// <param name="properties">The property bag that can be used to pass information between filters.</param>
/// <param name="functionInstanceId">The instance ID for the function invocation.</param>
/// <param name="functionName">The name of the function.</param>
/// <param name="logger"><see cref="ILogger"/> that can be used by the filter to log information.</param>
/// <param name="functionResult">The function result.</param>
public FunctionExecutedContext(IReadOnlyDictionary<string, object> arguments, IDictionary<string, object> properties, Guid functionInstanceId, string functionName, ILogger logger, FunctionResult functionResult)
: base(arguments, properties, functionInstanceId, functionName, logger)
{
FunctionResult = functionResult;
}

/// <summary>
/// Gets the <see cref="FunctionResult"/>.
/// </summary>
public FunctionResult FunctionResult { get; internal set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Host.Filters
{
/// <summary>
/// Context class for <see cref="IFunctionInvocationFilter.OnExecutingAsync(FunctionExecutingContext, System.Threading.CancellationToken)"/>>.
/// </summary>
public class FunctionExecutingContext : FunctionInvocationContext
{
/// <summary>
/// Constructs a new instance.
/// </summary>
/// <param name="arguments">The function arguments.</param>
/// <param name="properties">The property bag that can be used to pass information between filters.</param>
/// <param name="functionInstanceId">The instance ID for the function invocation.</param>
/// <param name="functionName">The name of the function.</param>
/// <param name="logger"><see cref="ILogger"/> that can be used by the filter to log information.</param>
public FunctionExecutingContext(IReadOnlyDictionary<string, object> arguments, IDictionary<string, object> properties, Guid functionInstanceId, string functionName, ILogger logger)
: base(arguments, properties, functionInstanceId, functionName, logger)
{
}
}
}
47 changes: 47 additions & 0 deletions src/Microsoft.Azure.WebJobs.Host/Filters/FunctionFilterContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Host.Filters
{
/// <summary>
/// Base context class for all function filter context objects.
/// </summary>
public abstract class FunctionFilterContext
{
/// <summary>
/// Constructs a new instance.
/// </summary>
/// <param name="functionInstanceId">The instance ID for the function invocation.</param>
/// <param name="functionName">The name of the function.</param>
/// <param name="logger"><see cref="ILogger"/> that can be used by the filter to log information.</param>
protected FunctionFilterContext(Guid functionInstanceId, string functionName, ILogger logger)
{
FunctionInstanceId = functionInstanceId;
FunctionName = functionName;
Logger = logger;
}

/// <summary>
/// Gets the function instance ID.
/// </summary>
public Guid FunctionInstanceId { get; }

/// <summary>
/// Gets the name of the function.
/// </summary>
public string FunctionName { get; }

/// <summary>
/// Gets the <see cref="ILogger"/> that can be used by the filter to log information.
/// </summary>
public ILogger Logger { get; }

/// <summary>
/// Gets or sets the <see cref="IJobInvoker"/>.
/// </summary>
internal IJobInvoker Invoker { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Host.Filters
{
/// <summary>
/// Base context class for <see cref="IFunctionInvocationFilter"/> context objects.
/// </summary>
public abstract class FunctionInvocationContext : FunctionFilterContext
{
/// <summary>
/// Constructs a new instance.
/// </summary>
/// <param name="arguments">The function arguments.</param>
/// <param name="properties">The property bag that can be used to pass information between filters.</param>
/// <param name="functionInstanceId">The instance ID for the function invocation.</param>
/// <param name="functionName">The name of the function.</param>
/// <param name="logger"><see cref="ILogger"/> that can be used by the filter to log information.</param>
protected FunctionInvocationContext(IReadOnlyDictionary<string, object> arguments, IDictionary<string, object> properties, Guid functionInstanceId, string functionName, ILogger logger)
: base(functionInstanceId, functionName, logger)
{
Arguments = arguments;
Properties = properties;
}

/// <summary>
/// Gets the function arguments.
/// </summary>
public IReadOnlyDictionary<string, object> Arguments { get; }

/// <summary>
/// Gets the property bag that can be used to pass information between filters.
/// </summary>
public IDictionary<string, object> Properties { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.WebJobs.Host
namespace Microsoft.Azure.WebJobs.Host.Filters
{
/// <summary>
/// Base class for declarative function invocation filters.
/// </summary>
public abstract class InvocationFilterAttribute : Attribute, IFunctionInvocationFilter
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public abstract class FunctionInvocationFilterAttribute : Attribute, IFunctionInvocationFilter
{
/// <inheritdoc/>
public virtual Task OnExecutingAsync(FunctionExecutingContext executingContext, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.WebJobs.Host.Filters
{
/// <summary>
/// Defines a filter that will be called as part of the function invocation pipeline
/// for failed function invocations.
/// </summary>
public interface IFunctionExceptionFilter : IFunctionFilter
{
/// <summary>
/// Handles the function exception.
/// </summary>
/// <param name="exceptionContext">The <see cref="FunctionExceptionContext"/> for the failed invocation.</param>
/// <param name="cancellationToken">the cancellation token.</param>
/// <returns></returns>
Task OnExceptionAsync(FunctionExceptionContext exceptionContext, CancellationToken cancellationToken);
}
}
Loading

0 comments on commit 7617c92

Please sign in to comment.