From a7187dcf5743f5e31072d8d0feeef01768ba0ada Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Tue, 24 Jul 2018 11:35:40 +0300 Subject: [PATCH] Implemented IBackgroundJobStore --- .../BackgroundJobs/BackgroundJobExecuter.cs | 2 +- .../Abp/BackgroundJobs/BackgroundJobInfo.cs | 16 ----- .../Abp/BackgroundJobs/IBackgroundJobStore.cs | 4 +- .../InMemoryBackgroundJobStore.cs | 6 +- .../DemoAppModule.cs | 19 ++++++ .../Program.cs | 11 +++- .../Volo.Abp.BackgroundJobs.DemoApp.csproj | 1 + .../BackgroundJobRecordConsts.cs | 9 +++ .../Volo.Abp.BackgroundJobs.Domain.csproj | 2 + .../BackgroundJobs/BackgroundJobRecord.cs | 56 +++++++++++++++++ .../BackgroundJobs/BackgroundJobStore.cs | 55 ++++++++++++++++ .../BackgroundJobs/BackgroundJobsConsts.cs | 2 +- .../BackgroundJobsDomainAutoMapperProfile.cs | 13 ++++ .../BackgroundJobsDomainModule.cs | 10 ++- .../IBackgroundJobRepository.cs | 12 ++++ .../BackgroundJobsDbContext.cs | 6 +- ...undJobsDbContextModelCreatingExtensions.cs | 29 +++++---- ...BackgroundJobsEntityFrameworkCoreModule.cs | 4 +- .../EfCoreBackgroundJobRepository.cs | 35 +++++++++++ .../IBackgroundJobsDbContext.cs | 9 ++- .../BackgroundJobRepositoryTests.cs | 7 +++ .../MyEntityRepository_Tests.cs | 7 --- .../MongoDB/BackgroundJobRepositoryTests.cs | 7 +++ .../MongoDB/MyEntityRepository_Tests.cs | 7 --- .../BackgroundJobRepository_Tests.cs | 35 +++++++++++ .../BackgroundJobsTestBaseModule.cs | 5 ++ .../BackgroundJobs/BackgroundJobsTestData.cs | 6 +- .../BackgroundJobsTestDataBuilder.cs | 63 ++++++++++++++++--- .../MyEntityRepository_Tests.cs | 16 ----- 29 files changed, 363 insertions(+), 91 deletions(-) create mode 100644 modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs create mode 100644 modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain.Shared/Volo.Abp/BackgroundJobs/BackgroundJobRecordConsts.cs create mode 100644 modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobRecord.cs create mode 100644 modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobStore.cs create mode 100644 modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsDomainAutoMapperProfile.cs create mode 100644 modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/IBackgroundJobRepository.cs create mode 100644 modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/EfCoreBackgroundJobRepository.cs create mode 100644 modules/background-jobs/test/Volo.Abp.BackgroundJobs.EntityFrameworkCore.Tests/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobRepositoryTests.cs delete mode 100644 modules/background-jobs/test/Volo.Abp.BackgroundJobs.EntityFrameworkCore.Tests/Volo.Abp/BackgroundJobs/EntityFrameworkCore/MyEntityRepository_Tests.cs create mode 100644 modules/background-jobs/test/Volo.Abp.BackgroundJobs.MongoDB.Tests/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobRepositoryTests.cs delete mode 100644 modules/background-jobs/test/Volo.Abp.BackgroundJobs.MongoDB.Tests/Volo.Abp/BackgroundJobs/MongoDB/MyEntityRepository_Tests.cs create mode 100644 modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobRepository_Tests.cs delete mode 100644 modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/MyEntityRepository_Tests.cs diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs index 793b993fbc..7fdf9b1fe0 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs @@ -63,7 +63,7 @@ namespace Volo.Abp.BackgroundJobs try { jobExecuteMethod.Invoke(job, new[] { argsObj }); - AsyncHelper.RunSync(() => Store.DeleteAsync(jobInfo)); + AsyncHelper.RunSync(() => Store.DeleteAsync(jobInfo.Id)); } catch (Exception ex) { diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs index 3825196014..49735e83e0 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs @@ -9,18 +9,6 @@ namespace Volo.Abp.BackgroundJobs /// public class BackgroundJobInfo { - /// - /// Maximum length of . - /// Value: 512. - /// - public const int MaxJobTypeLength = 512; - - /// - /// Maximum length of . - /// Value: 1 MB (1,048,576 bytes). - /// - public const int MaxJobArgsLength = 1024 * 1024; - /// /// Default duration (as seconds) for the first wait on a failure. /// Default value: 60 (1 minutes). @@ -46,15 +34,11 @@ namespace Volo.Abp.BackgroundJobs /// Type of the job. /// It's AssemblyQualifiedName of job type. /// - [Required] - [StringLength(MaxJobTypeLength)] public virtual string JobName { get; set; } /// /// Job arguments as JSON string. /// - [Required] - [MaxLength(MaxJobArgsLength)] public virtual string JobArgs { get; set; } //TODO: Consider to conver to byte[] /// diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobStore.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobStore.cs index d1fd307c7a..839156c225 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobStore.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobStore.cs @@ -34,8 +34,8 @@ namespace Volo.Abp.BackgroundJobs /// /// Deletes a job. /// - /// Job information. - Task DeleteAsync(BackgroundJobInfo jobInfo); + /// The Job Unique Identifier. + Task DeleteAsync(Guid jobId); /// /// Updates a job. diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs index 405b96060e..2e3e8879ec 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs @@ -48,9 +48,9 @@ namespace Volo.Abp.BackgroundJobs return Task.FromResult(waitingJobs); } - public Task DeleteAsync(BackgroundJobInfo jobInfo) + public Task DeleteAsync(Guid jobId) { - _jobs.TryRemove(jobInfo.Id, out _); + _jobs.TryRemove(jobId, out _); return Task.FromResult(0); } @@ -59,7 +59,7 @@ namespace Volo.Abp.BackgroundJobs { if (jobInfo.IsAbandoned) { - return DeleteAsync(jobInfo); + return DeleteAsync(jobInfo.Id); } return Task.FromResult(0); diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs new file mode 100644 index 0000000000..0edf72a607 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; +using Volo.Abp.BackgroundJobs.EntityFrameworkCore; +using Volo.Abp.Modularity; + +namespace Volo.Abp.BackgroundJobs.DemoApp +{ + [DependsOn( + typeof(BackgroundJobsEntityFrameworkCoreModule), + typeof(AbpAutofacModule) + )] + public class DemoAppModule : AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddAssemblyOf(); + } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Program.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Program.cs index 41b8c36670..63d8689c33 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Program.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Program.cs @@ -6,7 +6,16 @@ namespace Volo.Abp.BackgroundJobs.DemoApp { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + using (var application = AbpApplicationFactory.Create(options => + { + options.UseAutofac(); + })) + { + application.Initialize(); + + Console.WriteLine("Press ENTER to stop the application..!"); + Console.ReadLine(); + } } } } diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Volo.Abp.BackgroundJobs.DemoApp.csproj b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Volo.Abp.BackgroundJobs.DemoApp.csproj index a1579731b3..c9d46445f1 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Volo.Abp.BackgroundJobs.DemoApp.csproj +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Volo.Abp.BackgroundJobs.DemoApp.csproj @@ -6,6 +6,7 @@ + diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain.Shared/Volo.Abp/BackgroundJobs/BackgroundJobRecordConsts.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain.Shared/Volo.Abp/BackgroundJobs/BackgroundJobRecordConsts.cs new file mode 100644 index 0000000000..f4418bc423 --- /dev/null +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain.Shared/Volo.Abp/BackgroundJobs/BackgroundJobRecordConsts.cs @@ -0,0 +1,9 @@ +namespace Volo.Abp.BackgroundJobs +{ + public static class BackgroundJobRecordConsts + { + public const int MaxJobNameLength = 128; + + public const int MaxJobArgsLength = 1024 * 1024; + } +} diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp.BackgroundJobs.Domain.csproj b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp.BackgroundJobs.Domain.csproj index ddac542c72..82fd254232 100644 --- a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp.BackgroundJobs.Domain.csproj +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp.BackgroundJobs.Domain.csproj @@ -9,7 +9,9 @@ + + diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobRecord.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobRecord.cs new file mode 100644 index 0000000000..11f6168065 --- /dev/null +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobRecord.cs @@ -0,0 +1,56 @@ +using System; +using Volo.Abp.Auditing; +using Volo.Abp.Domain.Entities; + +namespace Volo.Abp.BackgroundJobs +{ + public class BackgroundJobRecord : AggregateRoot, IHasCreationTime + { + /// + /// Type of the job. + /// It's AssemblyQualifiedName of job type. + /// + public virtual string JobName { get; set; } + + /// + /// Job arguments as JSON string. + /// + public virtual string JobArgs { get; set; } //TODO: Consider to conver to byte[] + + /// + /// Try count of this job. + /// A job is re-tried if it fails. + /// + public virtual short TryCount { get; set; } + + /// + /// Creation time of this job. + /// + public virtual DateTime CreationTime { get; set; } + + /// + /// Next try time of this job. + /// + public virtual DateTime NextTryTime { get; set; } + + /// + /// Last try time of this job. + /// + public virtual DateTime? LastTryTime { get; set; } + + /// + /// This is true if this job is continously failed and will not be executed again. + /// + public virtual bool IsAbandoned { get; set; } + + /// + /// Priority of this job. + /// + public virtual BackgroundJobPriority Priority { get; set; } + + public BackgroundJobRecord() + { + + } + } +} diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobStore.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobStore.cs new file mode 100644 index 0000000000..5277b5f4f4 --- /dev/null +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobStore.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using Volo.Abp.ObjectMapping; + +namespace Volo.Abp.BackgroundJobs +{ + public class BackgroundJobStore : IBackgroundJobStore, ITransientDependency + { + protected IBackgroundJobRepository BackgroundJobRepository { get; } + protected IObjectMapper ObjectMapper { get; } + + public BackgroundJobStore( + IBackgroundJobRepository backgroundJobRepository, + IObjectMapper objectMapper) + { + ObjectMapper = objectMapper; + BackgroundJobRepository = backgroundJobRepository; + } + + public async Task FindAsync(Guid jobId) + { + return ObjectMapper.Map( + await BackgroundJobRepository.FindAsync(jobId) + ); + } + + public async Task InsertAsync(BackgroundJobInfo jobInfo) + { + await BackgroundJobRepository.InsertAsync( + ObjectMapper.Map(jobInfo) + ); + } + + public async Task> GetWaitingJobsAsync(int maxResultCount) + { + return ObjectMapper.Map, List>( + await BackgroundJobRepository.GetWaitingListAsync(maxResultCount) + ); + } + + public async Task DeleteAsync(Guid jobId) + { + await BackgroundJobRepository.DeleteAsync(jobId); + } + + public async Task UpdateAsync(BackgroundJobInfo jobInfo) + { + await BackgroundJobRepository.UpdateAsync( + ObjectMapper.Map(jobInfo) + ); + } + } +} diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsConsts.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsConsts.cs index 66c2f4e95b..7f74fa3f4f 100644 --- a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsConsts.cs +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsConsts.cs @@ -2,7 +2,7 @@ { public static class BackgroundJobsConsts { - public const string DefaultDbTablePrefix = "BackgroundJobs"; + public const string DefaultDbTablePrefix = "Abp"; public const string DefaultDbSchema = null; } diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsDomainAutoMapperProfile.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsDomainAutoMapperProfile.cs new file mode 100644 index 0000000000..2ba2bed8cb --- /dev/null +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsDomainAutoMapperProfile.cs @@ -0,0 +1,13 @@ +using AutoMapper; + +namespace Volo.Abp.BackgroundJobs +{ + public class BackgroundJobsDomainAutoMapperProfile : Profile + { + public BackgroundJobsDomainAutoMapperProfile() + { + CreateMap() + .ReverseMap(); + } + } +} diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsDomainModule.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsDomainModule.cs index 03bd1ec01d..3f1d9406a8 100644 --- a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsDomainModule.cs +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/BackgroundJobsDomainModule.cs @@ -1,15 +1,23 @@ using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.AutoMapper; using Volo.Abp.Modularity; namespace Volo.Abp.BackgroundJobs { [DependsOn( - typeof(BackgroundJobsDomainSharedModule) + typeof(BackgroundJobsDomainSharedModule), + typeof(AbpBackgroundJobsModule), + typeof(AbpAutoMapperModule) )] public class BackgroundJobsDomainModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { + context.Services.Configure(options => + { + options.AddProfile(validate: true); + }); + context.Services.AddAssemblyOf(); } } diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/IBackgroundJobRepository.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/IBackgroundJobRepository.cs new file mode 100644 index 0000000000..e12aeff70a --- /dev/null +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.Domain/Volo.Abp/BackgroundJobs/IBackgroundJobRepository.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories; + +namespace Volo.Abp.BackgroundJobs +{ + public interface IBackgroundJobRepository : IBasicRepository + { + Task> GetWaitingListAsync(int maxResultCount); + } +} \ No newline at end of file diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsDbContext.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsDbContext.cs index 1fcda6b4e4..e9e22fc16d 100644 --- a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsDbContext.cs +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsDbContext.cs @@ -4,16 +4,14 @@ using Volo.Abp.EntityFrameworkCore; namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore { - [ConnectionStringName("BackgroundJobs")] + [ConnectionStringName("AbpBackgroundJobs")] public class BackgroundJobsDbContext : AbpDbContext, IBackgroundJobsDbContext { public static string TablePrefix { get; set; } = BackgroundJobsConsts.DefaultDbTablePrefix; public static string Schema { get; set; } = BackgroundJobsConsts.DefaultDbSchema; - /* Add DbSet for each Aggregate Root here. Example: - * public DbSet Questions { get; set; } - */ + public DbSet BackgroundJobs { get; set; } public BackgroundJobsDbContext(DbContextOptions options) : base(options) diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsDbContextModelCreatingExtensions.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsDbContextModelCreatingExtensions.cs index c1964d6fb9..026ed8efe0 100644 --- a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsDbContextModelCreatingExtensions.cs +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsDbContextModelCreatingExtensions.cs @@ -1,6 +1,6 @@ using System; using Microsoft.EntityFrameworkCore; -using Volo.Abp; +using Volo.Abp.EntityFrameworkCore.Modeling; namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore { @@ -15,24 +15,23 @@ namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore var options = new BackgroundJobsModelBuilderConfigurationOptions(); optionsAction?.Invoke(options); + + builder.Entity(b => + { + b.ToTable(options.TablePrefix + "BackgroundJobs", options.Schema); - /* Configure all entities here. Example: + b.ConfigureCreationTime(); - builder.Entity(b => - { - //Configure table & schema name - //b.ToTable(options.TablePrefix + "Questions", options.Schema); + b.Property(x => x.JobName).IsRequired().HasMaxLength(BackgroundJobRecordConsts.MaxJobNameLength); + b.Property(x => x.JobArgs).IsRequired().HasMaxLength(BackgroundJobRecordConsts.MaxJobArgsLength); + b.Property(x => x.TryCount).HasDefaultValue(0); + b.Property(x => x.NextTryTime); + b.Property(x => x.LastTryTime); + b.Property(x => x.IsAbandoned).HasDefaultValue(false); + b.Property(x => x.Priority).HasDefaultValue(BackgroundJobPriority.Normal); - //Properties - //b.Property(q => q.Title).IsRequired().HasMaxLength(QuestionConsts.MaxTitleLength); - - //Configure relations - //b.HasMany(question => question.Tags).WithOne().HasForeignKey(qt => qt.QuestionId); - - //Configure indexes - //b.HasIndex(q => q.CreationTime); + b.HasIndex(x => new { x.IsAbandoned, x.NextTryTime }); }); - */ } } } \ No newline at end of file diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsEntityFrameworkCoreModule.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsEntityFrameworkCoreModule.cs index 972376d08f..edc29096d9 100644 --- a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsEntityFrameworkCoreModule.cs +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobsEntityFrameworkCoreModule.cs @@ -14,9 +14,7 @@ namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore { context.Services.AddAbpDbContext(options => { - /* Add custom repositories here. Example: - * options.AddRepository(); - */ + options.AddRepository(); }); context.Services.AddAssemblyOf(); diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/EfCoreBackgroundJobRepository.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/EfCoreBackgroundJobRepository.cs new file mode 100644 index 0000000000..670c1a6ab4 --- /dev/null +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/EfCoreBackgroundJobRepository.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Volo.Abp.Domain.Repositories.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Timing; + +namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore +{ + public class EfCoreBackgroundJobRepository : EfCoreRepository, IBackgroundJobRepository + { + protected IClock Clock { get; } + + public EfCoreBackgroundJobRepository( + IDbContextProvider dbContextProvider, + IClock clock) + : base(dbContextProvider) + { + Clock = clock; + } + + public async Task> GetWaitingListAsync(int maxResultCount) + { + return await DbSet + .Where(t => !t.IsAbandoned && t.NextTryTime <= Clock.Now) + .OrderByDescending(t => t.Priority) + .ThenBy(t => t.TryCount) + .ThenBy(t => t.NextTryTime) + .Take(maxResultCount) + .ToListAsync(); + } + } +} diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/IBackgroundJobsDbContext.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/IBackgroundJobsDbContext.cs index 716bbad810..5625e07367 100644 --- a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/IBackgroundJobsDbContext.cs +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/IBackgroundJobsDbContext.cs @@ -1,13 +1,12 @@ -using Volo.Abp.Data; +using Microsoft.EntityFrameworkCore; +using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore { - [ConnectionStringName("BackgroundJobs")] + [ConnectionStringName("AbpBackgroundJobs")] public interface IBackgroundJobsDbContext : IEfCoreDbContext { - /* Add DbSet for each Aggregate Root here. Example: - * DbSet Questions { get; } - */ + DbSet BackgroundJobs { get; } } } \ No newline at end of file diff --git a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.EntityFrameworkCore.Tests/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobRepositoryTests.cs b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.EntityFrameworkCore.Tests/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobRepositoryTests.cs new file mode 100644 index 0000000000..e2f4f22476 --- /dev/null +++ b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.EntityFrameworkCore.Tests/Volo.Abp/BackgroundJobs/EntityFrameworkCore/BackgroundJobRepositoryTests.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore +{ + public class BackgroundJobRepositoryTests : BackgroundJobRepository_Tests + { + + } +} diff --git a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.EntityFrameworkCore.Tests/Volo.Abp/BackgroundJobs/EntityFrameworkCore/MyEntityRepository_Tests.cs b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.EntityFrameworkCore.Tests/Volo.Abp/BackgroundJobs/EntityFrameworkCore/MyEntityRepository_Tests.cs deleted file mode 100644 index 9dde34370d..0000000000 --- a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.EntityFrameworkCore.Tests/Volo.Abp/BackgroundJobs/EntityFrameworkCore/MyEntityRepository_Tests.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore -{ - public class MyEntityRepository_Tests : MyEntityRepository_Tests - { - - } -} diff --git a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.MongoDB.Tests/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobRepositoryTests.cs b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.MongoDB.Tests/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobRepositoryTests.cs new file mode 100644 index 0000000000..c6106e777a --- /dev/null +++ b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.MongoDB.Tests/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobRepositoryTests.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.BackgroundJobs.MongoDB +{ + public class BackgroundJobRepositoryTests : BackgroundJobRepository_Tests + { + + } +} diff --git a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.MongoDB.Tests/Volo.Abp/BackgroundJobs/MongoDB/MyEntityRepository_Tests.cs b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.MongoDB.Tests/Volo.Abp/BackgroundJobs/MongoDB/MyEntityRepository_Tests.cs deleted file mode 100644 index c9d386c638..0000000000 --- a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.MongoDB.Tests/Volo.Abp/BackgroundJobs/MongoDB/MyEntityRepository_Tests.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Volo.Abp.BackgroundJobs.MongoDB -{ - public class MyEntityRepository_Tests : MyEntityRepository_Tests - { - - } -} diff --git a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobRepository_Tests.cs b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobRepository_Tests.cs new file mode 100644 index 0000000000..59a2709edd --- /dev/null +++ b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobRepository_Tests.cs @@ -0,0 +1,35 @@ +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Modularity; +using Volo.Abp.Timing; +using Xunit; + +namespace Volo.Abp.BackgroundJobs +{ + public abstract class BackgroundJobRepository_Tests : BackgroundJobsTestBase + where TStartupModule : IAbpModule + { + private readonly IBackgroundJobRepository _backgroundJobRepository; + private readonly IClock _clock; + + protected BackgroundJobRepository_Tests() + { + _backgroundJobRepository = GetRequiredService(); + _clock = GetRequiredService(); + } + + [Theory] + [InlineData(2)] + [InlineData(5)] + public async Task GetWaitingListAsync(int maxResultCount) + { + var backgroundJobs = await _backgroundJobRepository.GetWaitingListAsync(maxResultCount); + + backgroundJobs.Count.ShouldBeGreaterThan(0); + backgroundJobs.Count.ShouldBeLessThanOrEqualTo(maxResultCount); + + backgroundJobs.ForEach(j => j.IsAbandoned.ShouldBeFalse()); + backgroundJobs.ForEach(j => j.NextTryTime.ShouldBeLessThanOrEqualTo(_clock.Now.AddSeconds(1))); //1 second tolerance + } + } +} diff --git a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestBaseModule.cs b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestBaseModule.cs index d1f8145c39..310a819205 100644 --- a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestBaseModule.cs +++ b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestBaseModule.cs @@ -13,6 +13,11 @@ namespace Volo.Abp.BackgroundJobs { public override void ConfigureServices(ServiceConfigurationContext context) { + context.Services.Configure(options => + { + options.IsJobExecutionEnabled = false; + }); + context.Services.AddAssemblyOf(); } diff --git a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestData.cs b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestData.cs index 5d46b25ca9..25a1891d74 100644 --- a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestData.cs +++ b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestData.cs @@ -1,8 +1,12 @@ -using Volo.Abp.DependencyInjection; +using System; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.BackgroundJobs { public class BackgroundJobsTestData : ISingletonDependency { + public Guid JobId1 { get; } = Guid.NewGuid(); + public Guid JobId2 { get; } = Guid.NewGuid(); + public Guid JobId3 { get; } = Guid.NewGuid(); } } diff --git a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestDataBuilder.cs b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestDataBuilder.cs index f9df05c4d7..347db003a2 100644 --- a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestDataBuilder.cs +++ b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/BackgroundJobsTestDataBuilder.cs @@ -1,24 +1,71 @@ -using Volo.Abp.DependencyInjection; -using Volo.Abp.Guids; +using System; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Timing; namespace Volo.Abp.BackgroundJobs { public class BackgroundJobsTestDataBuilder : ITransientDependency { - private readonly IGuidGenerator _guidGenerator; - private BackgroundJobsTestData _testData; + private readonly BackgroundJobsTestData _testData; + private readonly IBackgroundJobRepository _backgroundJobRepository; + private readonly IClock _clock; public BackgroundJobsTestDataBuilder( - IGuidGenerator guidGenerator, - BackgroundJobsTestData testData) + BackgroundJobsTestData testData, + IBackgroundJobRepository backgroundJobRepository, + IClock clock) { - _guidGenerator = guidGenerator; _testData = testData; + _backgroundJobRepository = backgroundJobRepository; + _clock = clock; } public void Build() { - + _backgroundJobRepository.Insert( + new BackgroundJobRecord + { + Id = _testData.JobId1, + JobName = "TestJobName", + JobArgs = "{ value: 1 }", + NextTryTime = _clock.Now.Subtract(TimeSpan.FromMinutes(1)), + Priority = BackgroundJobPriority.Normal, + IsAbandoned = false, + LastTryTime = null, + CreationTime = _clock.Now.Subtract(TimeSpan.FromMinutes(2)), + TryCount = 0 + } + ); + + _backgroundJobRepository.Insert( + new BackgroundJobRecord + { + Id = _testData.JobId2, + JobName = "TestJobName", + JobArgs = "{ value: 2 }", + NextTryTime = _clock.Now.AddMinutes(42), + Priority = BackgroundJobPriority.AboveNormal, + IsAbandoned = true, + LastTryTime = _clock.Now.Subtract(TimeSpan.FromDays(1)), + CreationTime = _clock.Now.Subtract(TimeSpan.FromDays(2)), + TryCount = 3 + } + ); + + _backgroundJobRepository.Insert( + new BackgroundJobRecord + { + Id = _testData.JobId3, + JobName = "TestJobName", + JobArgs = "{ value: 3 }", + NextTryTime = _clock.Now, + Priority = BackgroundJobPriority.BelowNormal, + IsAbandoned = false, + LastTryTime = _clock.Now.Subtract(TimeSpan.FromMinutes(60)), + CreationTime = _clock.Now.Subtract(TimeSpan.FromMinutes(90)), + TryCount = 2 + } + ); } } } \ No newline at end of file diff --git a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/MyEntityRepository_Tests.cs b/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/MyEntityRepository_Tests.cs deleted file mode 100644 index 5053a64a62..0000000000 --- a/modules/background-jobs/test/Volo.Abp.BackgroundJobs.TestBase/Volo.Abp/BackgroundJobs/MyEntityRepository_Tests.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Threading.Tasks; -using Volo.Abp.Modularity; -using Xunit; - -namespace Volo.Abp.BackgroundJobs -{ - public abstract class MyEntityRepository_Tests : BackgroundJobsTestBase - where TStartupModule : IAbpModule - { - [Fact] - public async Task Test1() - { - - } - } -}