[maliming] said: HangfireBackgroundWorkerManager
Hello maliming and thank you for you response I am using Abp 9.3.5 and the UI is Blazor WASM I tried to impalement your suggestion and overridden the HangfireBackgroundWorkerManager like this
namespace MyApp.BackgroundJobs;
[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(IBackgroundWorkerManager),typeof(HangfireBackgroundWorkerManager))]
public class HangfireBackgroundWorkerManagerOverride : HangfireBackgroundWorkerManager, IBackgroundWorkerManager, ISingletonDependency
{
public HangfireBackgroundWorkerManagerOverride(IServiceProvider serviceProvider)
:base(serviceProvider)
{
}
public void Initialize()
{
BackgroundJobServer = ServiceProvider.GetRequiredService<AbpHangfireBackgroundJobServer>();
}
public async override Task AddAsync(IBackgroundWorker worker, CancellationToken cancellationToken = default)
{
var abpHangfireOptions = ServiceProvider.GetRequiredService<IOptions<AbpHangfireOptions>>().Value;
var defaultQueuePrefix = abpHangfireOptions.DefaultQueuePrefix;
var defaultQueue = abpHangfireOptions.DefaultQueue;
switch (worker)
{
case IHangfireBackgroundWorker hangfireBackgroundWorker:
{
var unProxyWorker = ProxyHelper.UnProxy(hangfireBackgroundWorker);
RecurringJob.AddOrUpdate(
hangfireBackgroundWorker.RecurringJobId,
//hangfireBackgroundWorker.Queue.IsNullOrWhiteSpace() ? defaultQueue : defaultQueuePrefix + hangfireBackgroundWorker,
() => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(cancellationToken),
hangfireBackgroundWorker.CronExpression,
new RecurringJobOptions
{
TimeZone = hangfireBackgroundWorker.TimeZone
});
break;
}
case AsyncPeriodicBackgroundWorkerBase or PeriodicBackgroundWorkerBase:
{
int? period = null;
string? CronExpression = null;
if (worker is AsyncPeriodicBackgroundWorkerBase asyncPeriodicBackgroundWorkerBase)
{
period = asyncPeriodicBackgroundWorkerBase.Period;
CronExpression = asyncPeriodicBackgroundWorkerBase.CronExpression;
}
else if (worker is PeriodicBackgroundWorkerBase periodicBackgroundWorkerBase)
{
period = periodicBackgroundWorkerBase.Period;
CronExpression = periodicBackgroundWorkerBase.CronExpression;
}
if (period == null && CronExpression.IsNullOrWhiteSpace())
{
return;
}
var adapterType = typeof(HangfirePeriodicBackgroundWorkerAdapter<>).MakeGenericType(ProxyHelper.GetUnProxiedType(worker));
var workerAdapter = (Activator.CreateInstance(adapterType) as IHangfireBackgroundWorker)!;
Expression<Func<Task>> methodCall = () => workerAdapter.DoWorkAsync(cancellationToken);
var recurringJobId = !workerAdapter.RecurringJobId.IsNullOrWhiteSpace() ? workerAdapter.RecurringJobId : GetRecurringJobId(worker, methodCall);
RecurringJob.AddOrUpdate(
recurringJobId,
//workerAdapter.Queue.IsNullOrWhiteSpace() ? defaultQueue : defaultQueuePrefix + workerAdapter,
methodCall,
CronExpression ?? GetCron(period!.Value),
new RecurringJobOptions
{
TimeZone = workerAdapter.TimeZone
});
break;
}
default:
await base.AddAsync(worker, cancellationToken);
break;
}
}
private readonly static MethodInfo? GetRecurringJobIdMethodInfo = typeof(RecurringJob).GetMethod("GetRecurringJobId", BindingFlags.NonPublic | BindingFlags.Static);
protected virtual string? GetRecurringJobId(IBackgroundWorker worker, Expression<Func<Task>> methodCall)
{
...
}
protected virtual string GetCron(int period)
{
...
}
}
commenting the Queue but HangfireBackgroundWorkerManagerOverride is never called and the error persists can you give me some guidance what I am doing wrong.
Thank you in advance