using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Abp.Timing; namespace Abp.BackgroundJobs { /// /// In memory implementation of . /// It's used if is not implemented by actual persistent store /// and job execution is enabled () for the application. /// public class InMemoryBackgroundJobStore : IBackgroundJobStore { private readonly ConcurrentDictionary _jobs; private long _lastId; /// /// Initializes a new instance of the class. /// public InMemoryBackgroundJobStore() { _jobs = new ConcurrentDictionary(); } public Task GetAsync(long jobId) { return Task.FromResult(_jobs[jobId]); } public Task InsertAsync(BackgroundJobInfo jobInfo) { jobInfo.Id = Interlocked.Increment(ref _lastId); _jobs[jobInfo.Id] = jobInfo; return Task.FromResult(0); } public Task> 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); } } }