diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobsModule.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobsModule.cs index 0613633893..9159ef966c 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobsModule.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobsModule.cs @@ -1,9 +1,12 @@ -using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.BackgroundWorkers; using Volo.Abp.Guids; using Volo.Abp.Json; using Volo.Abp.Modularity; +using Volo.Abp.Reflection; using Volo.Abp.Timing; namespace Volo.Abp.BackgroundJobs @@ -16,6 +19,11 @@ namespace Volo.Abp.BackgroundJobs )] public class AbpBackgroundJobsModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + RegisterJobs(context.Services); + } + public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAssemblyOf(); @@ -34,5 +42,28 @@ namespace Volo.Abp.BackgroundJobs ); } } + + private static void RegisterJobs(IServiceCollection services) + { + var jobTypes = new List(); + + services.OnRegistred(context => + { + if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IBackgroundJob<>))) + { + jobTypes.Add(context.ImplementationType); + } + }); + + services.Configure(options => + { + foreach (var jobType in jobTypes) + { + var jobArgsType = BackgroundJobHelper.GetJobArgsType(jobType); + var jobName = BackgroundJobNameAttribute.GetName(jobArgsType); + options.JobTypes[jobName] = jobType; + } + }); + } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobHelper.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobHelper.cs new file mode 100644 index 0000000000..050b47a329 --- /dev/null +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobHelper.cs @@ -0,0 +1,33 @@ +using System; + +namespace Volo.Abp.BackgroundJobs +{ + internal static class BackgroundJobHelper + { + public static Type GetJobArgsType(Type jobType) + { + foreach (var @interface in jobType.GetInterfaces()) + { + if (!@interface.IsGenericType) + { + continue; + } + + if (@interface.GetGenericTypeDefinition() != typeof(IBackgroundJob<>)) + { + continue; + } + + var genericArgs = @interface.GetGenericArguments(); + if (genericArgs.Length != 1) + { + continue; + } + + return genericArgs[0]; + } + + throw new AbpException($"Could not find type of the job args. Ensure that given type implements the {typeof(IBackgroundJob<>).AssemblyQualifiedName} interface. Given job type: {jobType.AssemblyQualifiedName}"); + } + } +} diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobManager.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobManager.cs index e03cc63793..5fc81eeb07 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobManager.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobManager.cs @@ -31,13 +31,13 @@ namespace Volo.Abp.BackgroundJobs Store = store; } - public Task EnqueueAsync(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null) + public virtual Task EnqueueAsync(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null) { - var jobName = BackgroundJobNameAttribute.GetNameOrNull(); + var jobName = BackgroundJobNameAttribute.GetName(); return EnqueueAsync(jobName, args, priority, delay); } - public async Task EnqueueAsync(string jobName, object args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null) + protected virtual async Task EnqueueAsync(string jobName, object args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null) { var jobInfo = new BackgroundJobInfo { diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobManagerExtensions.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobManagerExtensions.cs index 852e9acb71..467e03224b 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobManagerExtensions.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobManagerExtensions.cs @@ -21,24 +21,5 @@ namespace Volo.Abp.BackgroundJobs { AsyncHelper.RunSync(() => backgroundJobManager.EnqueueAsync(args, priority, delay)); } - - /// - /// Enqueues a job to be executed. - /// - /// Background job manager reference - /// Job name. - /// Job arguments. - /// Job priority. - /// Job delay (wait duration before first try). - /// Unique identifier of a background job. - public static Guid EnqueueAsync( - this IBackgroundJobManager backgroundJobManager, - string jobName, - object args, - BackgroundJobPriority priority = BackgroundJobPriority.Normal, - TimeSpan? delay = null) - { - return AsyncHelper.RunSync(() => backgroundJobManager.EnqueueAsync(jobName, args, priority, delay)); - } } } diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobNameAttribute.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobNameAttribute.cs index ea5505edab..ecff8e1ba6 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobNameAttribute.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobNameAttribute.cs @@ -13,18 +13,21 @@ namespace Volo.Abp.BackgroundJobs Name = Check.NotNullOrWhiteSpace(name, nameof(name)); } - public static string GetNameOrNull() + public static string GetName() { - return GetNameOrNull(typeof(TJobArgs)); + return GetName(typeof(TJobArgs)); } - public static string GetNameOrNull(Type jobArgsType) + public static string GetName([NotNull] Type jobArgsType) { + Check.NotNull(jobArgsType, nameof(jobArgsType)); + return jobArgsType - .GetCustomAttributes(true) - .OfType() - .FirstOrDefault() - ?.Name; + .GetCustomAttributes(true) + .OfType() + .FirstOrDefault() + ?.Name + ?? jobArgsType.FullName; } } } diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobManager.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobManager.cs index bea7c648a0..7dfef8c1a1 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobManager.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobManager.cs @@ -21,15 +21,5 @@ namespace Volo.Abp.BackgroundJobs BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null ); - - /// - /// Enqueues a job to be executed. - /// - /// Job name. - /// Job arguments. - /// Job priority. - /// Job delay (wait duration before first try). - /// Unique identifier of a background job. - Task EnqueueAsync(string jobName, object args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null); } } \ No newline at end of file diff --git a/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo.Abp.BackgroundJobs.Tests.csproj b/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo.Abp.BackgroundJobs.Tests.csproj index 6c238a997c..81a2bfb230 100644 --- a/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo.Abp.BackgroundJobs.Tests.csproj +++ b/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo.Abp.BackgroundJobs.Tests.csproj @@ -14,6 +14,7 @@ + diff --git a/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpBackgroundJobsTestModule.cs b/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpBackgroundJobsTestModule.cs index a359ed3b7d..50a0199426 100644 --- a/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpBackgroundJobsTestModule.cs +++ b/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/AbpBackgroundJobsTestModule.cs @@ -1,21 +1,18 @@ using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; using Volo.Abp.Modularity; namespace Volo.Abp.BackgroundJobs { [DependsOn( - typeof(AbpBackgroundJobsModule) + typeof(AbpBackgroundJobsModule), + typeof(AbpAutofacModule), + typeof(AbpTestBaseModule) )] public class AbpBackgroundJobsTestModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - //TODO: Can we automatically register these! - context.Services.Configure(options => - { - options.JobTypes[MyJobArgs.Name] = typeof(MyJob); - }); - context.Services.AddAssemblyOf(); } } diff --git a/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobsTestBase.cs b/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobsTestBase.cs index 23ca2d2d2a..6351320242 100644 --- a/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobsTestBase.cs +++ b/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobsTestBase.cs @@ -2,6 +2,9 @@ { public abstract class BackgroundJobsTestBase : AbpIntegratedTest { - + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } } } \ No newline at end of file diff --git a/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyJobArgs.cs b/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyJobArgs.cs index e1254ee565..2a641b8e43 100644 --- a/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyJobArgs.cs +++ b/framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyJobArgs.cs @@ -1,10 +1,7 @@ namespace Volo.Abp.BackgroundJobs { - [BackgroundJobName(Name)] public class MyJobArgs { - public const string Name = "TestJobs.MyJob"; - public string Value { get; set; } public MyJobArgs() diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs new file mode 100644 index 0000000000..842940cfaf --- /dev/null +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.Reflection +{ + public class ReflectionHelper_Tests + { + //TODO: ... + } +}