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

Add BackgroundWorkerNameAttribute #20295

Merged
merged 6 commits into from
Jul 29, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Hangfire.States;

namespace Volo.Abp.BackgroundWorkers.Hangfire;

Expand All @@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,54 @@ public async override Task AddAsync(IBackgroundWorker worker, CancellationToken
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -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)
Expand All @@ -33,4 +34,6 @@ public async override Task DoWorkAsync(CancellationToken cancellationToken = def
break;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,7 @@ public void BuildWorker(IBackgroundWorker worker)

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
{
Expand All @@ -60,10 +53,10 @@ public void BuildWorker(IBackgroundWorker worker)

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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Volo.Abp.BackgroundWorkers;

public interface IBackgroundWorkerNameProvider
{
string Name { get; }
}
Loading