mirror of https://github.com/abpframework/abp.git
Browse Source
Introduce dynamic background job support so jobs can be registered and executed without a compile-time job type. Key changes: - Add DynamicBackgroundJobContext, IDynamicBackgroundJobHandlerProvider and DynamicBackgroundJobHandlerProvider to allow registering/unregistering dynamic handlers at runtime. - Extend BackgroundJobConfiguration with DynamicHandler and IsDynamic, and make JobType nullable for dynamic scenarios. - Update AbpBackgroundJobOptions to use a ConcurrentDictionary for name lookup, and add methods to Add/Remove dynamic jobs and GetJobOrNull. - Extend JobExecutionContext with JobName and propagate it through Hangfire/Quartz/RabbitMQ/TickerQ adapters and worker code. - Update BackgroundJobExecuter to detect and execute dynamic handlers, deserialize/ensure dictionary args, and retain existing typed execution path. - Add tests (DynamicJobExecutionTracker, runtime/compile-time dynamic handler tests) and register a sample dynamic job in test module. - Update demo SampleJobCreator and DemoAppSharedModule to demonstrate compile-time and runtime dynamic job registration and enqueueing. These changes enable flexible, dictionary-based job arguments and runtime registration of background job handlers while preserving existing typed job execution.pull/25059/head
18 changed files with 390 additions and 39 deletions
@ -0,0 +1,25 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundJobs; |
||||
|
|
||||
|
public class DynamicBackgroundJobContext : IServiceProviderAccessor |
||||
|
{ |
||||
|
public IServiceProvider ServiceProvider { get; } |
||||
|
|
||||
|
public Dictionary<string, object> Args { get; } |
||||
|
|
||||
|
public CancellationToken CancellationToken { get; } |
||||
|
|
||||
|
public DynamicBackgroundJobContext( |
||||
|
IServiceProvider serviceProvider, |
||||
|
Dictionary<string, object> args, |
||||
|
CancellationToken cancellationToken = default) |
||||
|
{ |
||||
|
ServiceProvider = serviceProvider; |
||||
|
Args = args; |
||||
|
CancellationToken = cancellationToken; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundJobs; |
||||
|
|
||||
|
public class DynamicBackgroundJobHandlerProvider : IDynamicBackgroundJobHandlerProvider, ISingletonDependency |
||||
|
{ |
||||
|
protected AbpBackgroundJobOptions Options { get; } |
||||
|
|
||||
|
public DynamicBackgroundJobHandlerProvider(IOptions<AbpBackgroundJobOptions> options) |
||||
|
{ |
||||
|
Options = options.Value; |
||||
|
} |
||||
|
|
||||
|
public virtual void Register(string jobName, Func<DynamicBackgroundJobContext, Task> handler) |
||||
|
{ |
||||
|
Options.AddDynamicJob(jobName, handler); |
||||
|
} |
||||
|
|
||||
|
public virtual void Register(string jobName, Action<DynamicBackgroundJobContext> handler) |
||||
|
{ |
||||
|
Options.AddDynamicJob(jobName, handler); |
||||
|
} |
||||
|
|
||||
|
public virtual bool Unregister(string jobName) |
||||
|
{ |
||||
|
return Options.RemoveDynamicJob(jobName); |
||||
|
} |
||||
|
|
||||
|
public virtual bool IsRegistered(string jobName) |
||||
|
{ |
||||
|
return Options.GetJobOrNull(jobName)?.IsDynamic == true; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundJobs; |
||||
|
|
||||
|
public interface IDynamicBackgroundJobHandlerProvider |
||||
|
{ |
||||
|
void Register(string jobName, Func<DynamicBackgroundJobContext, Task> handler); |
||||
|
|
||||
|
void Register(string jobName, Action<DynamicBackgroundJobContext> handler); |
||||
|
|
||||
|
bool Unregister(string jobName); |
||||
|
|
||||
|
bool IsRegistered(string jobName); |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundJobs; |
||||
|
|
||||
|
public class DynamicJobExecutionTracker |
||||
|
{ |
||||
|
public List<Dictionary<string, object>> ExecutedArgs { get; } = new(); |
||||
|
} |
||||
Loading…
Reference in new issue