From 4ce0381da020e852d76ec6275afefeca03570fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 9 Jul 2019 15:04:07 +0300 Subject: [PATCH] Fixed #1464: Add sync methods to IBackgroundJobStore --- .../Authorization/AuthorizationInterceptor.cs | 4 +-- .../Abp/BackgroundJobs/BackgroundJobWorker.cs | 13 +++---- .../Abp/BackgroundJobs/IBackgroundJobStore.cs | 34 ++++++++++++++++++ .../InMemoryBackgroundJobStore.cs | 36 ++++++++++++++++++- .../Abp/Validation/ValidationInterceptor.cs | 5 +-- .../Abp/BackgroundJobs/BackgroundJobStore.cs | 33 +++++++++++++++++ .../IBackgroundJobRepository.cs | 2 ++ .../EfCoreBackgroundJobRepository.cs | 17 +++++++-- .../MongoDB/MongoBackgroundJobRepository.cs | 17 +++++++-- 9 files changed, 140 insertions(+), 21 deletions(-) diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptor.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptor.cs index 7c89bcbe113..45cb2ec39d8 100644 --- a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptor.cs +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/AuthorizationInterceptor.cs @@ -39,9 +39,9 @@ public override async Task InterceptAsync(IAbpMethodInvocation invocation) await invocation.ProceedAsync(); } - protected virtual Task AuthorizeAsync(IAbpMethodInvocation invocation) + protected virtual async Task AuthorizeAsync(IAbpMethodInvocation invocation) { - return _methodInvocationAuthorizationService.CheckAsync( + await _methodInvocationAuthorizationService.CheckAsync( new MethodInvocationAuthorizationContext( invocation.Method ) diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs index b2a66f6fc4a..4c9efbe6d89 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs @@ -36,9 +36,7 @@ protected override void DoWork() { var store = scope.ServiceProvider.GetRequiredService(); - var waitingJobs = AsyncHelper.RunSync( - () => store.GetWaitingJobsAsync(WorkerOptions.MaxJobFetchCount) - ); + var waitingJobs = store.GetWaitingJobs(WorkerOptions.MaxJobFetchCount); if (!waitingJobs.Any()) { @@ -64,7 +62,7 @@ protected override void DoWork() { jobExecuter.Execute(context); - AsyncHelper.RunSync(() => store.DeleteAsync(jobInfo.Id)); + store.Delete(jobInfo.Id); } catch (BackgroundJobExecutionException) { @@ -96,7 +94,7 @@ protected virtual void TryUpdate(IBackgroundJobStore store, BackgroundJobInfo jo { try { - store.UpdateAsync(jobInfo); + store.Update(jobInfo); } catch (Exception updateEx) { @@ -107,9 +105,8 @@ protected virtual void TryUpdate(IBackgroundJobStore store, BackgroundJobInfo jo protected virtual DateTime? CalculateNextTryTime(BackgroundJobInfo jobInfo, IClock clock) { var nextWaitDuration = WorkerOptions.DefaultFirstWaitDuration * (Math.Pow(WorkerOptions.DefaultWaitFactor, jobInfo.TryCount - 1)); - var nextTryDate = jobInfo.LastTryTime.HasValue - ? jobInfo.LastTryTime.Value.AddSeconds(nextWaitDuration) - : clock.Now.AddSeconds(nextWaitDuration); + var nextTryDate = jobInfo.LastTryTime?.AddSeconds(nextWaitDuration) ?? + clock.Now.AddSeconds(nextWaitDuration); if (nextTryDate.Subtract(jobInfo.CreationTime).TotalSeconds > WorkerOptions.DefaultTimeout) { 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 839156c2256..f909b3846d0 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobStore.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/IBackgroundJobStore.cs @@ -9,6 +9,13 @@ namespace Volo.Abp.BackgroundJobs /// public interface IBackgroundJobStore { + /// + /// Gets a BackgroundJobInfo based on the given jobId. + /// + /// The Job Unique Identifier. + /// The BackgroundJobInfo object. + BackgroundJobInfo Find(Guid jobId); + /// /// Gets a BackgroundJobInfo based on the given jobId. /// @@ -16,12 +23,27 @@ public interface IBackgroundJobStore /// The BackgroundJobInfo object. Task FindAsync(Guid jobId); + /// + /// Inserts a background job. + /// + /// Job information. + void Insert(BackgroundJobInfo jobInfo); + /// /// Inserts a background job. /// /// Job information. Task InsertAsync(BackgroundJobInfo jobInfo); + /// + /// Gets waiting jobs. It should get jobs based on these: + /// Conditions: !IsAbandoned And NextTryTime <= Clock.Now. + /// Order by: Priority DESC, TryCount ASC, NextTryTime ASC. + /// Maximum result: . + /// + /// Maximum result count. + List GetWaitingJobs(int maxResultCount); + /// /// Gets waiting jobs. It should get jobs based on these: /// Conditions: !IsAbandoned And NextTryTime <= Clock.Now. @@ -31,12 +53,24 @@ public interface IBackgroundJobStore /// Maximum result count. Task> GetWaitingJobsAsync(int maxResultCount); + /// + /// Deletes a job. + /// + /// The Job Unique Identifier. + void Delete(Guid jobId); + /// /// Deletes a job. /// /// The Job Unique Identifier. Task DeleteAsync(Guid jobId); + /// + /// Updates a job. + /// + /// Job information. + void Update(BackgroundJobInfo jobInfo); + /// /// 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 1dc6d7c7000..30dd7fe0904 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs @@ -23,11 +23,21 @@ public InMemoryBackgroundJobStore(IClock clock) _jobs = new ConcurrentDictionary(); } + public BackgroundJobInfo Find(Guid jobId) + { + return _jobs.GetOrDefault(jobId); + } + public virtual Task FindAsync(Guid jobId) { return Task.FromResult(_jobs.GetOrDefault(jobId)); } - + + public void Insert(BackgroundJobInfo jobInfo) + { + _jobs[jobInfo.Id] = jobInfo; + } + public virtual Task InsertAsync(BackgroundJobInfo jobInfo) { _jobs[jobInfo.Id] = jobInfo; @@ -35,6 +45,17 @@ public virtual Task InsertAsync(BackgroundJobInfo jobInfo) return Task.FromResult(0); } + public List GetWaitingJobs(int maxResultCount) + { + return _jobs.Values + .Where(t => !t.IsAbandoned && t.NextTryTime <= Clock.Now) + .OrderByDescending(t => t.Priority) + .ThenBy(t => t.TryCount) + .ThenBy(t => t.NextTryTime) + .Take(maxResultCount) + .ToList(); + } + public virtual Task> GetWaitingJobsAsync(int maxResultCount) { var waitingJobs = _jobs.Values @@ -48,6 +69,11 @@ public virtual Task> GetWaitingJobsAsync(int maxResultCo return Task.FromResult(waitingJobs); } + public void Delete(Guid jobId) + { + _jobs.TryRemove(jobId, out _); + } + public virtual Task DeleteAsync(Guid jobId) { _jobs.TryRemove(jobId, out _); @@ -55,6 +81,14 @@ public virtual Task DeleteAsync(Guid jobId) return Task.FromResult(0); } + public void Update(BackgroundJobInfo jobInfo) + { + if (jobInfo.IsAbandoned) + { + DeleteAsync(jobInfo.Id); + } + } + public virtual Task UpdateAsync(BackgroundJobInfo jobInfo) { if (jobInfo.IsAbandoned) diff --git a/framework/src/Volo.Abp.Validation/Volo/Abp/Validation/ValidationInterceptor.cs b/framework/src/Volo.Abp.Validation/Volo/Abp/Validation/ValidationInterceptor.cs index c7fedec8db6..594a19535ec 100644 --- a/framework/src/Volo.Abp.Validation/Volo/Abp/Validation/ValidationInterceptor.cs +++ b/framework/src/Volo.Abp.Validation/Volo/Abp/Validation/ValidationInterceptor.cs @@ -1,7 +1,4 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using Volo.Abp.Aspects; using Volo.Abp.DependencyInjection; using Volo.Abp.DynamicProxy; 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 index fed3718f857..eda224153f7 100644 --- 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 @@ -19,6 +19,13 @@ public BackgroundJobStore( BackgroundJobRepository = backgroundJobRepository; } + public BackgroundJobInfo Find(Guid jobId) + { + return ObjectMapper.Map( + BackgroundJobRepository.Find(jobId) + ); + } + public virtual async Task FindAsync(Guid jobId) { return ObjectMapper.Map( @@ -26,6 +33,13 @@ await BackgroundJobRepository.FindAsync(jobId) ); } + public void Insert(BackgroundJobInfo jobInfo) + { + BackgroundJobRepository.Insert( + ObjectMapper.Map(jobInfo) + ); + } + public virtual async Task InsertAsync(BackgroundJobInfo jobInfo) { await BackgroundJobRepository.InsertAsync( @@ -33,6 +47,13 @@ await BackgroundJobRepository.InsertAsync( ); } + public List GetWaitingJobs(int maxResultCount) + { + return ObjectMapper.Map, List>( + BackgroundJobRepository.GetWaitingList(maxResultCount) + ); + } + public virtual async Task> GetWaitingJobsAsync(int maxResultCount) { return ObjectMapper.Map, List>( @@ -40,11 +61,23 @@ await BackgroundJobRepository.GetWaitingListAsync(maxResultCount) ); } + public void Delete(Guid jobId) + { + BackgroundJobRepository.Delete(jobId); + } + public virtual async Task DeleteAsync(Guid jobId) { await BackgroundJobRepository.DeleteAsync(jobId); } + public void Update(BackgroundJobInfo jobInfo) + { + BackgroundJobRepository.Update( + ObjectMapper.Map(jobInfo) + ); + } + public virtual async Task UpdateAsync(BackgroundJobInfo jobInfo) { await BackgroundJobRepository.UpdateAsync( 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 index e12aeff70a3..a34cbd6abbc 100644 --- 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 @@ -7,6 +7,8 @@ namespace Volo.Abp.BackgroundJobs { public interface IBackgroundJobRepository : IBasicRepository { + List GetWaitingList(int maxResultCount); + 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/EfCoreBackgroundJobRepository.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo/Abp/BackgroundJobs/EntityFrameworkCore/EfCoreBackgroundJobRepository.cs index 28d86f589ac..8128d18bfbc 100644 --- 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 @@ -21,16 +21,27 @@ public EfCoreBackgroundJobRepository( Clock = clock; } + public List GetWaitingList(int maxResultCount) + { + return GetWaitingListQuery(maxResultCount) + .ToList(); + } + public async Task> GetWaitingListAsync(int maxResultCount) + { + return await GetWaitingListQuery(maxResultCount) + .ToListAsync(); + } + + private IQueryable GetWaitingListQuery(int maxResultCount) { var now = Clock.Now; - return await DbSet + return DbSet .Where(t => !t.IsAbandoned && t.NextTryTime <= now) .OrderByDescending(t => t.Priority) .ThenBy(t => t.TryCount) .ThenBy(t => t.NextTryTime) - .Take(maxResultCount) - .ToListAsync(); + .Take(maxResultCount); } } } diff --git a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo/Abp/BackgroundJobs/MongoDB/MongoBackgroundJobRepository.cs b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo/Abp/BackgroundJobs/MongoDB/MongoBackgroundJobRepository.cs index 78a1238aa84..fe3ffa7e072 100644 --- a/modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo/Abp/BackgroundJobs/MongoDB/MongoBackgroundJobRepository.cs +++ b/modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo/Abp/BackgroundJobs/MongoDB/MongoBackgroundJobRepository.cs @@ -21,16 +21,27 @@ public MongoBackgroundJobRepository( Clock = clock; } + public List GetWaitingList(int maxResultCount) + { + return GetWaitingListQuery(maxResultCount) + .ToList(); + } + public async Task> GetWaitingListAsync(int maxResultCount) + { + return await GetWaitingListQuery(maxResultCount) + .ToListAsync(); + } + + private IMongoQueryable GetWaitingListQuery(int maxResultCount) { var now = Clock.Now; - return await GetMongoQueryable() + return GetMongoQueryable() .Where(t => !t.IsAbandoned && t.NextTryTime <= now) .OrderByDescending(t => t.Priority) .ThenBy(t => t.TryCount) .ThenBy(t => t.NextTryTime) - .Take(maxResultCount) - .ToListAsync(); + .Take(maxResultCount); } } }