Browse Source

Move anonymous job executor; normalize TickerQ args

Move AnonymousJobExecutorAsyncBackgroundJob into the Abstractions package and remove its dependency on ICancellationTokenProvider. The constructor and handler invocation now use a default CancellationToken and unused usings were cleaned up.

In AbpTickerQBackgroundJobManager, add NormalizeArgs and use it before creating the ticker request to ensure the provided args match the expected args type. The normalization performs a JSON round-trip when necessary so CreateTickerRequest receives an instance of the correct type.
pull/25059/head
SALİH ÖZKARA 7 months ago
parent
commit
2c6e3b565a
  1. 10
      framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AnonymousJobExecutorAsyncBackgroundJob.cs
  2. 15
      framework/src/Volo.Abp.BackgroundJobs.TickerQ/Volo/Abp/BackgroundJobs/TickerQ/AbpTickerQBackgroundJobManager.cs

10
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AnonymousJobExecutorAsyncBackgroundJob.cs → framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/AnonymousJobExecutorAsyncBackgroundJob.cs

@ -1,8 +1,7 @@
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace Volo.Abp.BackgroundJobs; namespace Volo.Abp.BackgroundJobs;
@ -10,16 +9,13 @@ public class AnonymousJobExecutorAsyncBackgroundJob : AsyncBackgroundJob<Anonymo
{ {
protected IAnonymousJobHandlerRegistry HandlerRegistry { get; } protected IAnonymousJobHandlerRegistry HandlerRegistry { get; }
protected IServiceProvider ServiceProvider { get; } protected IServiceProvider ServiceProvider { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; }
public AnonymousJobExecutorAsyncBackgroundJob( public AnonymousJobExecutorAsyncBackgroundJob(
IAnonymousJobHandlerRegistry handlerRegistry, IAnonymousJobHandlerRegistry handlerRegistry,
IServiceProvider serviceProvider, IServiceProvider serviceProvider)
ICancellationTokenProvider cancellationTokenProvider)
{ {
HandlerRegistry = handlerRegistry; HandlerRegistry = handlerRegistry;
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
CancellationTokenProvider = cancellationTokenProvider;
} }
public override async Task ExecuteAsync(AnonymousJobArgs args) public override async Task ExecuteAsync(AnonymousJobArgs args)
@ -30,6 +26,6 @@ public class AnonymousJobExecutorAsyncBackgroundJob : AsyncBackgroundJob<Anonymo
throw new AbpException("No anonymous job handler registered for: " + args.JobName); throw new AbpException("No anonymous job handler registered for: " + args.JobName);
} }
await handler(args.JsonData, ServiceProvider, CancellationTokenProvider.Token); await handler(args.JsonData, ServiceProvider, default(CancellationToken));
} }
} }

15
framework/src/Volo.Abp.BackgroundJobs.TickerQ/Volo/Abp/BackgroundJobs/TickerQ/AbpTickerQBackgroundJobManager.cs

@ -61,12 +61,14 @@ public class AbpTickerQBackgroundJobManager : IBackgroundJobManager, ITransientD
protected virtual async Task<string> EnqueueAsync(BackgroundJobConfiguration job, object args, BackgroundJobPriority priority, TimeSpan? delay) protected virtual async Task<string> EnqueueAsync(BackgroundJobConfiguration job, object args, BackgroundJobPriority priority, TimeSpan? delay)
{ {
var normalizedArgs = NormalizeArgs(job.ArgsType, args);
var timeTicker = new TimeTickerEntity var timeTicker = new TimeTickerEntity
{ {
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
Function = job.JobName, Function = job.JobName,
ExecutionTime = delay == null ? DateTime.UtcNow : DateTime.UtcNow.Add(delay.Value), ExecutionTime = delay == null ? DateTime.UtcNow : DateTime.UtcNow.Add(delay.Value),
Request = CreateTickerRequest(job.ArgsType, args), Request = CreateTickerRequest(job.ArgsType, normalizedArgs),
}; };
var config = TickerQOptions.GetConfigurationOrNull(job.JobType); var config = TickerQOptions.GetConfigurationOrNull(job.JobType);
@ -81,6 +83,17 @@ public class AbpTickerQBackgroundJobManager : IBackgroundJobManager, ITransientD
return !result.IsSucceeded ? timeTicker.Id.ToString() : result.Result.Id.ToString(); return !result.IsSucceeded ? timeTicker.Id.ToString() : result.Result.Id.ToString();
} }
protected virtual object NormalizeArgs(Type argsType, object args)
{
if (argsType.IsInstanceOfType(args))
{
return args;
}
var serialized = JsonSerializer.Serialize(args);
return JsonSerializer.Deserialize(argsType, serialized)!;
}
protected virtual byte[]? CreateTickerRequest(Type argsType, object args) protected virtual byte[]? CreateTickerRequest(Type argsType, object args)
{ {
return (byte[]?)CreateTickerRequestMethod return (byte[]?)CreateTickerRequestMethod

Loading…
Cancel
Save