mirror of https://github.com/abpframework/abp.git
4 changed files with 70 additions and 41 deletions
@ -0,0 +1,68 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Timing; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs |
|||
{ |
|||
public class InMemoryBackgroundJobStore : IBackgroundJobStore, ISingletonDependency |
|||
{ |
|||
private readonly ConcurrentDictionary<Guid, BackgroundJobInfo> _jobs; |
|||
|
|||
protected IClock Clock { get; } |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="InMemoryBackgroundJobStore"/> class.
|
|||
/// </summary>
|
|||
public InMemoryBackgroundJobStore(IClock clock) |
|||
{ |
|||
Clock = clock; |
|||
_jobs = new ConcurrentDictionary<Guid, BackgroundJobInfo>(); |
|||
} |
|||
|
|||
public Task<BackgroundJobInfo> FindAsync(Guid jobId) |
|||
{ |
|||
return Task.FromResult(_jobs[jobId]); |
|||
} |
|||
|
|||
public Task InsertAsync(BackgroundJobInfo jobInfo) |
|||
{ |
|||
_jobs[jobInfo.Id] = jobInfo; |
|||
|
|||
return Task.FromResult(0); |
|||
} |
|||
|
|||
public Task<List<BackgroundJobInfo>> GetWaitingJobsAsync(int maxResultCount) |
|||
{ |
|||
var waitingJobs = _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(); |
|||
|
|||
return Task.FromResult(waitingJobs); |
|||
} |
|||
|
|||
public Task DeleteAsync(BackgroundJobInfo jobInfo) |
|||
{ |
|||
_jobs.TryRemove(jobInfo.Id, out _); |
|||
|
|||
return Task.FromResult(0); |
|||
} |
|||
|
|||
public Task UpdateAsync(BackgroundJobInfo jobInfo) |
|||
{ |
|||
if (jobInfo.IsAbandoned) |
|||
{ |
|||
return DeleteAsync(jobInfo); |
|||
} |
|||
|
|||
return Task.FromResult(0); |
|||
} |
|||
} |
|||
} |
|||
@ -1,39 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs |
|||
{ |
|||
/// <summary>
|
|||
/// Null pattern implementation of <see cref="IBackgroundJobStore"/>.
|
|||
/// It's used if <see cref="IBackgroundJobStore"/> is not implemented by actual persistent store
|
|||
/// and job execution is not enabled (<see cref="IBackgroundJobOptions.IsJobExecutionEnabled"/>) for the application.
|
|||
/// </summary>
|
|||
public class NullBackgroundJobStore : IBackgroundJobStore |
|||
{ |
|||
public Task<BackgroundJobInfo> GetAsync(Guid jobId) |
|||
{ |
|||
return Task.FromResult((BackgroundJobInfo)null); |
|||
} |
|||
|
|||
public Task InsertAsync(BackgroundJobInfo jobInfo) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task<List<BackgroundJobInfo>> GetWaitingJobsAsync(int maxResultCount) |
|||
{ |
|||
return Task.FromResult(new List<BackgroundJobInfo>()); |
|||
} |
|||
|
|||
public Task DeleteAsync(BackgroundJobInfo jobInfo) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task UpdateAsync(BackgroundJobInfo jobInfo) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue