Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
adambajguz committed Dec 6, 2023
1 parent bc6f841 commit 157c5f3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@
/// <summary>
/// Pipeline invocation exception.
/// </summary>
public sealed class PipelineInvocationException : Exception
/// <remarks>
/// Initializes a new instance of <see cref="PipelineInvocationException"/>.
/// </remarks>
/// <param name="args"></param>
/// <param name="pipeline"></param>
/// <param name="innerException"></param>
public sealed class PipelineInvocationException(
object? args,
IPipeline pipeline,
Exception? innerException) :
Exception($"An unhandled error occurred while executing pipeline '{pipeline.GetType().FullName ?? pipeline.GetType().Name}' with name '{pipeline.Name}'.", innerException)
{
/// <summary>
/// Pipeline args.
/// </summary>
public object? Args { get; }
public object? Args { get; } = args;

/// <summary>
/// Pipeline.
/// </summary>
public IPipeline Pipeline { get; }

/// <summary>
/// Initializes a new instance of <see cref="PipelineInvocationException"/>.
/// </summary>
/// <param name="args"></param>
/// <param name="pipeline"></param>
/// <param name="innerException"></param>
public PipelineInvocationException(object? args, IPipeline pipeline, Exception? innerException) :
base($"An unhandled error occurred while executing pipeline '{pipeline.GetType().FullName ?? pipeline.GetType().Name}' with name '{pipeline.Name}'.", innerException)
{
Args = args;
Pipeline = pipeline;
}
public IPipeline Pipeline { get; } = pipeline;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,25 @@
using PackSite.Library.Pipelining.Configuration;
using PackSite.Library.Pipelining.Configuration.Extensions;

internal sealed class PipeliningConfigurationHostedService : IHostedService, IDisposable
/// <summary>
/// Initializes a new instance of <see cref="PipeliningConfigurationHostedService"/>.
/// </summary>
/// <param name="options"></param>
/// <param name="pipelineCollection"></param>
/// <param name="loggerFactory"></param>
internal sealed class PipeliningConfigurationHostedService(
IOptionsMonitor<PipeliningConfiguration> options,
IPipelineCollection pipelineCollection,
ILoggerFactory loggerFactory) : IHostedService, IDisposable
{
private IReadOnlyList<PipelineName> _lastRegistered = new List<PipelineName>();
private IDisposable? _optionsMonitor;

private readonly SemaphoreSlim _lock = new(1, 1);

private readonly IOptionsMonitor<PipeliningConfiguration> _options;
private readonly IPipelineCollection _pipelineCollection;
private readonly ILogger _logger;

/// <summary>
/// Initializes a new instance of <see cref="PipeliningConfigurationHostedService"/>.
/// </summary>
/// <param name="options"></param>
/// <param name="pipelineCollection"></param>
/// <param name="loggerFactory"></param>
public PipeliningConfigurationHostedService(IOptionsMonitor<PipeliningConfiguration> options, IPipelineCollection pipelineCollection, ILoggerFactory loggerFactory)
{
_options = options;

_pipelineCollection = pipelineCollection;
_logger = loggerFactory.CreateLogger("PackSite.Library.Pipelining.Configuration");
}
private readonly IOptionsMonitor<PipeliningConfiguration> _options = options;
private readonly IPipelineCollection _pipelineCollection = pipelineCollection;
private readonly ILogger _logger = loggerFactory.CreateLogger("PackSite.Library.Pipelining.Configuration");

/// <inheritdoc/>
public async Task StartAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -74,10 +69,10 @@ private async Task UpdatePipelinesAsync(PipeliningConfiguration pipeliningConfig

foreach (IPipeline pipeline in pipelines)
{
bool r = _pipelineCollection.AddOrUpdate(pipeline);
bool wasAdded = _pipelineCollection.AddOrUpdate(pipeline);
toPreserve.Add(pipeline.Name);

if (r)
if (wasAdded)
{
++added;
}
Expand Down
43 changes: 17 additions & 26 deletions src/PackSite.Library.Pipelining/Internal/InvokablePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ namespace PackSite.Library.Pipelining.Internal
/// Invokable pipeline.
/// </summary>
/// <typeparam name="TArgs"></typeparam>
internal sealed class InvokablePipeline<TArgs> : IInvokablePipeline<TArgs>
/// <remarks>
/// Initializes a new instance of <see cref="InvokablePipeline{TArgs}"/>.
/// </remarks>
/// <param name="pipeline"></param>
/// <param name="pipelineCounters"></param>
/// <param name="universalSteps"></param>
/// <param name="genericSteps"></param>
internal sealed class InvokablePipeline<TArgs>(
IPipeline<TArgs> pipeline,
PipelineCounters pipelineCounters,
IStep?[] universalSteps,
IStep<TArgs>?[] genericSteps) : IInvokablePipeline<TArgs>
where TArgs : class
{
/// <summary>
Expand All @@ -21,39 +32,19 @@ internal delegate ValueTask ConcreteStepDelegate(IBaseStep[] steps,
StepDelegate terminationContinuation,
CancellationToken cancellationToken);

private readonly PipelineCounters _pipelineCounters;
private readonly PipelineCounters _pipelineCounters = pipelineCounters;
private readonly InvokablePipelineCounters _invokablePipelineCounters = new();
private readonly IStep?[] _universalSteps;
private readonly IStep<TArgs>?[] _genericSteps;
private readonly int _stepsCount;
private readonly IStep?[] _universalSteps = universalSteps;
private readonly IStep<TArgs>?[] _genericSteps = genericSteps;
private readonly int _stepsCount = Math.Max(universalSteps.Length, genericSteps.Length);

/// <inheritdoc/>
public IInvokablePipelineCounters Counters => _invokablePipelineCounters;

/// <inheritdoc/>
public IPipeline<TArgs> Pipeline { get; }
public IPipeline<TArgs> Pipeline { get; } = pipeline;
IPipeline IInvokablePipeline.Pipeline => Pipeline;

/// <summary>
/// Initializes a new instance of <see cref="InvokablePipeline{TArgs}"/>.
/// </summary>
/// <param name="pipeline"></param>
/// <param name="pipelineCounters"></param>
/// <param name="universalSteps"></param>
/// <param name="genericSteps"></param>
public InvokablePipeline(IPipeline<TArgs> pipeline,
PipelineCounters pipelineCounters,
IStep?[] universalSteps,
IStep<TArgs>?[] genericSteps)
{
Pipeline = pipeline;
_pipelineCounters = pipelineCounters;
_universalSteps = universalSteps;
_genericSteps = genericSteps;

_stepsCount = Math.Max(universalSteps.Length, genericSteps.Length);
}

/// <inheritdoc/>
public ValueTask<TArgs> InvokeAsync(TArgs input, CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
{
internal static class PipelineDelegateInitializerProxy
{
public sealed class Simple : IPipelineInitializer
/// <summary>
/// Initializes a new instance of <see cref="Simple"/>.
/// </summary>
/// <param name="initializerDelegate"></param>
public sealed class Simple(
Action<IPipelineCollection>? initializerDelegate) : IPipelineInitializer
{
private readonly Action<IPipelineCollection>? _initializerDelegate;

/// <summary>
/// Initializes a new instance of <see cref="Simple"/>.
/// </summary>
/// <param name="initializerDelegate"></param>
public Simple(Action<IPipelineCollection>? initializerDelegate)
{
_initializerDelegate = initializerDelegate;
}
private readonly Action<IPipelineCollection>? _initializerDelegate = initializerDelegate;

/// <inheritdoc/>
public ValueTask RegisterAsync(IPipelineCollection pipelines, CancellationToken cancellationToken)
Expand All @@ -24,21 +20,17 @@ public ValueTask RegisterAsync(IPipelineCollection pipelines, CancellationToken
}
}

public sealed class Complex : IPipelineInitializer
/// <summary>
/// Initializes a new instance of <see cref="Complex"/>.
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="initializerDelegate"></param>
public sealed class Complex(
IServiceProvider serviceProvider,
Func<IServiceProvider, IPipelineCollection, CancellationToken, ValueTask>? initializerDelegate) : IPipelineInitializer
{
private readonly IServiceProvider _serviceProvider;
private readonly Func<IServiceProvider, IPipelineCollection, CancellationToken, ValueTask>? _initializerDelegate;

/// <summary>
/// Initializes a new instance of <see cref="Complex"/>.
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="initializerDelegate"></param>
public Complex(IServiceProvider serviceProvider, Func<IServiceProvider, IPipelineCollection, CancellationToken, ValueTask>? initializerDelegate)
{
_serviceProvider = serviceProvider;
_initializerDelegate = initializerDelegate;
}
private readonly IServiceProvider _serviceProvider = serviceProvider;
private readonly Func<IServiceProvider, IPipelineCollection, CancellationToken, ValueTask>? _initializerDelegate = initializerDelegate;

/// <inheritdoc/>
public async ValueTask RegisterAsync(IPipelineCollection pipelines, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public IBaseStep Create(Type stepType)
{
ObjectFactory stepFactory = _cache.GetOrAdd(stepType, (key) => // The factory may run multiple times but we don't care since we don't want to add overhead with lazy
{
return ActivatorUtilities.CreateFactory(key, Array.Empty<Type>());
return ActivatorUtilities.CreateFactory(key, []);
});

return stepFactory(_serviceProvider, null) as IBaseStep ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@
/// <summary>
/// <see cref="IServiceProvider"/> and <see cref="ActivatorUtilities"/> based step activator.
/// </summary>
public sealed class ServicesStepActivator : IStepActivator
/// <remarks>
/// Initializes a new instance of <see cref="ServicesStepActivator"/>
/// </remarks>
public sealed class ServicesStepActivator(
IServiceProvider serviceProvider) : IStepActivator
{
private readonly IServiceProvider _serviceProvider;
private readonly IServiceProvider _serviceProvider = serviceProvider;

private static readonly ConcurrentDictionary<Type, ObjectFactory> _cache = new();

/// <summary>
/// Initializes a new instance of <see cref="ServicesStepActivator"/>
/// </summary>
public ServicesStepActivator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

/// <inheritdoc/>
public IBaseStep Create(Type stepType)
{
ObjectFactory stepFactory = _cache.GetOrAdd(stepType, (key) => // The factory may run multiple times but we don't care since we don't want to add overhead with lazy
{
return ActivatorUtilities.CreateFactory(key, Array.Empty<Type>());
return ActivatorUtilities.CreateFactory(key, []);
});

return stepFactory(_serviceProvider, null) as IBaseStep ??
Expand Down

0 comments on commit 157c5f3

Please sign in to comment.