Browse Source

Merge pull request #20305 from abpframework/auto-merge/prerel-8-3/2824

Merge branch dev with prerel-8.3
pull/20316/head
maliming 2 years ago
committed by GitHub
parent
commit
ecaf5b34a7
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 11
      framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerBase.cs
  2. 53
      framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs
  3. 9
      framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfirePeriodicBackgroundWorkerAdapter.cs
  4. 13
      framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs
  5. 42
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerNameAttribute.cs
  6. 6
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/IBackgroundWorkerNameProvider.cs

11
framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerBase.cs

@ -1,6 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Hangfire.States;
namespace Volo.Abp.BackgroundWorkers.Hangfire;
@ -10,15 +11,9 @@ public abstract class HangfireBackgroundWorkerBase : BackgroundWorkerBase, IHang
public string CronExpression { get; set; } = default!;
public TimeZoneInfo? TimeZone { get; set; }
public TimeZoneInfo? TimeZone { get; set; } = TimeZoneInfo.Utc;
public string Queue { get; set; }
public string Queue { get; set; } = EnqueuedState.DefaultQueue;
public abstract Task DoWorkAsync(CancellationToken cancellationToken = default);
protected HangfireBackgroundWorkerBase()
{
TimeZone = null;
Queue = "default";
}
}

53
framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs

@ -35,39 +35,54 @@ public class HangfireBackgroundWorkerManager : BackgroundWorkerManager, ISinglet
case IHangfireBackgroundWorker hangfireBackgroundWorker:
{
var unProxyWorker = ProxyHelper.UnProxy(hangfireBackgroundWorker);
if (hangfireBackgroundWorker.RecurringJobId.IsNullOrWhiteSpace())
{
RecurringJob.AddOrUpdate(
() => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(cancellationToken),
hangfireBackgroundWorker.CronExpression, hangfireBackgroundWorker.TimeZone,
hangfireBackgroundWorker.Queue);
}
else
{
RecurringJob.AddOrUpdate(hangfireBackgroundWorker.RecurringJobId,
() => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(cancellationToken),
hangfireBackgroundWorker.CronExpression, hangfireBackgroundWorker.TimeZone,
hangfireBackgroundWorker.Queue);
}
RecurringJob.AddOrUpdate(
hangfireBackgroundWorker.RecurringJobId,
hangfireBackgroundWorker.Queue,
() => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(cancellationToken),
hangfireBackgroundWorker.CronExpression,
new RecurringJobOptions
{
TimeZone = hangfireBackgroundWorker.TimeZone
});
break;
}
case AsyncPeriodicBackgroundWorkerBase or PeriodicBackgroundWorkerBase:
{
var timer = worker.GetType()
.GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker);
var timer = worker.GetType().GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker);
var period = worker is AsyncPeriodicBackgroundWorkerBase ? ((AbpAsyncTimer?)timer)?.Period : ((AbpTimer?)timer)?.Period;
if (period == null)
{
return;
}
var adapterType = typeof(HangfirePeriodicBackgroundWorkerAdapter<>).MakeGenericType(ProxyHelper.GetUnProxiedType(worker));
var workerAdapter = (Activator.CreateInstance(adapterType) as IHangfireBackgroundWorker)!;
RecurringJob.AddOrUpdate(() => workerAdapter.DoWorkAsync(cancellationToken), GetCron(period.Value), workerAdapter.TimeZone, workerAdapter.Queue);
if (workerAdapter.RecurringJobId.IsNullOrWhiteSpace())
{
RecurringJob.AddOrUpdate(
() => workerAdapter.DoWorkAsync(cancellationToken),
GetCron(period.Value),
workerAdapter.TimeZone ,
workerAdapter.Queue);
}
else
{
RecurringJob.AddOrUpdate(
workerAdapter.RecurringJobId,
workerAdapter.Queue,
() => workerAdapter.DoWorkAsync(cancellationToken),
GetCron(period.Value),
new RecurringJobOptions
{
TimeZone = workerAdapter.TimeZone
});
}
break;
}

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

@ -1,4 +1,5 @@
using System.Reflection;
using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
@ -13,9 +14,9 @@ public class HangfirePeriodicBackgroundWorkerAdapter<TWorker> : HangfireBackgrou
public HangfirePeriodicBackgroundWorkerAdapter()
{
_doWorkAsyncMethod =
typeof(TWorker).GetMethod("DoWorkAsync", BindingFlags.Instance | BindingFlags.NonPublic)!;
_doWorkAsyncMethod = typeof(TWorker).GetMethod("DoWorkAsync", BindingFlags.Instance | BindingFlags.NonPublic)!;
_doWorkMethod = typeof(TWorker).GetMethod("DoWork", BindingFlags.Instance | BindingFlags.NonPublic)!;
RecurringJobId = BackgroundWorkerNameAttribute.GetNameOrNull<TWorker>();
}
public async override Task DoWorkAsync(CancellationToken cancellationToken = default)
@ -33,4 +34,6 @@ public class HangfirePeriodicBackgroundWorkerAdapter<TWorker> : HangfireBackgrou
break;
}
}
}

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