-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding IListenerDecorator support (#3011)
- Loading branch information
Showing
9 changed files
with
398 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Microsoft.Azure.WebJobs.Host/Listeners/FunctionListenerDecorator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.Listeners | ||
{ | ||
internal class FunctionListenerDecorator : IListenerDecorator | ||
{ | ||
private readonly ILoggerFactory _loggerFactory; | ||
private readonly IOptions<JobHostOptions> _jobHostOptions; | ||
|
||
public FunctionListenerDecorator(ILoggerFactory loggerFactory, IOptions<JobHostOptions> jobHostOptions) | ||
{ | ||
_loggerFactory = loggerFactory; | ||
_jobHostOptions = jobHostOptions; | ||
} | ||
|
||
public IListener Decorate(ListenerDecoratorContext context) | ||
{ | ||
// wrap the listener with a function listener to handle exceptions | ||
bool allowPartialHostStartup = _jobHostOptions.Value.AllowPartialHostStartup; | ||
return new FunctionListener(context.Listener, context.FunctionDefinition.Descriptor, _loggerFactory, allowPartialHostStartup); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Microsoft.Azure.WebJobs.Host/Listeners/IListenerDecorator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.Listeners | ||
{ | ||
/// <summary> | ||
/// Custom decorator interface called during <see cref="JobHost"/> listener creation to | ||
/// allow function listeners to be customized. | ||
/// </summary> | ||
public interface IListenerDecorator | ||
{ | ||
/// <summary> | ||
/// Creates a listener. | ||
/// </summary> | ||
/// <param name="context">The listener context.</param> | ||
/// <returns>The listener to use. This may be a new wrapped listener, or the original | ||
/// listener.</returns> | ||
IListener Decorate(ListenerDecoratorContext context); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Microsoft.Azure.WebJobs.Host/Listeners/ListenerDecoratorContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Azure.WebJobs.Host.Indexers; | ||
using System; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.Listeners | ||
{ | ||
/// <summary> | ||
/// Context class for <see cref="IListenerDecorator.Decorate(ListenerDecoratorContext)"/>. | ||
/// </summary> | ||
public class ListenerDecoratorContext | ||
{ | ||
/// <summary> | ||
/// Constructs an instance. | ||
/// </summary> | ||
/// <param name="functionDefinition">The function the specified listener is for.</param> | ||
/// <param name="rootListenerType">Gets the type of the root listener.</param> | ||
/// <param name="listener">The listener to decorate.</param> | ||
public ListenerDecoratorContext(IFunctionDefinition functionDefinition, Type rootListenerType, IListener listener) | ||
{ | ||
FunctionDefinition = functionDefinition; | ||
ListenerType = rootListenerType; | ||
Listener = listener; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the <see cref="IFunctionDefinition"/> the specified listener is for. | ||
/// </summary> | ||
public IFunctionDefinition FunctionDefinition { get; } | ||
|
||
/// <summary> | ||
/// Gets the listener to decorate. | ||
/// </summary> | ||
public IListener Listener { get; } | ||
|
||
/// <summary> | ||
/// Gets the type of the root listener. | ||
/// </summary> | ||
public Type ListenerType { get; } | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Microsoft.Azure.WebJobs.Host/Listeners/SingletonListenerDecorator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.Listeners | ||
{ | ||
internal class SingletonListenerDecorator : IListenerDecorator | ||
{ | ||
private readonly SingletonManager _singletonManager; | ||
private readonly ILoggerFactory _loggerFactory; | ||
|
||
public SingletonListenerDecorator(SingletonManager singletonManager, ILoggerFactory loggerFactory) | ||
{ | ||
_singletonManager = singletonManager; | ||
_loggerFactory = loggerFactory; | ||
} | ||
|
||
public IListener Decorate(ListenerDecoratorContext context) | ||
{ | ||
var functionDescriptor = context.FunctionDefinition.Descriptor; | ||
|
||
// if the listener is a Singleton, wrap it with our SingletonListener | ||
IListener listener = context.Listener; | ||
SingletonAttribute singletonAttribute = SingletonManager.GetListenerSingletonOrNull(context.ListenerType, functionDescriptor); | ||
if (singletonAttribute != null) | ||
{ | ||
listener = new SingletonListener(functionDescriptor, singletonAttribute, _singletonManager, listener, _loggerFactory); | ||
} | ||
|
||
return listener; | ||
} | ||
} | ||
} |
Oops, something went wrong.