diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs index 9db6ff3501..f778774018 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs @@ -61,15 +61,28 @@ public class HangfireBackgroundWorkerManager : BackgroundWorkerManager, ISinglet var adapterType = typeof(HangfirePeriodicBackgroundWorkerAdapter<>).MakeGenericType(ProxyHelper.GetUnProxiedType(worker)); var workerAdapter = (Activator.CreateInstance(adapterType) as IHangfireBackgroundWorker)!; - RecurringJob.AddOrUpdate( - workerAdapter.RecurringJobId, - workerAdapter.Queue, - () => workerAdapter.DoWorkAsync(cancellationToken), - GetCron(period.Value), - new RecurringJobOptions - { - TimeZone = workerAdapter.TimeZone - }); + if (workerAdapter.RecurringJobId.IsNullOrWhiteSpace()) + { + RecurringJob.AddOrUpdate( + workerAdapter.Queue, + () => workerAdapter.DoWorkAsync(cancellationToken), + GetCron(period.Value), + workerAdapter.TimeZone); + } + else + { + + RecurringJob.AddOrUpdate( + workerAdapter.RecurringJobId, + workerAdapter.Queue, + () => workerAdapter.DoWorkAsync(cancellationToken), + GetCron(period.Value), + new RecurringJobOptions + { + TimeZone = workerAdapter.TimeZone + }); + } + break; } diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfirePeriodicBackgroundWorkerAdapter.cs b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfirePeriodicBackgroundWorkerAdapter.cs index 65fc4ecab1..9553bf12fc 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfirePeriodicBackgroundWorkerAdapter.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfirePeriodicBackgroundWorkerAdapter.cs @@ -16,7 +16,7 @@ public class HangfirePeriodicBackgroundWorkerAdapter : HangfireBackgrou { _doWorkAsyncMethod = typeof(TWorker).GetMethod("DoWorkAsync", BindingFlags.Instance | BindingFlags.NonPublic)!; _doWorkMethod = typeof(TWorker).GetMethod("DoWork", BindingFlags.Instance | BindingFlags.NonPublic)!; - RecurringJobId = typeof(TWorker).Name; + RecurringJobId = BackgroundWorkerNameAttribute.GetNameOrNull(); } public async override Task DoWorkAsync(CancellationToken cancellationToken = default) diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs index 767ac36412..522ef7a807 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs @@ -39,14 +39,7 @@ public class QuartzPeriodicBackgroundWorkerAdapter : QuartzBackgroundWo var timer = workerType.GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker); - if (worker is AsyncPeriodicBackgroundWorkerBase) - { - period = ((AbpAsyncTimer?)timer)?.Period; - } - else - { - period = ((AbpTimer?)timer)?.Period; - } + period = worker is AsyncPeriodicBackgroundWorkerBase ? ((AbpAsyncTimer?)timer)?.Period : ((AbpTimer?)timer)?.Period; } else { @@ -60,10 +53,10 @@ public class QuartzPeriodicBackgroundWorkerAdapter : QuartzBackgroundWo JobDetail = JobBuilder .Create>() - .WithIdentity(workerType.FullName!) + .WithIdentity(BackgroundWorkerNameAttribute.GetName()) .Build(); Trigger = TriggerBuilder.Create() - .WithIdentity(workerType.FullName!) + .WithIdentity(BackgroundWorkerNameAttribute.GetName()) .WithSimpleSchedule(builder => builder.WithInterval(TimeSpan.FromMilliseconds(period.Value)).RepeatForever()) .Build(); } diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerNameAttribute.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerNameAttribute.cs new file mode 100644 index 0000000000..4fc8521dad --- /dev/null +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerNameAttribute.cs @@ -0,0 +1,42 @@ +using System; +using System.Linq; + +namespace Volo.Abp.BackgroundWorkers; + +public class BackgroundWorkerNameAttribute : Attribute, IBackgroundWorkerNameProvider +{ + public string Name { get; } + + public BackgroundWorkerNameAttribute(string name) + { + Name = Check.NotNullOrWhiteSpace(name, nameof(name)); + } + + public static string GetName() + { + return GetName(typeof(TWorkerType)); + } + + public static string GetName(Type workerType) + { + Check.NotNull(workerType, nameof(workerType)); + + return GetNameOrNull(workerType) ?? workerType.FullName!; + } + + public static string? GetNameOrNull() + { + return GetNameOrNull(typeof(TWorkerType)); + } + + public static string? GetNameOrNull(Type workerType) + { + Check.NotNull(workerType, nameof(workerType)); + + return workerType + .GetCustomAttributes(true) + .OfType() + .FirstOrDefault() + ?.Name; + } +} diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/IBackgroundWorkerNameProvider.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/IBackgroundWorkerNameProvider.cs new file mode 100644 index 0000000000..8956a89c98 --- /dev/null +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/IBackgroundWorkerNameProvider.cs @@ -0,0 +1,6 @@ +namespace Volo.Abp.BackgroundWorkers; + +public interface IBackgroundWorkerNameProvider +{ + string Name { get; } +} \ No newline at end of file