Skip to content

Commit

Permalink
Convert LINQ magic into regular method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
odinserj committed Jun 20, 2024
1 parent a7d211c commit 3600806
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
35 changes: 23 additions & 12 deletions src/Hangfire.Core/Client/BackgroundJobFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,34 @@ private CreatedContext CreateWithFilters(
IEnumerable<IClientFilter> filters)
{
var preContext = new CreatingContext(context);
Func<CreatedContext> continuation = () =>
{
var backgroundJob = _innerFactory.Create(context);
return new CreatedContext(context, backgroundJob, false, null);
};
using var enumerator = filters.GetEnumerator();

var thunk = filters.Reverse().Aggregate(continuation,
(next, filter) => () => InvokeClientFilter(filter, preContext, next));
return InvokeNextClientFilter(enumerator, _innerFactory, context, preContext);
}

private static CreatedContext InvokeNextClientFilter(
IEnumerator<IClientFilter> enumerator,
IBackgroundJobFactory innerFactory,
CreateContext context,
CreatingContext preContext)
{
if (enumerator.MoveNext())
{
return InvokeClientFilter(enumerator, innerFactory, context, preContext);
}

return thunk();
var backgroundJob = innerFactory.Create(context);
return new CreatedContext(context, backgroundJob, false, null);
}

private static CreatedContext InvokeClientFilter(
IClientFilter filter,
CreatingContext preContext,
Func<CreatedContext> continuation)
IEnumerator<IClientFilter> enumerator,
IBackgroundJobFactory innerFactory,
CreateContext context,
CreatingContext preContext)
{
var filter = enumerator.Current!;

preContext.Profiler.InvokeMeasured(
new KeyValuePair<IClientFilter, CreatingContext>(filter, preContext),
InvokeOnCreating,
Expand All @@ -143,7 +154,7 @@ private static CreatedContext InvokeClientFilter(
CreatedContext postContext;
try
{
postContext = continuation();
postContext = InvokeNextClientFilter(enumerator, innerFactory, context, preContext);
}
catch (Exception ex) when (ex.IsCatchableExceptionType())
{
Expand Down
41 changes: 24 additions & 17 deletions src/Hangfire.Core/Server/BackgroundJobPerformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,35 @@ private JobFilterInfo GetFilters(Job job)

private object PerformJobWithFilters(PerformContext context, IEnumerable<IServerFilter> filters)
{
object result = null;

var preContext = new PerformingContext(context);
Func<PerformedContext> continuation = () =>
{
result = _innerPerformer.Perform(context);
return new PerformedContext(context, result, false, null);
};
using var enumerator = filters.GetEnumerator();

var thunk = filters.Reverse().Aggregate(continuation,
(next, filter) => () => InvokePerformFilter(filter, preContext, next));

thunk();
return InvokeNextServerFilter(enumerator, _innerPerformer, context, preContext).Result;
}

return result;
private static PerformedContext InvokeNextServerFilter(
IEnumerator<IServerFilter> enumerator,
IBackgroundJobPerformer innerPerformer,
PerformContext context,
PerformingContext preContext)
{
if (enumerator.MoveNext())
{
return InvokeServerFilter(enumerator, innerPerformer, context, preContext);
}

var result = innerPerformer.Perform(context);
return new PerformedContext(context, result, false, null);
}

private static PerformedContext InvokePerformFilter(
IServerFilter filter,
PerformingContext preContext,
Func<PerformedContext> continuation)
private static PerformedContext InvokeServerFilter(
IEnumerator<IServerFilter> enumerator,
IBackgroundJobPerformer innerPerformer,
PerformContext context,
PerformingContext preContext)
{
var filter = enumerator.Current!;

try
{
preContext.Profiler.InvokeMeasured(
Expand Down Expand Up @@ -165,7 +172,7 @@ private static PerformedContext InvokePerformFilter(
PerformedContext postContext;
try
{
postContext = continuation();
postContext = InvokeNextServerFilter(enumerator, innerPerformer, context, preContext);
}
catch (Exception ex) when (ex.IsCatchableExceptionType())
{
Expand Down

0 comments on commit 3600806

Please sign in to comment.