Merge pull request #14347 from abpframework/liangshiwei/backgroundjob

Switch to the tenant before the Execute Background Job
pull/14366/head
maliming 3 years ago committed by GitHub
commit b43920addd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ExceptionHandling;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.BackgroundJobs;
@ -14,9 +15,12 @@ public class BackgroundJobExecuter : IBackgroundJobExecuter, ITransientDependenc
public ILogger<BackgroundJobExecuter> Logger { protected get; set; }
protected AbpBackgroundJobOptions Options { get; }
protected ICurrentTenant CurrentTenant { get; }
public BackgroundJobExecuter(IOptions<AbpBackgroundJobOptions> options)
public BackgroundJobExecuter(IOptions<AbpBackgroundJobOptions> options, ICurrentTenant currentTenant)
{
CurrentTenant = currentTenant;
Options = options.Value;
Logger = NullLogger<BackgroundJobExecuter>.Instance;
@ -40,14 +44,18 @@ public class BackgroundJobExecuter : IBackgroundJobExecuter, ITransientDependenc
try
{
if (jobExecuteMethod.Name == nameof(IAsyncBackgroundJob<object>.ExecuteAsync))
using(CurrentTenant.Change(GetJobArgsTenantId(context.JobArgs)))
{
await ((Task)jobExecuteMethod.Invoke(job, new[] { context.JobArgs }));
}
else
{
jobExecuteMethod.Invoke(job, new[] { context.JobArgs });
if (jobExecuteMethod.Name == nameof(IAsyncBackgroundJob<object>.ExecuteAsync))
{
await ((Task)jobExecuteMethod.Invoke(job, new[] { context.JobArgs }));
}
else
{
jobExecuteMethod.Invoke(job, new[] { context.JobArgs });
}
}
}
catch (Exception ex)
{
@ -64,4 +72,13 @@ public class BackgroundJobExecuter : IBackgroundJobExecuter, ITransientDependenc
};
}
}
protected virtual Guid? GetJobArgsTenantId(object jobArgs)
{
return jobArgs switch
{
IMultiTenant multiTenantJobArgs => multiTenantJobArgs.TenantId,
_ => CurrentTenant.Id
};
}
}

@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
@ -58,4 +59,36 @@ public class BackgroundJobExecuter_Tests : BackgroundJobsTestBase
jobObject.ExecutedValues.ShouldContain("42");
}
[Fact]
public async Task Should_Change_TenantId_If_EventData_Is_MultiTenant()
{
//Arrange
var tenantId = Guid.NewGuid();
var jobObject = GetRequiredService<MyJob>();
var asyncJobObject = GetRequiredService<MyAsyncJob>();
//Act
await _backgroundJobExecuter.ExecuteAsync(
new JobExecutionContext(
ServiceProvider,
typeof(MyJob),
new MyJobArgs("42", tenantId)
)
);
await _backgroundJobExecuter.ExecuteAsync(
new JobExecutionContext(
ServiceProvider,
typeof(MyAsyncJob),
new MyAsyncJobArgs("42", tenantId)
)
);
//Assert
jobObject.TenantId.ShouldBe(tenantId);
asyncJobObject.TenantId.ShouldBe(tenantId);
}
}

@ -2,17 +2,27 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.BackgroundJobs;
public class MyAsyncJob : AsyncBackgroundJob<MyAsyncJobArgs>, ISingletonDependency
{
public List<string> ExecutedValues { get; } = new List<string>();
public Guid? TenantId { get; set; }
private readonly ICurrentTenant _currentTenant;
public MyAsyncJob(ICurrentTenant currentTenant)
{
_currentTenant = currentTenant;
}
public override Task ExecuteAsync(MyAsyncJobArgs args)
{
ExecutedValues.Add(args.Value);
TenantId = _currentTenant.Id;
return Task.CompletedTask;
}
}

@ -1,16 +1,22 @@
namespace Volo.Abp.BackgroundJobs;
using System;
using Volo.Abp.MultiTenancy;
public class MyAsyncJobArgs
namespace Volo.Abp.BackgroundJobs;
public class MyAsyncJobArgs: IMultiTenant
{
public string Value { get; set; }
public Guid? TenantId { get; }
public MyAsyncJobArgs()
{
}
public MyAsyncJobArgs(string value)
public MyAsyncJobArgs(string value, Guid? tenantId = null)
{
Value = value;
TenantId = tenantId;
}
}

@ -1,14 +1,26 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.BackgroundJobs;
public class MyJob : BackgroundJob<MyJobArgs>, ISingletonDependency
{
public List<string> ExecutedValues { get; } = new List<string>();
public Guid? TenantId { get; set; }
private readonly ICurrentTenant _currentTenant;
public MyJob(ICurrentTenant currentTenant)
{
_currentTenant = currentTenant;
}
public override void Execute(MyJobArgs args)
{
ExecutedValues.Add(args.Value);
TenantId = _currentTenant.Id;
}
}

@ -1,6 +1,9 @@
namespace Volo.Abp.BackgroundJobs;
using System;
using Volo.Abp.MultiTenancy;
public class MyJobArgs
namespace Volo.Abp.BackgroundJobs;
public class MyJobArgs : IMultiTenant
{
public string Value { get; set; }
@ -8,9 +11,13 @@ public class MyJobArgs
{
}
public MyJobArgs(string value)
public MyJobArgs(string value, Guid? tenantId = null)
{
Value = value;
TenantId = tenantId;
}
public Guid? TenantId { get; }
}

Loading…
Cancel
Save