mirror of https://github.com/abpframework/abp.git
13 changed files with 157 additions and 58 deletions
@ -0,0 +1,52 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.BackgroundWorkers; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Guids; |
|||
using Volo.Abp.Threading; |
|||
using Volo.Abp.Timing; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs |
|||
{ |
|||
public class BackgroundJobWorker : PeriodicBackgroundWorkerBase, IBackgroundJobWorker, ISingletonDependency |
|||
{ |
|||
/// <summary>
|
|||
/// Interval between polling jobs from <see cref="IBackgroundJobStore"/>.
|
|||
/// Default value: 5000 (5 seconds).
|
|||
/// </summary>
|
|||
public static int JobPollPeriod { get; set; } //TODO: Move to options
|
|||
|
|||
protected IBackgroundJobExecuter JobExecuter { get; } |
|||
protected IBackgroundJobStore Store { get; } |
|||
|
|||
static BackgroundJobWorker() |
|||
{ |
|||
JobPollPeriod = 5000; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="BackgroundJobManager"/> class.
|
|||
/// </summary>
|
|||
public BackgroundJobWorker( |
|||
IBackgroundJobStore store, |
|||
AbpTimer timer, |
|||
IBackgroundJobExecuter jobExecuter) |
|||
: base(timer) |
|||
{ |
|||
JobExecuter = jobExecuter; |
|||
Store = store; |
|||
|
|||
Timer.Period = JobPollPeriod; |
|||
} |
|||
|
|||
protected override void DoWork() |
|||
{ |
|||
var waitingJobs = AsyncHelper.RunSync(() => Store.GetWaitingJobsAsync(1000)); |
|||
|
|||
foreach (var job in waitingJobs) |
|||
{ |
|||
JobExecuter.Execute(job); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.BackgroundWorkers; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs |
|||
{ |
|||
public interface IBackgroundJobWorker : IBackgroundWorker |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs |
|||
{ |
|||
public class BackgroundJobExecuter_Tests : BackgroundJobsTestBase |
|||
{ |
|||
private readonly IBackgroundJobExecuter _backgroundJobExecuter; |
|||
private readonly IBackgroundJobManager _backgroundJobManager; |
|||
private readonly IBackgroundJobStore _backgroundJobStore; |
|||
|
|||
public BackgroundJobExecuter_Tests() |
|||
{ |
|||
_backgroundJobExecuter = GetRequiredService<IBackgroundJobExecuter>(); |
|||
_backgroundJobManager = GetRequiredService<IBackgroundJobManager>(); |
|||
_backgroundJobStore = GetRequiredService<IBackgroundJobStore>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Execute_Tasks() |
|||
{ |
|||
//Arrange
|
|||
|
|||
var jobObject = GetRequiredService<MyJob>(); |
|||
jobObject.ExecutedValues.ShouldBeEmpty(); |
|||
|
|||
var jobId = await _backgroundJobManager.EnqueueAsync(new MyJobArgs("42")); |
|||
|
|||
var job = await _backgroundJobStore.FindAsync(jobId); |
|||
job.ShouldNotBeNull(); |
|||
|
|||
//Act
|
|||
|
|||
_backgroundJobExecuter.Execute(job); |
|||
|
|||
//Assert
|
|||
|
|||
jobObject.ExecutedValues.ShouldContain("42"); |
|||
|
|||
job = await _backgroundJobStore.FindAsync(jobId); |
|||
job.ShouldBeNull(); //Because it's deleted after the execution
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
using System.Collections.Generic; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs |
|||
{ |
|||
public class MyJob : BackgroundJob<MyJobArgs>, ISingletonDependency |
|||
{ |
|||
public List<string> ExecutedValues { get; } = new List<string>(); |
|||
|
|||
public override void Execute(MyJobArgs args) |
|||
{ |
|||
ExecutedValues.Add(args.Value); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
namespace Volo.Abp.BackgroundJobs |
|||
{ |
|||
[BackgroundJobName(Name)] |
|||
public class MyJobArgs |
|||
{ |
|||
public const string Name = "TestJobs.MyJob"; |
|||
|
|||
public string Value { get; set; } |
|||
|
|||
public MyJobArgs() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public MyJobArgs(string value) |
|||
{ |
|||
Value = value; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue