Browse Source

CancellationToken parameter added for background jobs

pull/15282/head
ahmetfarukulu 4 years ago
parent
commit
dcd091adfb
  1. 5
      framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs
  2. 3
      framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJob.cs
  3. 4
      framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs
  4. 6
      framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/IAsyncBackgroundJob.cs
  5. 7
      framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/IBackgroundJob.cs
  6. 10
      framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/JobExecutionContext.cs
  7. 8
      framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireBackgroundJobManager.cs
  8. 9
      framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs
  9. 2
      framework/src/Volo.Abp.BackgroundJobs.Quartz/Volo/Abp/BackgroundJobs/Quartz/QuartzJobExecutionAdapter.cs
  10. 3
      framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/BackgroundEmailSendingJob.cs
  11. 3
      framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyAsyncJob.cs
  12. 3
      framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyJob.cs

5
framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AsyncBackgroundJob.cs

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
@ -15,5 +16,5 @@ public abstract class AsyncBackgroundJob<TArgs> : IAsyncBackgroundJob<TArgs>
Logger = NullLogger<AsyncBackgroundJob<TArgs>>.Instance;
}
public abstract Task ExecuteAsync(TArgs args);
public abstract Task ExecuteAsync(TArgs args, CancellationToken cancellationToken = default);
}

3
framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJob.cs

@ -1,3 +1,4 @@
using System.Threading;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
@ -14,5 +15,5 @@ public abstract class BackgroundJob<TArgs> : IBackgroundJob<TArgs>
Logger = NullLogger<BackgroundJob<TArgs>>.Instance;
}
public abstract void Execute(TArgs args);
public abstract void Execute(TArgs args, CancellationToken cancellationToken = default);
}

4
framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs

@ -48,11 +48,11 @@ public class BackgroundJobExecuter : IBackgroundJobExecuter, ITransientDependenc
{
if (jobExecuteMethod.Name == nameof(IAsyncBackgroundJob<object>.ExecuteAsync))
{
await ((Task)jobExecuteMethod.Invoke(job, new[] { context.JobArgs }));
await ((Task)jobExecuteMethod.Invoke(job, new[] { context.JobArgs, context.CancellationToken }));
}
else
{
jobExecuteMethod.Invoke(job, new[] { context.JobArgs });
jobExecuteMethod.Invoke(job, new[] { context.JobArgs, context.CancellationToken });
}
}

6
framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/IAsyncBackgroundJob.cs

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
namespace Volo.Abp.BackgroundJobs;
@ -11,5 +12,6 @@ public interface IAsyncBackgroundJob<in TArgs>
/// Executes the job with the <paramref name="args"/>.
/// </summary>
/// <param name="args">Job arguments.</param>
Task ExecuteAsync(TArgs args);
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
Task ExecuteAsync(TArgs args, CancellationToken cancellationToken = default);
}

7
framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/IBackgroundJob.cs

@ -1,4 +1,6 @@
namespace Volo.Abp.BackgroundJobs;
using System.Threading;
namespace Volo.Abp.BackgroundJobs;
/// <summary>
/// Defines interface of a background job.
@ -9,5 +11,6 @@ public interface IBackgroundJob<in TArgs>
/// Executes the job with the <paramref name="args"/>.
/// </summary>
/// <param name="args">Job arguments.</param>
void Execute(TArgs args);
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
void Execute(TArgs args, CancellationToken cancellationToken = default);
}

10
framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/JobExecutionContext.cs

@ -1,4 +1,5 @@
using System;
using System.Threading;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.BackgroundJobs;
@ -11,10 +12,17 @@ public class JobExecutionContext : IServiceProviderAccessor
public object JobArgs { get; }
public JobExecutionContext(IServiceProvider serviceProvider, Type jobType, object jobArgs)
public CancellationToken CancellationToken { get; }
public JobExecutionContext(
IServiceProvider serviceProvider,
Type jobType,
object jobArgs,
CancellationToken cancellationToken = default)
{
ServiceProvider = serviceProvider;
JobType = jobType;
JobArgs = jobArgs;
CancellationToken = cancellationToken;
}
}

8
framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireBackgroundJobManager.cs

@ -12,22 +12,22 @@ namespace Volo.Abp.BackgroundJobs.Hangfire;
public class HangfireBackgroundJobManager : IBackgroundJobManager, ITransientDependency
{
protected AbpBackgroundJobOptions Options { get; }
public HangfireBackgroundJobManager(IOptions<AbpBackgroundJobOptions> options)
{
Options = options.Value;
}
public virtual Task<string> EnqueueAsync<TArgs>(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal,
TimeSpan? delay = null)
{
return Task.FromResult(delay.HasValue
? BackgroundJob.Schedule<HangfireJobExecutionAdapter<TArgs>>(
adapter => adapter.ExecuteAsync(GetQueueName(typeof(TArgs)),args),
adapter => adapter.ExecuteAsync(GetQueueName(typeof(TArgs)), args, default),
delay.Value
)
: BackgroundJob.Enqueue<HangfireJobExecutionAdapter<TArgs>>(
adapter => adapter.ExecuteAsync(GetQueueName(typeof(TArgs)) ,args)
adapter => adapter.ExecuteAsync(GetQueueName(typeof(TArgs)), args, default)
));
}

9
framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using Hangfire;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
@ -21,8 +22,8 @@ public class HangfireJobExecutionAdapter<TArgs>
Options = options.Value;
}
[Queue("{0}")]
public async Task ExecuteAsync(string queue, TArgs args)
[Queue("{0}")]
public async Task ExecuteAsync(string queue, TArgs args, CancellationToken cancellationToken = default)
{
if (!Options.IsJobExecutionEnabled)
{
@ -38,7 +39,7 @@ public class HangfireJobExecutionAdapter<TArgs>
using (var scope = ServiceScopeFactory.CreateScope())
{
var jobType = Options.GetJob(typeof(TArgs)).JobType;
var context = new JobExecutionContext(scope.ServiceProvider, jobType, args);
var context = new JobExecutionContext(scope.ServiceProvider, jobType, args, cancellationToken: cancellationToken);
await JobExecuter.ExecuteAsync(context);
}
}

2
framework/src/Volo.Abp.BackgroundJobs.Quartz/Volo/Abp/BackgroundJobs/Quartz/QuartzJobExecutionAdapter.cs

@ -40,7 +40,7 @@ public class QuartzJobExecutionAdapter<TArgs> : IJob
{
var args = JsonSerializer.Deserialize<TArgs>(context.JobDetail.JobDataMap.GetString(nameof(TArgs)));
var jobType = Options.GetJob(typeof(TArgs)).JobType;
var jobContext = new JobExecutionContext(scope.ServiceProvider, jobType, args);
var jobContext = new JobExecutionContext(scope.ServiceProvider, jobType, args, cancellationToken: context.CancellationToken);
try
{
await JobExecuter.ExecuteAsync(jobContext);

3
framework/src/Volo.Abp.Emailing/Volo/Abp/Emailing/BackgroundEmailSendingJob.cs

@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
@ -14,7 +15,7 @@ public class BackgroundEmailSendingJob : AsyncBackgroundJob<BackgroundEmailSendi
EmailSender = emailSender;
}
public override async Task ExecuteAsync(BackgroundEmailSendingJobArgs args)
public override async Task ExecuteAsync(BackgroundEmailSendingJobArgs args, CancellationToken cancellationToken = default)
{
if (args.From.IsNullOrWhiteSpace())
{

3
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyAsyncJob.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
@ -19,7 +20,7 @@ public class MyAsyncJob : AsyncBackgroundJob<MyAsyncJobArgs>, ISingletonDependen
_currentTenant = currentTenant;
}
public override Task ExecuteAsync(MyAsyncJobArgs args)
public override Task ExecuteAsync(MyAsyncJobArgs args, CancellationToken cancellationToken = default)
{
ExecutedValues.Add(args.Value);
TenantId = _currentTenant.Id;

3
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyJob.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
@ -18,7 +19,7 @@ public class MyJob : BackgroundJob<MyJobArgs>, ISingletonDependency
_currentTenant = currentTenant;
}
public override void Execute(MyJobArgs args)
public override void Execute(MyJobArgs args, CancellationToken cancellationToken = default)
{
ExecutedValues.Add(args.Value);
TenantId = _currentTenant.Id;

Loading…
Cancel
Save