|
|
|
@ -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
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|