Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't store actual values for the special argument types #1388

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Hangfire.Core/Common/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Hangfire.Annotations;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Hangfire.Server;

namespace Hangfire.Common
{
Expand Down Expand Up @@ -153,7 +154,7 @@ public Job([NotNull] Type type, [NotNull] MethodInfo method, [NotNull] params ob

Type = type;
Method = method;
Args = args;
Args = PrepareArgs(method, args);
}

/// <summary>
Expand Down Expand Up @@ -450,5 +451,26 @@ private static object GetExpressionValue(Expression expression)
? constantExpression.Value
: CachedExpressionCompiler.Evaluate(expression);
}

private static object[] PrepareArgs(MethodInfo method, object[] args)
{
var parameters = method.GetParameters();

for (var i = 0; i < parameters.Length; i++)
{
var parameter = parameters[i];

if (CoreBackgroundJobPerformer.
Substitutions.ContainsKey(parameter.ParameterType))
{
// This argument has a special type, so any values would be
// overwritten by CoreBackgroundJobPerformer upon execution.
// Discard them early on (while also saving some storage).
args[i] = null;
}
}

return args;
}
}
}
16 changes: 16 additions & 0 deletions src/Hangfire.Core/Dashboard/JobMethodCallRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@
using System.Text;
using System.Threading;
using Hangfire.Common;
using Hangfire.Server;

namespace Hangfire.Dashboard
{
internal static class JobMethodCallRenderer
{
private static readonly int MaxArgumentToRenderSize = 4096;

private static readonly Dictionary<Type, string> Substitutions
= new Dictionary<Type, string>
{
{ typeof (IJobCancellationToken), $"{WrapType(nameof(JobCancellationToken))}.Null" },
{ typeof (CancellationToken), $"{WrapType(nameof(CancellationToken))}.None" },
{ typeof (PerformContext), WrapKeyword("null") }
};

public static NonEscapedString Render(Job job)
{
if (job == null) { return new NonEscapedString("<em>Can not find the target method.</em>"); }
Expand Down Expand Up @@ -94,6 +103,13 @@ public static NonEscapedString Render(Job job)
for (var i = 0; i < parameters.Length; i++)
{
var parameter = parameters[i];

if (Substitutions.TryGetValue(parameter.ParameterType, out var substitution))
{
renderedArguments.Add(substitution);
continue;
}

#pragma warning disable 618
var arguments = job.Arguments;
#pragma warning restore 618
Expand Down
35 changes: 35 additions & 0 deletions tests/Hangfire.Core.Tests/Common/JobFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class JobFacts
private readonly string[] _arguments;
private readonly Mock<JobActivator> _activator;
private readonly Mock<IJobCancellationToken> _token;
private readonly PerformContextMock _performContext;

public JobFacts()
{
Expand All @@ -38,6 +39,7 @@ public JobFacts()

_activator = new Mock<JobActivator> { CallBase = true };
_token = new Mock<IJobCancellationToken>();
_performContext = new PerformContextMock();
}

[Fact]
Expand Down Expand Up @@ -341,6 +343,39 @@ public void Ctor_ThrowsAnException_WhenMethodParametersContainAnExpression()
() => Job.FromExpression(() => ExpressionMethod(() => Console.WriteLine("Hey expression!"))));
}

public interface ISpecialArgs
{
void Method1(PerformContext context);

void Method2(IJobCancellationToken cancellationToken, int number);

Task Method3(string str, CancellationToken cancellationToken);
}

[Fact]
public void Ctor_IgnoresValue_ForSpecialType_PerformContext()
{
var job = Job.FromExpression<ISpecialArgs>(x => x.Method1(_performContext.Object));

Assert.Equal(new object[] { null }, job.Args);
}

[Fact]
public void Ctor_IgnoresValue_ForSpecialType_IJobCancellationToken()
{
var job = Job.FromExpression<ISpecialArgs>(x => x.Method2(_token.Object, 123));

Assert.Equal(new object[] { null, 123 }, job.Args);
}

[Fact]
public void Ctor_IgnoresValue_ForSpecialType_CancellationToken()
{
var job = Job.FromExpression<ISpecialArgs>(x => x.Method3("123", CancellationToken.None));

Assert.Equal(new object[] { "123", null }, job.Args);
}

[Fact]
public void Perform_ThrowsAnException_WhenActivatorIsNull()
{
Expand Down