From 3099f4728e4507fd3ecd83bcb1c690925bd84087 Mon Sep 17 00:00:00 2001 From: Nicola Avancini Date: Wed, 14 Jun 2023 12:01:08 +0200 Subject: [PATCH 1/2] add displayname functionality --- src/Hangfire.DynamicJobs/DynamicJob.cs | 9 +++++- .../DynamicJobDisplayNameAttribute.cs | 29 +++++++++++++++++++ ...DynamicJobRecurringJobManagerExtensions.cs | 20 ++++++++++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/Hangfire.DynamicJobs/DynamicJob.cs b/src/Hangfire.DynamicJobs/DynamicJob.cs index 256b42b..850d245 100644 --- a/src/Hangfire.DynamicJobs/DynamicJob.cs +++ b/src/Hangfire.DynamicJobs/DynamicJob.cs @@ -17,13 +17,15 @@ public DynamicJob( [NotNull] string method, [CanBeNull] string parameterTypes, [CanBeNull] string args, - [CanBeNull] JobFilterAttribute[] filters) + [CanBeNull] JobFilterAttribute[] filters, + [CanBeNull] JobDisplayNameAttribute displayName) { Type = type ?? throw new ArgumentNullException(nameof(type)); Method = method ?? throw new ArgumentNullException(nameof(method)); ParameterTypes = parameterTypes; Args = args; Filters = filters; + DisplayName = displayName; } [NotNull] @@ -46,6 +48,11 @@ public DynamicJob( [JsonProperty("f", NullValueHandling = NullValueHandling.Ignore, ItemTypeNameHandling = TypeNameHandling.Auto)] public JobFilterAttribute[] Filters { get; } + //TODO: Check if is better save display name as resolved string (maybe when you use location resources can be cause crash?) + [CanBeNull] + [JsonProperty("dn", NullValueHandling = NullValueHandling.Ignore, ItemTypeNameHandling = TypeNameHandling.Auto)] + public JobDisplayNameAttribute DisplayName { get; } + [PublicAPI] [DynamicJobDisplayName] public static object Execute(DynamicJob dynamicJob, PerformContext context) diff --git a/src/Hangfire.DynamicJobs/DynamicJobDisplayNameAttribute.cs b/src/Hangfire.DynamicJobs/DynamicJobDisplayNameAttribute.cs index ed488f1..789e221 100644 --- a/src/Hangfire.DynamicJobs/DynamicJobDisplayNameAttribute.cs +++ b/src/Hangfire.DynamicJobs/DynamicJobDisplayNameAttribute.cs @@ -2,6 +2,7 @@ // Please see the LICENSE file for the licensing details. using System; +using System.Globalization; using Hangfire.Annotations; using Hangfire.Common; using Hangfire.Dashboard; @@ -23,6 +24,13 @@ public override string Format([NotNull] DashboardContext context, [NotNull] Job { if (arg is DynamicJob dynamicJob) { + //create display name from JobDisplayNameAttribute + if (dynamicJob.DisplayName != null) + { + var displayName = GenerateDisplayName(dynamicJob); + return displayName; + } + return $"Dynamic: {ExtractTypeName(dynamicJob.Type, out _, out _)}.{dynamicJob.Method}"; } } @@ -60,5 +68,26 @@ internal static string ExtractTypeName(string type, out string @namespace, out s return type; } + + + + internal static string GenerateDisplayName(DynamicJob job) + { + + var text = job.DisplayName.DisplayName; + + //code from format method of JobDisplayNameAttribute with private properties (cache/resources) + //if (ResourceType != null) + //{ + // text = _resourceManagerCache.GetOrAdd(ResourceType, InitResourceManager).GetString(DisplayName, CultureInfo.CurrentUICulture); + // if (string.IsNullOrEmpty(text)) + // { + // text = DisplayName; + // } + //} + string[] arguments = SerializationHelper.Deserialize(job.Args); + return string.Format(CultureInfo.CurrentCulture, text, arguments); + } + } } \ No newline at end of file diff --git a/src/Hangfire.DynamicJobs/DynamicJobRecurringJobManagerExtensions.cs b/src/Hangfire.DynamicJobs/DynamicJobRecurringJobManagerExtensions.cs index a94aae0..3e2831b 100644 --- a/src/Hangfire.DynamicJobs/DynamicJobRecurringJobManagerExtensions.cs +++ b/src/Hangfire.DynamicJobs/DynamicJobRecurringJobManagerExtensions.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Reflection; using System.Threading.Tasks; using Hangfire.Annotations; using Hangfire.Common; @@ -138,11 +139,28 @@ private static Job ToDynamicJob([NotNull] Job job, [CanBeNull] IEnumerable(inherit: true).FirstOrDefault(); + //TODO: maybe is better resolve name here and serialize as string? + + //get queue filter from original job + //// Job.Method.GetMethodFilterAttributes() inaccessibile + //// ReflectedAttributeCache.GetMethodFilterAttributes(job.Method); inaccessible + var typeFiltes = job.Type.GetTypeInfo().GetCustomAttributes(typeof(QueueAttribute), inherit: true).Cast(); + var methodFiltes = job.Method.GetCustomAttributes(typeof(QueueAttribute), inherit: true).Cast(); + + //generate list of filters for dynamicjob + var dynamicfilters = new List(filters?.ToArray() ?? Enumerable.Empty()); + dynamicfilters.AddRange(typeFiltes); + dynamicfilters.AddRange(methodFiltes); + var dynamicJob = new DynamicJob(invocationData.Type, invocationData.Method, !String.IsNullOrEmpty(invocationData.ParameterTypes) ? invocationData.ParameterTypes : null, invocationData.Arguments, - filters?.ToArray()); + dynamicfilters?.ToArray(), + jobDisplayNameAttribute); return Job.FromExpression(() => DynamicJob.Execute(dynamicJob, default)); } From 8fb70c95ee303c03e291e8ca64151a15de4b49c9 Mon Sep 17 00:00:00 2001 From: Nicola Avancini Date: Wed, 14 Jun 2023 12:05:22 +0200 Subject: [PATCH 2/2] update sample for use display name --- .../NewsletterSender.cs | 2 ++ samples/Hangfire.Microservices.OrdersService/OrderSubmitter.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/samples/Hangfire.Microservices.NewsletterService/NewsletterSender.cs b/samples/Hangfire.Microservices.NewsletterService/NewsletterSender.cs index 9fc8f4f..a5fc5a7 100644 --- a/samples/Hangfire.Microservices.NewsletterService/NewsletterSender.cs +++ b/samples/Hangfire.Microservices.NewsletterService/NewsletterSender.cs @@ -5,6 +5,8 @@ namespace Hangfire.Microservices.NewsletterService [Queue("newsletter")] public sealed class NewsletterSender { + + [JobDisplayName("Newsletter {0}")] public static void Execute(long campaignId) { Console.WriteLine($"Processing newsletter '{campaignId}'"); diff --git a/samples/Hangfire.Microservices.OrdersService/OrderSubmitter.cs b/samples/Hangfire.Microservices.OrdersService/OrderSubmitter.cs index 7f7707d..f6aa7c1 100644 --- a/samples/Hangfire.Microservices.OrdersService/OrderSubmitter.cs +++ b/samples/Hangfire.Microservices.OrdersService/OrderSubmitter.cs @@ -2,8 +2,10 @@ namespace Hangfire.Microservices.OrdersService { + [Queue("orders")] public class OrderSubmitter { + [JobDisplayName("order {0} - status {1}")] public void Execute(long orderId, string status) { Console.WriteLine($"Submitting order {orderId} with status {status}");