diff --git a/framework/src/Volo.Abp.BackgroundJobs.TickerQ/Volo/Abp/BackgroundJobs/TickerQ/AbpBackgroundJobsTickerQModule.cs b/framework/src/Volo.Abp.BackgroundJobs.TickerQ/Volo/Abp/BackgroundJobs/TickerQ/AbpBackgroundJobsTickerQModule.cs index 1ccdfca88a..5e03bc33cd 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.TickerQ/Volo/Abp/BackgroundJobs/TickerQ/AbpBackgroundJobsTickerQModule.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.TickerQ/Volo/Abp/BackgroundJobs/TickerQ/AbpBackgroundJobsTickerQModule.cs @@ -29,18 +29,16 @@ public class AbpBackgroundJobsTickerQModule : AbpModule requestTypes.TryAdd(jobConfiguration.JobName, (jobConfiguration.ArgsType.FullName, jobConfiguration.ArgsType)!); } - PreConfigure(options => + var abpTickerQFunctionProvider = context.ServiceProvider.GetRequiredService(); + foreach (var functionDelegate in tickerFunctionDelegates) { - foreach (var functionDelegate in tickerFunctionDelegates) - { - options.Functions.TryAdd(functionDelegate.Key, functionDelegate.Value); - } + abpTickerQFunctionProvider.Functions.TryAdd(functionDelegate.Key, functionDelegate.Value); + } - foreach (var requestType in requestTypes) - { - options.RequestTypes.TryAdd(requestType.Key, requestType.Value); - } - }); + foreach (var requestType in requestTypes) + { + abpTickerQFunctionProvider.RequestTypes.TryAdd(requestType.Key, requestType.Value); + } } private static TickerFunctionDelegate GetTickerFunctionDelegate(Type argsType) diff --git a/framework/src/Volo.Abp.BackgroundJobs.TickerQ/Volo/Abp/BackgroundJobs/TickerQ/TickerQBackgroundJobManager.cs b/framework/src/Volo.Abp.BackgroundJobs.TickerQ/Volo/Abp/BackgroundJobs/TickerQ/TickerQBackgroundJobManager.cs index 3d52475f40..c48efdada6 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.TickerQ/Volo/Abp/BackgroundJobs/TickerQ/TickerQBackgroundJobManager.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.TickerQ/Volo/Abp/BackgroundJobs/TickerQ/TickerQBackgroundJobManager.cs @@ -34,6 +34,6 @@ public class TickerQBackgroundJobManager : IBackgroundJobManager, ITransientDepe RetryIntervals = [30, 60, 120], // Retry after 30s, 60s, then 2min }); - return result.Result.Id.ToString(); + return !result.IsSucceded ? throw result.Exception : result.Result.Id.ToString(); } } diff --git a/framework/src/Volo.Abp.BackgroundWorkers.TickerQ/Volo/Abp/BackgroundWorkers/TickerQ/TickerQBackgroundWorkerManager.cs b/framework/src/Volo.Abp.BackgroundWorkers.TickerQ/Volo/Abp/BackgroundWorkers/TickerQ/TickerQBackgroundWorkerManager.cs index bb2ea865b4..1ef8b42e27 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.TickerQ/Volo/Abp/BackgroundWorkers/TickerQ/TickerQBackgroundWorkerManager.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.TickerQ/Volo/Abp/BackgroundWorkers/TickerQ/TickerQBackgroundWorkerManager.cs @@ -1,7 +1,6 @@ using System; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; using TickerQ.Utilities.Enums; using Volo.Abp.DependencyInjection; using Volo.Abp.TickerQ; @@ -12,11 +11,11 @@ namespace Volo.Abp.BackgroundWorkers.TickerQ; [ExposeServices(typeof(IBackgroundWorkerManager), typeof(TickerQBackgroundWorkerManager))] public class TickerQBackgroundWorkerManager : BackgroundWorkerManager, ISingletonDependency { - protected IObjectAccessor ObjectAccessor { get; } + protected AbpTickerQFunctionProvider AbpTickerQFunctionProvider { get; } - public TickerQBackgroundWorkerManager(IObjectAccessor objectAccessor) + public TickerQBackgroundWorkerManager(AbpTickerQFunctionProvider abpTickerQFunctionProvider) { - ObjectAccessor = objectAccessor; + AbpTickerQFunctionProvider = abpTickerQFunctionProvider; } public override async Task AddAsync(IBackgroundWorker worker, CancellationToken cancellationToken = default) @@ -47,15 +46,12 @@ public class TickerQBackgroundWorkerManager : BackgroundWorkerManager, ISingleto cronExpression = GetCron(period.Value); } - ObjectAccessor.Value!.PreConfigure(options => + var name = BackgroundWorkerNameAttribute.GetNameOrNull(worker.GetType()) ?? worker.GetType().FullName; + AbpTickerQFunctionProvider.Functions.TryAdd(name!, (cronExpression!, TickerTaskPriority.Normal, async (tickerQCancellationToken, serviceProvider, tickerFunctionContext) => { - var name = BackgroundWorkerNameAttribute.GetNameOrNull(worker.GetType()) ?? worker.GetType().FullName; - options.Functions.TryAdd(name!, (cronExpression!, TickerTaskPriority.Normal, async (tickerQCancellationToken, serviceProvider, tickerFunctionContext) => - { - var workerInvoker = new TickerQPeriodicBackgroundWorkerInvoker(worker, serviceProvider); - await workerInvoker.DoWorkAsync(tickerFunctionContext, tickerQCancellationToken); - })); - }); + var workerInvoker = new TickerQPeriodicBackgroundWorkerInvoker(worker, serviceProvider); + await workerInvoker.DoWorkAsync(tickerFunctionContext, tickerQCancellationToken); + })); } await base.AddAsync(worker, cancellationToken); diff --git a/framework/src/Volo.Abp.TickerQ/Microsoft/AspNetCore/Builder/AbpTickerQApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.TickerQ/Microsoft/AspNetCore/Builder/AbpTickerQApplicationBuilderExtensions.cs new file mode 100644 index 0000000000..4e81565e99 --- /dev/null +++ b/framework/src/Volo.Abp.TickerQ/Microsoft/AspNetCore/Builder/AbpTickerQApplicationBuilderExtensions.cs @@ -0,0 +1,20 @@ +using Microsoft.Extensions.DependencyInjection; +using TickerQ.DependencyInjection; +using TickerQ.Utilities; +using TickerQ.Utilities.Enums; +using Volo.Abp.TickerQ; + +namespace Microsoft.AspNetCore.Builder; + +public static class AbpTickerQApplicationBuilderExtensions +{ + public static IApplicationBuilder UseAbpTickerQ(this IApplicationBuilder app, TickerQStartMode qStartMode = TickerQStartMode.Immediate) + { + var abpTickerQFunctionProvider = app.ApplicationServices.GetRequiredService(); + TickerFunctionProvider.RegisterFunctions(abpTickerQFunctionProvider.Functions); + TickerFunctionProvider.RegisterRequestType(abpTickerQFunctionProvider.RequestTypes); + + app.UseTickerQ(qStartMode); + return app; + } +} diff --git a/framework/src/Volo.Abp.TickerQ/Volo/Abp/TickerQ/AbpTickerQOptions.cs b/framework/src/Volo.Abp.TickerQ/Volo/Abp/TickerQ/AbpTickerQFunctionProvider.cs similarity index 77% rename from framework/src/Volo.Abp.TickerQ/Volo/Abp/TickerQ/AbpTickerQOptions.cs rename to framework/src/Volo.Abp.TickerQ/Volo/Abp/TickerQ/AbpTickerQFunctionProvider.cs index 01463ea8b5..b92888e533 100644 --- a/framework/src/Volo.Abp.TickerQ/Volo/Abp/TickerQ/AbpTickerQOptions.cs +++ b/framework/src/Volo.Abp.TickerQ/Volo/Abp/TickerQ/AbpTickerQFunctionProvider.cs @@ -2,16 +2,17 @@ using System; using System.Collections.Generic; using TickerQ.Utilities; using TickerQ.Utilities.Enums; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.TickerQ; -public class AbpTickerQOptions +public class AbpTickerQFunctionProvider : ISingletonDependency { public Dictionary Functions { get;} public Dictionary RequestTypes { get; } - public AbpTickerQOptions() + public AbpTickerQFunctionProvider() { Functions = new Dictionary(); RequestTypes = new Dictionary(); diff --git a/framework/src/Volo.Abp.TickerQ/Volo/Abp/TickerQ/AbpTickerQModule.cs b/framework/src/Volo.Abp.TickerQ/Volo/Abp/TickerQ/AbpTickerQModule.cs index 1d6965741f..b6c140e962 100644 --- a/framework/src/Volo.Abp.TickerQ/Volo/Abp/TickerQ/AbpTickerQModule.cs +++ b/framework/src/Volo.Abp.TickerQ/Volo/Abp/TickerQ/AbpTickerQModule.cs @@ -1,7 +1,5 @@ using Microsoft.Extensions.DependencyInjection; using TickerQ.DependencyInjection; -using TickerQ.Utilities; -using Volo.Abp.DependencyInjection; using Volo.Abp.Modularity; namespace Volo.Abp.TickerQ; @@ -15,17 +13,4 @@ public class AbpTickerQModule : AbpModule options.SetInstanceIdentifier(context.Services.GetApplicationName()); }); } - - public override void OnPostApplicationInitialization(ApplicationInitializationContext context) - { - var serviceCollection = context.ServiceProvider.GetRequiredService>(); - if (serviceCollection.Value == null) - { - return; - } - - var tickerQ = serviceCollection.Value.ExecutePreConfiguredActions(); - TickerFunctionProvider.RegisterFunctions(tickerQ.Functions); - TickerFunctionProvider.RegisterRequestType(tickerQ.RequestTypes); - } } diff --git a/modules/background-jobs/Volo.Abp.BackgroundJobs.slnx b/modules/background-jobs/Volo.Abp.BackgroundJobs.slnx index d8a41ccb9d..22eb42689c 100644 --- a/modules/background-jobs/Volo.Abp.BackgroundJobs.slnx +++ b/modules/background-jobs/Volo.Abp.BackgroundJobs.slnx @@ -5,6 +5,7 @@ + @@ -19,4 +20,4 @@ - + \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Shared/DemoAppSharedModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Shared/DemoAppSharedModule.cs index ad002a5e18..7e67dd48a9 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Shared/DemoAppSharedModule.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Shared/DemoAppSharedModule.cs @@ -4,9 +4,6 @@ using Volo.Abp.Modularity; namespace Volo.Abp.BackgroundJobs.DemoApp.Shared { - [DependsOn( - typeof(AbpBackgroundJobsModule) - )] public class DemoAppSharedModule : AbpModule { public override void OnPostApplicationInitialization(ApplicationInitializationContext context) diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/DemoAppTickerQModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/DemoAppTickerQModule.cs new file mode 100644 index 0000000000..2ab4d606e7 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/DemoAppTickerQModule.cs @@ -0,0 +1,53 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using TickerQ.Utilities.Interfaces.Managers; +using TickerQ.Utilities.Models.Ticker; +using Volo.Abp.AspNetCore; +using Volo.Abp.Autofac; +using Volo.Abp.BackgroundJobs.DemoApp.Shared; +using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs; +using Volo.Abp.BackgroundJobs.TickerQ; +using Volo.Abp.Modularity; + +namespace Volo.Abp.BackgroundJobs.DemoApp.TickerQ; + +[DependsOn( + typeof(AbpBackgroundJobsTickerQModule), + typeof(DemoAppSharedModule), + typeof(AbpAutofacModule), + typeof(AbpAspNetCoreModule) +)] +public class DemoAppTickerQModule : AbpModule +{ + public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + app.UseAbpTickerQ(); + + app.UseRouting(); + app.UseEndpoints(endpoints => + { + endpoints.MapGet("/", async httpContext => + { + await httpContext.Response.WriteAsync("Hello TickerQ!"); + }); + }); + + await CancelableBackgroundJobAsync(context.ServiceProvider); + } + + private async Task CancelableBackgroundJobAsync(IServiceProvider serviceProvider) + { + var backgroundJobManager = serviceProvider.GetRequiredService(); + var jobId = await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-1" }); + await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" }); + Thread.Sleep(1000); + + var timeTickerManager = serviceProvider.GetRequiredService>(); + var result = await timeTickerManager.DeleteAsync(Guid.Parse(jobId)); + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/Program.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/Program.cs new file mode 100644 index 0000000000..4f5ae47673 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/Program.cs @@ -0,0 +1,19 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace Volo.Abp.BackgroundJobs.DemoApp.TickerQ; + +public class Program +{ + public static async Task Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + builder.Host.AddAppSettingsSecretsJson().UseAutofac(); + await builder.AddApplicationAsync(); + var app = builder.Build(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/Volo.Abp.BackgroundJobs.DemoApp.TickerQ.csproj b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/Volo.Abp.BackgroundJobs.DemoApp.TickerQ.csproj new file mode 100644 index 0000000000..be9ff04088 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/Volo.Abp.BackgroundJobs.DemoApp.TickerQ.csproj @@ -0,0 +1,24 @@ + + + + Exe + net10.0 + + + + + + + + + + + + Always + + + Always + + + + diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/appsettings.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/appsettings.json new file mode 100644 index 0000000000..0db3279e44 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/appsettings.json @@ -0,0 +1,3 @@ +{ + +}