Browse Source

Implemented mongodb background job module

pull/395/head
Halil ibrahim Kalkan 8 years ago
parent
commit
ae647e7757
  1. 3
      modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/EfCoreBackgroundJobRepository.cs
  2. 12
      modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobsBsonClassMap.cs
  3. 9
      modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobsMongoDbContext.cs
  4. 8
      modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobsMongoDbContextExtensions.cs
  5. 4
      modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobsMongoDbModule.cs
  6. 9
      modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/IBackgroundJobsMongoDbContext.cs
  7. 36
      modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/MongoBackgroundJobRepository.cs

3
modules/background-jobs/src/Volo.Abp.BackgroundJobs.EntityFrameworkCore/Volo.Abp/BackgroundJobs/EntityFrameworkCore/EfCoreBackgroundJobRepository.cs

@ -23,8 +23,9 @@ namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore
public async Task<List<BackgroundJobRecord>> GetWaitingListAsync(int maxResultCount)
{
var now = Clock.Now;
return await DbSet
.Where(t => !t.IsAbandoned && t.NextTryTime <= Clock.Now)
.Where(t => !t.IsAbandoned && t.NextTryTime <= now)
.OrderByDescending(t => t.Priority)
.ThenBy(t => t.TryCount)
.ThenBy(t => t.NextTryTime)

12
modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobsBsonClassMap.cs

@ -1,4 +1,5 @@
using Volo.Abp.Threading;
using MongoDB.Bson.Serialization;
using Volo.Abp.Threading;
namespace Volo.Abp.BackgroundJobs.MongoDB
{
@ -10,11 +11,10 @@ namespace Volo.Abp.BackgroundJobs.MongoDB
{
OneTimeRunner.Run(() =>
{
//Register mappings here. Example:
//BsonClassMap.RegisterClassMap<Question>(map =>
//{
// map.AutoMap();
//});
BsonClassMap.RegisterClassMap<BackgroundJobRecord>(map =>
{
map.AutoMap();
});
});
}
}

9
modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobsMongoDbContext.cs

@ -1,16 +1,15 @@
using Volo.Abp.Data;
using MongoDB.Driver;
using Volo.Abp.Data;
using Volo.Abp.MongoDB;
namespace Volo.Abp.BackgroundJobs.MongoDB
{
[ConnectionStringName("BackgroundJobs")]
[ConnectionStringName("AbpBackgroundJobs")]
public class BackgroundJobsMongoDbContext : AbpMongoDbContext, IBackgroundJobsMongoDbContext
{
public static string CollectionPrefix { get; set; } = BackgroundJobsConsts.DefaultDbTablePrefix;
/* Add mongo collections here. Example:
* public IMongoCollection<Question> Questions => Collection<Question>();
*/
public IMongoCollection<BackgroundJobRecord> BackgroundJobs { get; set; }
protected override void CreateModel(IMongoModelBuilder modelBuilder)
{

8
modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/AbpUsersMongoDbContextExtensions.cs → modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobsMongoDbContextExtensions.cs

@ -1,10 +1,9 @@
using System;
using Volo.Abp;
using Volo.Abp.MongoDB;
namespace Volo.Abp.BackgroundJobs.MongoDB
{
public static class AbpUsersMongoDbContextExtensions
public static class BackgroundJobsMongoDbContextExtensions
{
public static void ConfigureBackgroundJobs(
this IMongoModelBuilder builder,
@ -15,6 +14,11 @@ namespace Volo.Abp.BackgroundJobs.MongoDB
var options = new BackgroundJobsMongoModelBuilderConfigurationOptions();
optionsAction?.Invoke(options);
builder.Entity<BackgroundJobRecord>(b =>
{
b.CollectionName = options.CollectionPrefix + "BackgroundJobs";
});
}
}
}

4
modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/BackgroundJobsMongoDbModule.cs

@ -16,9 +16,7 @@ namespace Volo.Abp.BackgroundJobs.MongoDB
context.Services.AddMongoDbContext<BackgroundJobsMongoDbContext>(options =>
{
/* Add custom repositories here. Example:
* options.AddRepository<Question, MongoQuestionRepository>();
*/
options.AddRepository<BackgroundJobRecord, MongoBackgroundJobRepository>();
});
context.Services.AddAssemblyOf<BackgroundJobsMongoDbModule>();

9
modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/IBackgroundJobsMongoDbContext.cs

@ -1,13 +1,12 @@
using Volo.Abp.Data;
using MongoDB.Driver;
using Volo.Abp.Data;
using Volo.Abp.MongoDB;
namespace Volo.Abp.BackgroundJobs.MongoDB
{
[ConnectionStringName("BackgroundJobs")]
[ConnectionStringName("AbpBackgroundJobs")]
public interface IBackgroundJobsMongoDbContext : IAbpMongoDbContext
{
/* Define mongo collections here. Example:
* IMongoCollection<Question> Questions { get; }
*/
IMongoCollection<BackgroundJobRecord> BackgroundJobs { get; }
}
}

36
modules/background-jobs/src/Volo.Abp.BackgroundJobs.MongoDB/Volo.Abp/BackgroundJobs/MongoDB/MongoBackgroundJobRepository.cs

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.MongoDB;
using Volo.Abp.Timing;
namespace Volo.Abp.BackgroundJobs.MongoDB
{
public class MongoBackgroundJobRepository : MongoDbRepository<IBackgroundJobsMongoDbContext, BackgroundJobRecord, Guid>, IBackgroundJobRepository
{
protected IClock Clock { get; }
public MongoBackgroundJobRepository(
IMongoDbContextProvider<IBackgroundJobsMongoDbContext> dbContextProvider,
IClock clock)
: base(dbContextProvider)
{
Clock = clock;
}
public async Task<List<BackgroundJobRecord>> GetWaitingListAsync(int maxResultCount)
{
var now = Clock.Now;
return await GetMongoQueryable()
.Where(t => !t.IsAbandoned && t.NextTryTime <= now)
.OrderByDescending(t => t.Priority)
.ThenBy(t => t.TryCount)
.ThenBy(t => t.NextTryTime)
.Take(maxResultCount)
.ToListAsync();
}
}
}
Loading…
Cancel
Save