Browse Source

Merge pull request #1364 from colinin/remove-unnecessary-service-scope

feat(background-tasks): Remove unnecessary service scopes
pull/1366/head
yx lin 5 months ago
committed by GitHub
parent
commit
e79ceb4beb
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Quartz/LINGYUN/Abp/BackgroundTasks/Quartz/QuartzJobCreator.cs
  2. 23
      aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks/LINGYUN/Abp/BackgroundTasks/BackgroundJobAdapter.cs
  3. 2
      aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks/LINGYUN/Abp/BackgroundTasks/BackgroundWorkerAdapter.cs

9
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks.Quartz/LINGYUN/Abp/BackgroundTasks/Quartz/QuartzJobCreator.cs

@ -38,13 +38,10 @@ public class QuartzJobCreator : IQuartzJobCreator, ISingletonDependency
// 运行时搜寻本地作业 // 运行时搜寻本地作业
jobType = typeof(QuartzJobSearchJobAdapter); jobType = typeof(QuartzJobSearchJobAdapter);
} }
else else if (!typeof(IJob).IsAssignableFrom(jobType))
{ {
if (!typeof(IJob).IsAssignableFrom(jobType)) var adapterType = typeof(QuartzJobSimpleAdapter<>);
{ jobType = adapterType.MakeGenericType(jobType);
var adapterType = typeof(QuartzJobSimpleAdapter<>);
jobType = adapterType.MakeGenericType(jobType);
}
} }
if (jobType == null) if (jobType == null)

23
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks/LINGYUN/Abp/BackgroundTasks/BackgroundJobAdapter.cs

@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -14,15 +13,12 @@ public class BackgroundJobAdapter<TArgs> : IJobRunnable
protected AbpBackgroundJobOptions Options { get; } protected AbpBackgroundJobOptions Options { get; }
protected IBackgroundJobExecuter JobExecuter { get; } protected IBackgroundJobExecuter JobExecuter { get; }
protected IServiceScopeFactory ServiceScopeFactory { get; }
public BackgroundJobAdapter( public BackgroundJobAdapter(
IOptions<AbpBackgroundJobOptions> options, IOptions<AbpBackgroundJobOptions> options,
IBackgroundJobExecuter jobExecuter, IBackgroundJobExecuter jobExecuter)
IServiceScopeFactory serviceScopeFactory)
{ {
JobExecuter = jobExecuter; JobExecuter = jobExecuter;
ServiceScopeFactory = serviceScopeFactory;
Options = options.Value; Options = options.Value;
Logger = NullLogger<BackgroundJobAdapter<TArgs>>.Instance; Logger = NullLogger<BackgroundJobAdapter<TArgs>>.Instance;
@ -30,17 +26,20 @@ public class BackgroundJobAdapter<TArgs> : IJobRunnable
public async virtual Task ExecuteAsync(JobRunnableContext context) public async virtual Task ExecuteAsync(JobRunnableContext context)
{ {
using var scope = ServiceScopeFactory.CreateScope(); object jobArgs = null;
object args = null;
if (context.TryGetString(nameof(TArgs), out var argsJson)) if (context.TryGetString(nameof(TArgs), out var argsJson))
{ {
var jsonSerializer = context.GetRequiredService<IJsonSerializer>(); var jsonSerializer = context.GetRequiredService<IJsonSerializer>();
args = jsonSerializer.Deserialize<TArgs>(argsJson); jobArgs = jsonSerializer.Deserialize<TArgs>(argsJson);
// args = JsonConvert.DeserializeObject<TArgs>(argsJson);
} }
var jobType = Options.GetJob(typeof(TArgs)).JobType; var jobConfiguration = Options.GetJob<TArgs>();
var jobContext = new JobExecutionContext(scope.ServiceProvider, jobType, args);
var jobContext = new JobExecutionContext(
context.ServiceProvider,
jobConfiguration.JobType,
jobArgs,
context.CancellationToken);
await JobExecuter.ExecuteAsync(jobContext); await JobExecuter.ExecuteAsync(jobContext);
} }
} }

2
aspnet-core/modules/task-management/LINGYUN.Abp.BackgroundTasks/LINGYUN/Abp/BackgroundTasks/BackgroundWorkerAdapter.cs

@ -86,7 +86,7 @@ public class BackgroundWorkerAdapter<TWorker> : BackgroundWorkerBase, IBackgroun
public async Task ExecuteAsync(JobRunnableContext context) public async Task ExecuteAsync(JobRunnableContext context)
{ {
var worker = (IBackgroundWorker)context.GetService(typeof(TWorker)); var worker = (IBackgroundWorker)context.GetService(typeof(TWorker));
var workerContext = new PeriodicBackgroundWorkerContext(ServiceProvider, context.CancellationToken); var workerContext = new PeriodicBackgroundWorkerContext(context.ServiceProvider, context.CancellationToken);
switch (worker) switch (worker)
{ {

Loading…
Cancel
Save