Skip to content

Commit

Permalink
Merge pull request #6811 from aspnetboilerplate/feat/6647
Browse files Browse the repository at this point in the history
Create MaxWaitingJobToProcessPerPeriod for background job configuration
  • Loading branch information
ismcagdas committed Oct 26, 2023
2 parents 18a21e7 + 775151e commit be07045
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
19 changes: 18 additions & 1 deletion doc/WebSite/Background-Jobs-And-Workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ other problems. To prevent it, you have two options:
application and create a separated, standalone application (example:
a Windows Service) that executes background jobs.

#### User token removal period
##### User token removal period

ABP Framework defines a background worker named UserTokenExpirationWorker which cleans the records in table AbpUserTokens. If you disable background job execution, this worker will not run. By default, UserTokenExpirationWorker runs every one hour. If you want to change this period, you can configure it like below:

Expand All @@ -239,6 +239,23 @@ ABP Framework defines a background worker named UserTokenExpirationWorker which

//...
}

##### Max Waiting Jobs To Process

By default, ABP Framework processes 1000 waiting background jobs. You can change it using the configuration below;

```csharp
public class MyProjectWebModule : AbpModule
{
public override void PreInitialize()
{
Configuration.BackgroundJobs.MaxWaitingJobToProcessPerPeriod = 10;
}

//...
}
```

#### Exception Handling

Since the default background job manager should re-try failed jobs, it
Expand Down
2 changes: 1 addition & 1 deletion src/Abp/Authorization/SimplePermissionDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SimplePermissionDependency : IPermissionDependency
/// Default: false.
/// </summary>
public bool RequiresAll { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="SimplePermissionDependency"/> class.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Abp/BackgroundJobs/BackgroundJobConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ namespace Abp.BackgroundJobs
{
internal class BackgroundJobConfiguration : IBackgroundJobConfiguration
{
public static int DefaultMaxWaitingJobToProcessPerPeriod = 1000;

public bool IsJobExecutionEnabled { get; set; }

[Obsolete("Use UserTokenExpirationPeriod instead.")]
public int? CleanUserTokenPeriod { get; set; }

public TimeSpan? UserTokenExpirationPeriod { get; set; }

public int MaxWaitingJobToProcessPerPeriod { get; set; } = DefaultMaxWaitingJobToProcessPerPeriod;

public IAbpStartupConfiguration AbpConfiguration { get; private set; }

public BackgroundJobConfiguration(IAbpStartupConfiguration abpConfiguration)
Expand Down
13 changes: 8 additions & 5 deletions src/Abp/BackgroundJobs/BackgroundJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class BackgroundJobManager : AsyncPeriodicBackgroundWorkerBase, IBackgrou

private readonly IIocResolver _iocResolver;
private readonly IBackgroundJobStore _store;
private readonly IBackgroundJobConfiguration _backgroundJobConfiguration;

static BackgroundJobManager()
{
Expand All @@ -40,17 +41,19 @@ static BackgroundJobManager()
public BackgroundJobManager(
IIocResolver iocResolver,
IBackgroundJobStore store,
IBackgroundJobConfiguration backgroundJobConfiguration,
AbpAsyncTimer timer)
: base(timer)
{
_store = store;
_backgroundJobConfiguration = backgroundJobConfiguration;
_iocResolver = iocResolver;

EventBus = NullEventBus.Instance;

Timer.Period = JobPollPeriod;
}

public virtual async Task<string> EnqueueAsync<TJob, TArgs>(TArgs args,
BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null)
where TJob : IBackgroundJobBase<TArgs>
Expand Down Expand Up @@ -80,7 +83,7 @@ static BackgroundJobManager()

return jobInfoId;
}

public virtual string Enqueue<TJob, TArgs>(TArgs args,
BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null)
where TJob : IBackgroundJobBase<TArgs>
Expand All @@ -105,7 +108,7 @@ static BackgroundJobManager()
CurrentUnitOfWork.SaveChanges();

jobInfoId = jobInfo.Id.ToString();

uow.Complete();
}

Expand Down Expand Up @@ -140,7 +143,7 @@ public bool Delete(string jobId)

protected override async Task DoWorkAsync()
{
var waitingJobs = await _store.GetWaitingJobsAsync(1000);
var waitingJobs = await _store.GetWaitingJobsAsync(_backgroundJobConfiguration.MaxWaitingJobToProcessPerPeriod);

foreach (var job in waitingJobs)
{
Expand Down Expand Up @@ -240,4 +243,4 @@ private async Task TryUpdateAsync(BackgroundJobInfo jobInfo)
}
}
}
}
}
6 changes: 6 additions & 0 deletions src/Abp/BackgroundJobs/IBackgroundJobConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public interface IBackgroundJobConfiguration
/// </summary>
TimeSpan? UserTokenExpirationPeriod { get; set; }


/// <summary>
/// Maximum number of waiting jobs to process per period.
/// </summary>
int MaxWaitingJobToProcessPerPeriod { get; set; }

/// <summary>
/// Gets the ABP configuration object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System.Threading.Tasks;
using Abp.BackgroundJobs;
using Abp.Timing;
using NSubstitute;
using Shouldly;
using Xunit;

namespace Abp.Tests.BackgroundJobs
{
public class InMemoryBackgroundJobStore_Tests
public class InMemoryBackgroundJobStore_Tests: TestBaseWithLocalIocManager
{
private readonly InMemoryBackgroundJobStore _store;

public InMemoryBackgroundJobStore_Tests()
{
_store = new InMemoryBackgroundJobStore();
Expand All @@ -26,15 +27,15 @@ public async Task Test_All()
};

await _store.InsertAsync(jobInfo);
(await _store.GetWaitingJobsAsync(1000)).Count.ShouldBe(1);
(await _store.GetWaitingJobsAsync(BackgroundJobConfiguration.DefaultMaxWaitingJobToProcessPerPeriod)).Count.ShouldBe(1);

var jobInfoFromStore = await _store.GetAsync(1);
jobInfoFromStore.ShouldNotBeNull();
jobInfoFromStore.JobType.ShouldBeSameAs(jobInfo.JobType);
jobInfoFromStore.JobArgs.ShouldBeSameAs(jobInfo.JobArgs);

await _store.DeleteAsync(jobInfo);
(await _store.GetWaitingJobsAsync(1000)).Count.ShouldBe(0);
(await _store.GetWaitingJobsAsync(BackgroundJobConfiguration.DefaultMaxWaitingJobToProcessPerPeriod)).Count.ShouldBe(0);
}
}
}

0 comments on commit be07045

Please sign in to comment.