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) public async Task<List<BackgroundJobRecord>> GetWaitingListAsync(int maxResultCount)
{ {
var now = Clock.Now;
return await DbSet return await DbSet
.Where(t => !t.IsAbandoned && t.NextTryTime <= Clock.Now) .Where(t => !t.IsAbandoned && t.NextTryTime <= now)
.OrderByDescending(t => t.Priority) .OrderByDescending(t => t.Priority)
.ThenBy(t => t.TryCount) .ThenBy(t => t.TryCount)
.ThenBy(t => t.NextTryTime) .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 namespace Volo.Abp.BackgroundJobs.MongoDB
{ {
@ -10,11 +11,10 @@ namespace Volo.Abp.BackgroundJobs.MongoDB
{ {
OneTimeRunner.Run(() => OneTimeRunner.Run(() =>
{ {
//Register mappings here. Example: BsonClassMap.RegisterClassMap<BackgroundJobRecord>(map =>
//BsonClassMap.RegisterClassMap<Question>(map => {
//{ map.AutoMap();
// 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; using Volo.Abp.MongoDB;
namespace Volo.Abp.BackgroundJobs.MongoDB namespace Volo.Abp.BackgroundJobs.MongoDB
{ {
[ConnectionStringName("BackgroundJobs")] [ConnectionStringName("AbpBackgroundJobs")]
public class BackgroundJobsMongoDbContext : AbpMongoDbContext, IBackgroundJobsMongoDbContext public class BackgroundJobsMongoDbContext : AbpMongoDbContext, IBackgroundJobsMongoDbContext
{ {
public static string CollectionPrefix { get; set; } = BackgroundJobsConsts.DefaultDbTablePrefix; public static string CollectionPrefix { get; set; } = BackgroundJobsConsts.DefaultDbTablePrefix;
/* Add mongo collections here. Example: public IMongoCollection<BackgroundJobRecord> BackgroundJobs { get; set; }
* public IMongoCollection<Question> Questions => Collection<Question>();
*/
protected override void CreateModel(IMongoModelBuilder modelBuilder) 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 System;
using Volo.Abp;
using Volo.Abp.MongoDB; using Volo.Abp.MongoDB;
namespace Volo.Abp.BackgroundJobs.MongoDB namespace Volo.Abp.BackgroundJobs.MongoDB
{ {
public static class AbpUsersMongoDbContextExtensions public static class BackgroundJobsMongoDbContextExtensions
{ {
public static void ConfigureBackgroundJobs( public static void ConfigureBackgroundJobs(
this IMongoModelBuilder builder, this IMongoModelBuilder builder,
@ -15,6 +14,11 @@ namespace Volo.Abp.BackgroundJobs.MongoDB
var options = new BackgroundJobsMongoModelBuilderConfigurationOptions(); var options = new BackgroundJobsMongoModelBuilderConfigurationOptions();
optionsAction?.Invoke(options); 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 => context.Services.AddMongoDbContext<BackgroundJobsMongoDbContext>(options =>
{ {
/* Add custom repositories here. Example: options.AddRepository<BackgroundJobRecord, MongoBackgroundJobRepository>();
* options.AddRepository<Question, MongoQuestionRepository>();
*/
}); });
context.Services.AddAssemblyOf<BackgroundJobsMongoDbModule>(); 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; using Volo.Abp.MongoDB;
namespace Volo.Abp.BackgroundJobs.MongoDB namespace Volo.Abp.BackgroundJobs.MongoDB
{ {
[ConnectionStringName("BackgroundJobs")] [ConnectionStringName("AbpBackgroundJobs")]
public interface IBackgroundJobsMongoDbContext : IAbpMongoDbContext public interface IBackgroundJobsMongoDbContext : IAbpMongoDbContext
{ {
/* Define mongo collections here. Example: IMongoCollection<BackgroundJobRecord> BackgroundJobs { get; }
* IMongoCollection<Question> Questions { 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