Implement AsyncBackgroundJob.

Resolve #2374
pull/2381/head
maliming 6 years ago
parent e45d2c07d0
commit b1eed33962

@ -23,7 +23,8 @@ namespace Volo.Abp.BackgroundJobs
services.OnRegistred(context =>
{
if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IBackgroundJob<>)))
if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IBackgroundJob<>)) ||
ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IAsyncBackgroundJob<>)))
{
jobTypes.Add(context.ImplementationType);
}

@ -0,0 +1,20 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Volo.Abp.BackgroundJobs
{
public abstract class AsyncBackgroundJob<TArgs> : IAsyncBackgroundJob<TArgs>
{
//TODO: Add UOW, Localization and other useful properties..?
public ILogger<AsyncBackgroundJob<TArgs>> Logger { get; set; }
protected AsyncBackgroundJob()
{
Logger = NullLogger<AsyncBackgroundJob<TArgs>>.Instance;
}
public abstract Task ExecuteAsync(TArgs args);
}
}

@ -13,7 +13,8 @@ namespace Volo.Abp.BackgroundJobs
continue;
}
if (@interface.GetGenericTypeDefinition() != typeof(IBackgroundJob<>))
if (@interface.GetGenericTypeDefinition() != typeof(IBackgroundJob<>) &&
@interface.GetGenericTypeDefinition() != typeof(IAsyncBackgroundJob<>))
{
continue;
}
@ -27,7 +28,9 @@ namespace Volo.Abp.BackgroundJobs
return genericArgs[0];
}
throw new AbpException($"Could not find type of the job args. Ensure that given type implements the {typeof(IBackgroundJob<>).AssemblyQualifiedName} interface. Given job type: {jobType.AssemblyQualifiedName}");
throw new AbpException($"Could not find type of the job args. " +
$"Ensure that given type implements the {typeof(IBackgroundJob<>).AssemblyQualifiedName} or {typeof(IAsyncBackgroundJob<>).AssemblyQualifiedName} interface. " +
$"Given job type: {jobType.AssemblyQualifiedName}");
}
}
}

@ -2,7 +2,9 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace Volo.Abp.BackgroundJobs
{
@ -27,15 +29,24 @@ namespace Volo.Abp.BackgroundJobs
throw new AbpException("The job type is not registered to DI: " + context.JobType);
}
var jobExecuteMethod = context.JobType.GetMethod(nameof(IBackgroundJob<object>.Execute));
var jobExecuteMethod = context.JobType.GetMethod(nameof(IBackgroundJob<object>.Execute)) ??
context.JobType.GetMethod(nameof(IAsyncBackgroundJob<object>.ExecuteAsync));
if (jobExecuteMethod == null)
{
throw new AbpException($"Given job type does not implement {typeof(IBackgroundJob<>).Name}. The job type was: " + context.JobType);
throw new AbpException($"Given job type does not implement {typeof(IBackgroundJob<>).Name} or {typeof(IAsyncBackgroundJob<>).Name}. " +
"The job type was: " + context.JobType);
}
try
{
jobExecuteMethod.Invoke(job, new[] { context.JobArgs });
if (jobExecuteMethod.Name == nameof(IAsyncBackgroundJob<object>.ExecuteAsync))
{
AsyncHelper.RunSync(() => (Task) jobExecuteMethod.Invoke(job, new[] {context.JobArgs}));
}
else
{
jobExecuteMethod.Invoke(job, new[] { context.JobArgs });
}
}
catch (Exception ex)
{

@ -0,0 +1,16 @@
using System.Threading.Tasks;
namespace Volo.Abp.BackgroundJobs
{
/// <summary>
/// Defines interface of a background job.
/// </summary>
public interface IAsyncBackgroundJob<in TArgs>
{
/// <summary>
/// Executes the job with the <see cref="args"/>.
/// </summary>
/// <param name="args">Job arguments.</param>
Task ExecuteAsync(TArgs args);
}
}

@ -1,4 +1,4 @@
using System.Threading.Tasks;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
@ -35,5 +35,28 @@ namespace Volo.Abp.BackgroundJobs
jobObject.ExecutedValues.ShouldContain("42");
}
[Fact]
public async Task Should_Execute_Async_Tasks()
{
//Arrange
var jobObject = GetRequiredService<MyAsyncJob>();
jobObject.ExecutedValues.ShouldBeEmpty();
//Act
_backgroundJobExecuter.Execute(
new JobExecutionContext(
ServiceProvider,
typeof(MyAsyncJob),
new MyAsyncJobArgs("42")
)
);
//Assert
jobObject.ExecutedValues.ShouldContain("42");
}
}
}

@ -23,5 +23,13 @@ namespace Volo.Abp.BackgroundJobs
jobIdAsString.ShouldNotBe(default);
(await _backgroundJobStore.FindAsync(Guid.Parse(jobIdAsString))).ShouldNotBeNull();
}
[Fact]
public async Task Should_Store_Async_Jobs()
{
var jobIdAsString = await _backgroundJobManager.EnqueueAsync(new MyAsyncJobArgs("42"));
jobIdAsString.ShouldNotBe(default);
(await _backgroundJobStore.FindAsync(Guid.Parse(jobIdAsString))).ShouldNotBeNull();
}
}
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.BackgroundJobs
{
public class MyAsyncJob : AsyncBackgroundJob<MyAsyncJobArgs>, ISingletonDependency
{
public List<string> ExecutedValues { get; } = new List<string>();
public override Task ExecuteAsync(MyAsyncJobArgs args)
{
ExecutedValues.Add(args.Value);
return Task.CompletedTask;
}
}
}

@ -0,0 +1,17 @@
namespace Volo.Abp.BackgroundJobs
{
public class MyAsyncJobArgs
{
public string Value { get; set; }
public MyAsyncJobArgs()
{
}
public MyAsyncJobArgs(string value)
{
Value = value;
}
}
}
Loading…
Cancel
Save