Browse Source

Add BackgroundWorkerNameAttribute

pull/20295/head
liangshiwei 2 years ago
parent
commit
80579e6d74
  1. 31
      framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs
  2. 2
      framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfirePeriodicBackgroundWorkerAdapter.cs
  3. 13
      framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs
  4. 42
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerNameAttribute.cs
  5. 6
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/IBackgroundWorkerNameProvider.cs

31
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;
}

2
framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfirePeriodicBackgroundWorkerAdapter.cs

@ -16,7 +16,7 @@ public class HangfirePeriodicBackgroundWorkerAdapter<TWorker> : 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<TWorker>();
}
public async override Task DoWorkAsync(CancellationToken cancellationToken = default)

13
framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs

@ -39,14 +39,7 @@ public class QuartzPeriodicBackgroundWorkerAdapter<TWorker> : 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<TWorker> : QuartzBackgroundWo
JobDetail = JobBuilder
.Create<QuartzPeriodicBackgroundWorkerAdapter<TWorker>>()
.WithIdentity(workerType.FullName!)
.WithIdentity(BackgroundWorkerNameAttribute.GetName<TWorker>())
.Build();
Trigger = TriggerBuilder.Create()
.WithIdentity(workerType.FullName!)
.WithIdentity(BackgroundWorkerNameAttribute.GetName<TWorker>())
.WithSimpleSchedule(builder => builder.WithInterval(TimeSpan.FromMilliseconds(period.Value)).RepeatForever())
.Build();
}

42
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<TWorkerType>()
{
return GetName(typeof(TWorkerType));
}
public static string GetName(Type workerType)
{
Check.NotNull(workerType, nameof(workerType));
return GetNameOrNull(workerType) ?? workerType.FullName!;
}
public static string? GetNameOrNull<TWorkerType>()
{
return GetNameOrNull(typeof(TWorkerType));
}
public static string? GetNameOrNull(Type workerType)
{
Check.NotNull(workerType, nameof(workerType));
return workerType
.GetCustomAttributes(true)
.OfType<IBackgroundWorkerNameProvider>()
.FirstOrDefault()
?.Name;
}
}

6
framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/IBackgroundWorkerNameProvider.cs

@ -0,0 +1,6 @@
namespace Volo.Abp.BackgroundWorkers;
public interface IBackgroundWorkerNameProvider
{
string Name { get; }
}
Loading…
Cancel
Save