diff --git a/modules/background-jobs/Volo.Abp.BackgroundJobs.sln b/modules/background-jobs/Volo.Abp.BackgroundJobs.sln index 2b2d8620a6..55ff044072 100644 --- a/modules/background-jobs/Volo.Abp.BackgroundJobs.sln +++ b/modules/background-jobs/Volo.Abp.BackgroundJobs.sln @@ -37,6 +37,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.BackgroundJobs.Dem EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.BackgroundJobs.Installer", "src\Volo.Abp.BackgroundJobs.Installer\Volo.Abp.BackgroundJobs.Installer.csproj", "{FCD1EFC7-28D6-40F5-8C9A-7DE0328D6758}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.BackgroundJobs.DemoApp.HangFire2", "app\Volo.Abp.BackgroundJobs.DemoApp.HangFire2\Volo.Abp.BackgroundJobs.DemoApp.HangFire2.csproj", "{64C96E97-AF98-40A0-94D8-6C21106EA4B0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.BackgroundJobs.DemoApp.Quartz2", "app\Volo.Abp.BackgroundJobs.DemoApp.Quartz2\Volo.Abp.BackgroundJobs.DemoApp.Quartz2.csproj", "{7D897239-0D45-4A35-9CF7-0093F1AFD0E9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.BackgroundJobs.DemoApp2", "app\Volo.Abp.BackgroundJobs.DemoApp2\Volo.Abp.BackgroundJobs.DemoApp2.csproj", "{0CADE59C-72C6-4860-A382-A515291D43A4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -99,6 +105,18 @@ Global {FCD1EFC7-28D6-40F5-8C9A-7DE0328D6758}.Debug|Any CPU.Build.0 = Debug|Any CPU {FCD1EFC7-28D6-40F5-8C9A-7DE0328D6758}.Release|Any CPU.ActiveCfg = Release|Any CPU {FCD1EFC7-28D6-40F5-8C9A-7DE0328D6758}.Release|Any CPU.Build.0 = Release|Any CPU + {64C96E97-AF98-40A0-94D8-6C21106EA4B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {64C96E97-AF98-40A0-94D8-6C21106EA4B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {64C96E97-AF98-40A0-94D8-6C21106EA4B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {64C96E97-AF98-40A0-94D8-6C21106EA4B0}.Release|Any CPU.Build.0 = Release|Any CPU + {7D897239-0D45-4A35-9CF7-0093F1AFD0E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D897239-0D45-4A35-9CF7-0093F1AFD0E9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D897239-0D45-4A35-9CF7-0093F1AFD0E9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D897239-0D45-4A35-9CF7-0093F1AFD0E9}.Release|Any CPU.Build.0 = Release|Any CPU + {0CADE59C-72C6-4860-A382-A515291D43A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0CADE59C-72C6-4860-A382-A515291D43A4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0CADE59C-72C6-4860-A382-A515291D43A4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0CADE59C-72C6-4860-A382-A515291D43A4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -118,6 +136,9 @@ Global {7C8D03F7-165E-478C-A6D6-DC8229EEABB9} = {E400416D-2895-4512-9D17-90681EEC7E0A} {0146891D-3FF8-4516-94D6-B9EFC37383CD} = {E400416D-2895-4512-9D17-90681EEC7E0A} {FCD1EFC7-28D6-40F5-8C9A-7DE0328D6758} = {649A3FFA-182F-4E56-9717-E6A9A2BEC545} + {64C96E97-AF98-40A0-94D8-6C21106EA4B0} = {E400416D-2895-4512-9D17-90681EEC7E0A} + {7D897239-0D45-4A35-9CF7-0093F1AFD0E9} = {E400416D-2895-4512-9D17-90681EEC7E0A} + {0CADE59C-72C6-4860-A382-A515291D43A4} = {E400416D-2895-4512-9D17-90681EEC7E0A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4324B3B4-B60B-4E3C-91D8-59576B4E26DD} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/DemoAppHangfireModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/DemoAppHangfireModule.cs index cd74c10f82..fb3e5d6c56 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/DemoAppHangfireModule.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/DemoAppHangfireModule.cs @@ -1,17 +1,22 @@ -using Hangfire; +using System.Threading.Tasks; +using Hangfire; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Autofac; using Volo.Abp.BackgroundJobs.DemoApp.Shared; -using Volo.Abp.Modularity; -using Microsoft.Extensions.Configuration; using Volo.Abp.BackgroundJobs.Hangfire; +using Volo.Abp.BackgroundWorkers; +using Volo.Abp.BackgroundWorkers.Hangfire; +using Volo.Abp.Hangfire; +using Volo.Abp.Modularity; namespace Volo.Abp.BackgroundJobs.DemoApp.HangFire; [DependsOn( typeof(DemoAppSharedModule), typeof(AbpAutofacModule), - typeof(AbpBackgroundJobsHangfireModule) + typeof(AbpBackgroundJobsHangfireModule), + typeof(AbpBackgroundWorkersHangfireModule) )] public class DemoAppHangfireModule : AbpModule { @@ -19,9 +24,31 @@ public class DemoAppHangfireModule : AbpModule { var configuration = context.Services.GetConfiguration(); - context.Services.PreConfigure(hangfireConfiguration => + PreConfigure(hangfireConfiguration => { hangfireConfiguration.UseSqlServerStorage(configuration.GetConnectionString("Default")); }); } + + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.DefaultQueuePrefix = context.Services.GetApplicationName()!; + options.ServerOptions ??= new BackgroundJobServerOptions(); + options.ServerOptions.Queues = new[] { "default", "my_queue" }; + }); + + var configuration = context.Services.GetConfiguration(); + context.Services.AddHangfire(config => + { + config.UseSqlServerStorage(configuration.GetConnectionString("Default")); + }); + } + + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + await context.AddBackgroundWorkerAsync(); + await context.AddBackgroundWorkerAsync(); + } } diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/MyLogWorker.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/MyLogWorker.cs new file mode 100644 index 0000000000..bbb9655b4e --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/MyLogWorker.cs @@ -0,0 +1,38 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Hangfire; +using Volo.Abp.BackgroundWorkers.Hangfire; + +namespace Volo.Abp.BackgroundJobs.DemoApp.HangFire; + +public class MyLogWorker : HangfireBackgroundWorkerBase +{ + public MyLogWorker() + { + RecurringJobId = nameof(MyLogWorker); + CronExpression = Cron.Minutely(); + } + + public override Task DoWorkAsync(CancellationToken cancellationToken = default) + { + Console.WriteLine("Executed MyLogWorker..!"); + return Task.CompletedTask; + } +} + +public class MyLogWorker2 : HangfireBackgroundWorkerBase +{ + public MyLogWorker2() + { + RecurringJobId = nameof(MyLogWorker2); + CronExpression = Cron.Minutely(); + Queue = "my_queue"; + } + + public override Task DoWorkAsync(CancellationToken cancellationToken = default) + { + Console.WriteLine("Executed MyLogWorker2..!"); + return Task.CompletedTask; + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/Volo.Abp.BackgroundJobs.DemoApp.HangFire.csproj b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/Volo.Abp.BackgroundJobs.DemoApp.HangFire.csproj index 2236da5c2b..44cd149e39 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/Volo.Abp.BackgroundJobs.DemoApp.HangFire.csproj +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/Volo.Abp.BackgroundJobs.DemoApp.HangFire.csproj @@ -7,8 +7,10 @@ + + diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/appsettings.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/appsettings.json index f80742fd70..27bf0485bd 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/appsettings.json +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/appsettings.json @@ -1,5 +1,5 @@ { "ConnectionStrings": { - "Default": "Server=localhost;Database=BackgroundJobsDemoApp;Trusted_Connection=True;TrustServerCertificate=True" + "Default": "Server=localhost;Database=BackgroundJobsDemoApp;User Id=sa;Password=1q2w3E***;TrustServerCertificate=True" } } \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/DemoAppHangfireModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/DemoAppHangfireModule.cs new file mode 100644 index 0000000000..9193335b7e --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/DemoAppHangfireModule.cs @@ -0,0 +1,54 @@ +using System.Threading.Tasks; +using Hangfire; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; +using Volo.Abp.BackgroundJobs.DemoApp.Shared; +using Volo.Abp.BackgroundJobs.Hangfire; +using Volo.Abp.BackgroundWorkers; +using Volo.Abp.BackgroundWorkers.Hangfire; +using Volo.Abp.Hangfire; +using Volo.Abp.Modularity; + +namespace Volo.Abp.BackgroundJobs.DemoApp.HangFire2; + +[DependsOn( + typeof(DemoAppSharedModule), + typeof(AbpAutofacModule), + typeof(AbpBackgroundJobsHangfireModule), + typeof(AbpBackgroundWorkersHangfireModule) +)] +public class DemoAppHangfireModule : AbpModule +{ + public override void PreConfigureServices(ServiceConfigurationContext context) + { + var configuration = context.Services.GetConfiguration(); + + PreConfigure(hangfireConfiguration => + { + hangfireConfiguration.UseSqlServerStorage(configuration.GetConnectionString("Default")); + }); + } + + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.DefaultQueuePrefix = context.Services.GetApplicationName()!; + options.ServerOptions ??= new BackgroundJobServerOptions(); + options.ServerOptions.Queues = new[] { "default", "my_queue" }; + }); + + var configuration = context.Services.GetConfiguration(); + context.Services.AddHangfire(config => + { + config.UseSqlServerStorage(configuration.GetConnectionString("Default")); + }); + } + + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + await context.AddBackgroundWorkerAsync(); + await context.AddBackgroundWorkerAsync(); + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/MyLogWorker.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/MyLogWorker.cs new file mode 100644 index 0000000000..c68dcad675 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/MyLogWorker.cs @@ -0,0 +1,38 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Hangfire; +using Volo.Abp.BackgroundWorkers.Hangfire; + +namespace Volo.Abp.BackgroundJobs.DemoApp.HangFire2; + +public class MyLogWorker : HangfireBackgroundWorkerBase +{ + public MyLogWorker() + { + RecurringJobId = nameof(MyLogWorker); + CronExpression = Cron.Minutely(); + } + + public override Task DoWorkAsync(CancellationToken cancellationToken = default) + { + Console.WriteLine("Executed MyLogWorker..!"); + return Task.CompletedTask; + } +} + +public class MyLogWorker2 : HangfireBackgroundWorkerBase +{ + public MyLogWorker2() + { + RecurringJobId = nameof(MyLogWorker2); + CronExpression = Cron.Minutely(); + Queue = "my_queue"; + } + + public override Task DoWorkAsync(CancellationToken cancellationToken = default) + { + Console.WriteLine("Executed MyLogWorker2..!"); + return Task.CompletedTask; + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Program.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Program.cs new file mode 100644 index 0000000000..65d2639e7a --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Program.cs @@ -0,0 +1,39 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Hangfire; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs; + +namespace Volo.Abp.BackgroundJobs.DemoApp.HangFire2; + +class Program +{ + async static Task Main(string[] args) + { + using (var application = await AbpApplicationFactory.CreateAsync(options => + { + options.UseAutofac(); + })) + { + await application.InitializeAsync(); + + await CancelableBackgroundJobAsync(application.ServiceProvider); + + Console.WriteLine("Started: " + typeof(Program).Namespace); + Console.WriteLine("Press ENTER to stop the application..!"); + Console.ReadLine(); + + await application.ShutdownAsync(); + } + } + + private async static 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); + BackgroundJob.Delete(jobId); + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Volo.Abp.BackgroundJobs.DemoApp.HangFire.abppkg b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Volo.Abp.BackgroundJobs.DemoApp.HangFire.abppkg new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Volo.Abp.BackgroundJobs.DemoApp.HangFire.abppkg @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Volo.Abp.BackgroundJobs.DemoApp.HangFire.abppkg.analyze.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Volo.Abp.BackgroundJobs.DemoApp.HangFire.abppkg.analyze.json new file mode 100644 index 0000000000..99687fb7c5 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Volo.Abp.BackgroundJobs.DemoApp.HangFire.abppkg.analyze.json @@ -0,0 +1,73 @@ +{ + "name": "Volo.Abp.BackgroundJobs.DemoApp.HangFire", + "hash": "a4ac122e27b8e7b2048e2c8803e8f6f0", + "contents": [ + { + "namespace": "Volo.Abp.BackgroundJobs.DemoApp.HangFire", + "dependsOnModules": [ + { + "declaringAssemblyName": "Volo.Abp.BackgroundJobs.DemoApp.Shared", + "namespace": "Volo.Abp.BackgroundJobs.DemoApp.Shared", + "name": "DemoAppSharedModule" + }, + { + "declaringAssemblyName": "Volo.Abp.Autofac", + "namespace": "Volo.Abp.Autofac", + "name": "AbpAutofacModule" + }, + { + "declaringAssemblyName": "Volo.Abp.BackgroundJobs.HangFire", + "namespace": "Volo.Abp.BackgroundJobs.Hangfire", + "name": "AbpBackgroundJobsHangfireModule" + } + ], + "implementingInterfaces": [ + { + "name": "IAbpModule", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IAbpModule" + }, + { + "name": "IOnPreApplicationInitialization", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IOnPreApplicationInitialization" + }, + { + "name": "IOnApplicationInitialization", + "namespace": "Volo.Abp", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.IOnApplicationInitialization" + }, + { + "name": "IOnPostApplicationInitialization", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IOnPostApplicationInitialization" + }, + { + "name": "IOnApplicationShutdown", + "namespace": "Volo.Abp", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.IOnApplicationShutdown" + }, + { + "name": "IPreConfigureServices", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IPreConfigureServices" + }, + { + "name": "IPostConfigureServices", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IPostConfigureServices" + } + ], + "contentType": "abpModule", + "name": "DemoAppHangfireModule", + "summary": null + } + ] +} \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Volo.Abp.BackgroundJobs.DemoApp.HangFire2.csproj b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Volo.Abp.BackgroundJobs.DemoApp.HangFire2.csproj new file mode 100644 index 0000000000..550a17b63d --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/Volo.Abp.BackgroundJobs.DemoApp.HangFire2.csproj @@ -0,0 +1,24 @@ + + + + Exe + net9.0 + Volo.Abp.BackgroundJobs.DemoApp.HangFire2 + + + + + + + + + + + + + + Always + + + + diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/appsettings.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/appsettings.json new file mode 100644 index 0000000000..27bf0485bd --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire2/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "Default": "Server=localhost;Database=BackgroundJobsDemoApp;User Id=sa;Password=1q2w3E***;TrustServerCertificate=True" + } +} \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/DemoAppQuartzModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/DemoAppQuartzModule.cs index 0d521baf3d..084d7643d4 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/DemoAppQuartzModule.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/DemoAppQuartzModule.cs @@ -1,16 +1,40 @@ -using Volo.Abp.Autofac; +using System.Collections.Specialized; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; using Volo.Abp.BackgroundJobs.DemoApp.Shared; using Volo.Abp.BackgroundJobs.Quartz; +using Volo.Abp.BackgroundWorkers.Quartz; using Volo.Abp.Modularity; +using Volo.Abp.Quartz; namespace Volo.Abp.BackgroundJobs.DemoApp.Quartz; [DependsOn( typeof(DemoAppSharedModule), typeof(AbpAutofacModule), - typeof(AbpBackgroundJobsQuartzModule) + typeof(AbpBackgroundJobsQuartzModule), + typeof(AbpBackgroundWorkersQuartzModule) )] public class DemoAppQuartzModule : AbpModule { - + public override void PreConfigureServices(ServiceConfigurationContext context) + { + //https://github.com/quartznet/quartznet/blob/main/database/tables/tables_sqlServer.sql + var configuration = context.Services.GetConfiguration(); + PreConfigure(options => + { + options.Properties = new NameValueCollection + { + ["quartz.scheduler.instanceName"] = context.Services.GetApplicationName(), + ["quartz.jobStore.dataSource"] = "BackgroundJobsDemoApp", + ["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz", + ["quartz.jobStore.tablePrefix"] = "QRTZ_", + ["quartz.serializer.type"] = "json", + ["quartz.dataSource.BackgroundJobsDemoApp.connectionString"] = configuration.GetConnectionString("Default"), + ["quartz.dataSource.BackgroundJobsDemoApp.provider"] = "SqlServer", + ["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz", + }; + }); + } } diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/MyLogWorker.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/MyLogWorker.cs new file mode 100644 index 0000000000..457c32c093 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/MyLogWorker.cs @@ -0,0 +1,36 @@ +using System; +using System.Threading.Tasks; +using Quartz; +using Volo.Abp.BackgroundWorkers.Quartz; + +namespace Volo.Abp.BackgroundJobs.DemoApp.Quartz; + +public class MyLogWorker : QuartzBackgroundWorkerBase +{ + public MyLogWorker() + { + JobDetail = JobBuilder.Create().WithIdentity(nameof(MyLogWorker)).Build(); + Trigger = TriggerBuilder.Create().WithIdentity(nameof(MyLogWorker)).StartAt(DateTimeOffset.Now.AddSeconds(10)).Build(); + } + + public override Task Execute(IJobExecutionContext context) + { + Console.WriteLine("Executed MyLogWorker..!"); + return Task.CompletedTask; + } +} + +public class MyLogWorker2 : QuartzBackgroundWorkerBase +{ + public MyLogWorker2() + { + JobDetail = JobBuilder.Create().WithIdentity(nameof(MyLogWorker2)).Build(); + Trigger = TriggerBuilder.Create().WithIdentity(nameof(MyLogWorker2)).StartAt(DateTimeOffset.Now.AddSeconds(10)).Build(); + } + + public override Task Execute(IJobExecutionContext context) + { + Console.WriteLine("Executed MyLogWorker2..!"); + return Task.CompletedTask; + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/Volo.Abp.BackgroundJobs.DemoApp.Quartz.csproj b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/Volo.Abp.BackgroundJobs.DemoApp.Quartz.csproj index b646707e44..6125f6b5e2 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/Volo.Abp.BackgroundJobs.DemoApp.Quartz.csproj +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/Volo.Abp.BackgroundJobs.DemoApp.Quartz.csproj @@ -7,12 +7,20 @@ + + + + + Always + + + diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/appsettings.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/appsettings.json new file mode 100644 index 0000000000..27bf0485bd --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "Default": "Server=localhost;Database=BackgroundJobsDemoApp;User Id=sa;Password=1q2w3E***;TrustServerCertificate=True" + } +} \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/DemoAppQuartzModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/DemoAppQuartzModule.cs new file mode 100644 index 0000000000..70f7e77dcb --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/DemoAppQuartzModule.cs @@ -0,0 +1,40 @@ +using System.Collections.Specialized; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; +using Volo.Abp.BackgroundJobs.DemoApp.Shared; +using Volo.Abp.BackgroundJobs.Quartz; +using Volo.Abp.BackgroundWorkers.Quartz; +using Volo.Abp.Modularity; +using Volo.Abp.Quartz; + +namespace Volo.Abp.BackgroundJobs.DemoApp.Quartz2; + +[DependsOn( + typeof(DemoAppSharedModule), + typeof(AbpAutofacModule), + typeof(AbpBackgroundJobsQuartzModule), + typeof(AbpBackgroundWorkersQuartzModule) +)] +public class DemoAppQuartzModule : AbpModule +{ + public override void PreConfigureServices(ServiceConfigurationContext context) + { + //https://github.com/quartznet/quartznet/blob/main/database/tables/tables_sqlServer.sql + var configuration = context.Services.GetConfiguration(); + PreConfigure(options => + { + options.Properties = new NameValueCollection + { + ["quartz.scheduler.instanceName"] = context.Services.GetApplicationName(), + ["quartz.jobStore.dataSource"] = "BackgroundJobsDemoApp", + ["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz", + ["quartz.jobStore.tablePrefix"] = "QRTZ_", + ["quartz.serializer.type"] = "json", + ["quartz.dataSource.BackgroundJobsDemoApp.connectionString"] = configuration.GetConnectionString("Default"), + ["quartz.dataSource.BackgroundJobsDemoApp.provider"] = "SqlServer", + ["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz", + }; + }); + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/MyLogWorker.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/MyLogWorker.cs new file mode 100644 index 0000000000..8797190016 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/MyLogWorker.cs @@ -0,0 +1,36 @@ +using System; +using System.Threading.Tasks; +using Quartz; +using Volo.Abp.BackgroundWorkers.Quartz; + +namespace Volo.Abp.BackgroundJobs.DemoApp.Quartz2; + +public class MyLogWorker : QuartzBackgroundWorkerBase +{ + public MyLogWorker() + { + JobDetail = JobBuilder.Create().WithIdentity(nameof(MyLogWorker)).Build(); + Trigger = TriggerBuilder.Create().WithIdentity(nameof(MyLogWorker)).StartAt(DateTimeOffset.Now.AddSeconds(10)).Build(); + } + + public override Task Execute(IJobExecutionContext context) + { + Console.WriteLine("Executed MyLogWorker..!"); + return Task.CompletedTask; + } +} + +public class MyLogWorker2 : QuartzBackgroundWorkerBase +{ + public MyLogWorker2() + { + JobDetail = JobBuilder.Create().WithIdentity(nameof(MyLogWorker2)).Build(); + Trigger = TriggerBuilder.Create().WithIdentity(nameof(MyLogWorker2)).StartAt(DateTimeOffset.Now.AddSeconds(10)).Build(); + } + + public override Task Execute(IJobExecutionContext context) + { + Console.WriteLine("Executed MyLogWorker2..!"); + return Task.CompletedTask; + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Program.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Program.cs new file mode 100644 index 0000000000..1dfcbe3a08 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Program.cs @@ -0,0 +1,40 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Quartz; +using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs; + +namespace Volo.Abp.BackgroundJobs.DemoApp.Quartz2; + +class Program +{ + async static Task Main(string[] args) + { + using (var application = await AbpApplicationFactory.CreateAsync(options => + { + options.UseAutofac(); + })) + { + await application.InitializeAsync(); + + await CancelableBackgroundJobAsync(application.ServiceProvider); + + Console.WriteLine("Started: " + typeof(Program).Namespace); + Console.WriteLine("Press ENTER to stop the application..!"); + Console.ReadLine(); + + await application.ShutdownAsync(); + } + } + + private async static 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 scheduler = serviceProvider.GetRequiredService(); + await scheduler.Interrupt(new JobKey(jobId.Split('.')[1],jobId.Split('.')[0])); + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Volo.Abp.BackgroundJobs.DemoApp.Quartz.abppkg b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Volo.Abp.BackgroundJobs.DemoApp.Quartz.abppkg new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Volo.Abp.BackgroundJobs.DemoApp.Quartz.abppkg @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Volo.Abp.BackgroundJobs.DemoApp.Quartz.abppkg.analyze.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Volo.Abp.BackgroundJobs.DemoApp.Quartz.abppkg.analyze.json new file mode 100644 index 0000000000..0e42c5e73d --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Volo.Abp.BackgroundJobs.DemoApp.Quartz.abppkg.analyze.json @@ -0,0 +1,73 @@ +{ + "name": "Volo.Abp.BackgroundJobs.DemoApp.Quartz", + "hash": "04c9d2c41471882c5aed1a69db51d7df", + "contents": [ + { + "namespace": "Volo.Abp.BackgroundJobs.DemoApp.Quartz", + "dependsOnModules": [ + { + "declaringAssemblyName": "Volo.Abp.BackgroundJobs.DemoApp.Shared", + "namespace": "Volo.Abp.BackgroundJobs.DemoApp.Shared", + "name": "DemoAppSharedModule" + }, + { + "declaringAssemblyName": "Volo.Abp.Autofac", + "namespace": "Volo.Abp.Autofac", + "name": "AbpAutofacModule" + }, + { + "declaringAssemblyName": "Volo.Abp.BackgroundJobs.Quartz", + "namespace": "Volo.Abp.BackgroundJobs.Quartz", + "name": "AbpBackgroundJobsQuartzModule" + } + ], + "implementingInterfaces": [ + { + "name": "IAbpModule", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IAbpModule" + }, + { + "name": "IOnPreApplicationInitialization", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IOnPreApplicationInitialization" + }, + { + "name": "IOnApplicationInitialization", + "namespace": "Volo.Abp", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.IOnApplicationInitialization" + }, + { + "name": "IOnPostApplicationInitialization", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IOnPostApplicationInitialization" + }, + { + "name": "IOnApplicationShutdown", + "namespace": "Volo.Abp", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.IOnApplicationShutdown" + }, + { + "name": "IPreConfigureServices", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IPreConfigureServices" + }, + { + "name": "IPostConfigureServices", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IPostConfigureServices" + } + ], + "contentType": "abpModule", + "name": "DemoAppQuartzModule", + "summary": null + } + ] +} \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Volo.Abp.BackgroundJobs.DemoApp.Quartz2.csproj b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Volo.Abp.BackgroundJobs.DemoApp.Quartz2.csproj new file mode 100644 index 0000000000..2fc5bb394e --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/Volo.Abp.BackgroundJobs.DemoApp.Quartz2.csproj @@ -0,0 +1,27 @@ + + + + Exe + net9.0 + Volo.Abp.BackgroundJobs.DemoApp.Quartz2 + + + + + + + + + + + + + + + + + Always + + + + diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/appsettings.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/appsettings.json new file mode 100644 index 0000000000..27bf0485bd --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz2/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "Default": "Server=localhost;Database=BackgroundJobsDemoApp;User Id=sa;Password=1q2w3E***;TrustServerCertificate=True" + } +} \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/DemoAppRabbitMqModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/DemoAppRabbitMqModule.cs index 952d99da45..93aacea6ba 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/DemoAppRabbitMqModule.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/DemoAppRabbitMqModule.cs @@ -1,5 +1,8 @@ -using Volo.Abp.Autofac; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; using Volo.Abp.BackgroundJobs.DemoApp.Shared; +using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs; using Volo.Abp.BackgroundJobs.RabbitMQ; using Volo.Abp.Modularity; @@ -12,5 +15,18 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.RabbitMq; )] public class DemoAppRabbitMqModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + PreConfigure(options => + { + options.DefaultQueueNamePrefix = context.Services.GetApplicationName()!.EndsWith('.') + options.DefaultQueueNamePrefix; + options.DefaultDelayedQueueNamePrefix = context.Services.GetApplicationName()!.EndsWith('.') + options.DefaultDelayedQueueNamePrefix; + }); + } + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + var jobManager = context.ServiceProvider.GetRequiredService(); + await jobManager.EnqueueAsync(new RabbitMqJobArgs() { Value = "test 1 (rabbitmq)" }); + } } diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/RabbitMqJob.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/RabbitMqJob.cs new file mode 100644 index 0000000000..81ed0e2add --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/RabbitMqJob.cs @@ -0,0 +1,13 @@ +using System; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.BackgroundJobs.DemoApp.RabbitMq +{ + public class RabbitMqJob : BackgroundJob, ITransientDependency + { + public override void Execute(RabbitMqJobArgs args) + { + Console.WriteLine($"[RabbitMqJob] Started: {args.Value}"); + } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/RabbitMqJobArgs.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/RabbitMqJobArgs.cs new file mode 100644 index 0000000000..08601b18d5 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/RabbitMqJobArgs.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.BackgroundJobs.DemoApp.RabbitMq +{ + public class RabbitMqJobArgs + { + public string Value { get; set; } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/appsettings.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/appsettings.json new file mode 100644 index 0000000000..3710a3e8d7 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/appsettings.json @@ -0,0 +1,9 @@ +{ + "RabbitMQ": { + "Connections": { + "Default": { + "HostName": "localhost" + } + } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs index 976705b07d..368c8ea427 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs @@ -1,7 +1,9 @@ using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Autofac; using Volo.Abp.BackgroundJobs.DemoApp.Shared; using Volo.Abp.BackgroundJobs.EntityFrameworkCore; +using Volo.Abp.BackgroundWorkers; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.SqlServer; using Volo.Abp.Modularity; @@ -16,6 +18,14 @@ namespace Volo.Abp.BackgroundJobs.DemoApp; )] public class DemoAppModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + PreConfigure(options => + { + options.ApplicationName = context.Services.GetApplicationName()!; + }); + } + public override void ConfigureServices(ServiceConfigurationContext context) { Configure(options => @@ -29,13 +39,14 @@ public class DemoAppModule : AbpModule Configure(options => { //Configure for fast running + options.ApplicationName = context.Services.GetApplicationName()!; options.JobPollPeriod = 1000; options.DefaultFirstWaitDuration = 1; options.DefaultWaitFactor = 1; }); } - public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { //TODO: Configure console logging //context @@ -43,6 +54,7 @@ public class DemoAppModule : AbpModule // .GetRequiredService() // .AddConsole(LogLevel.Debug); - return Task.CompletedTask; + await context.AddBackgroundWorkerAsync(); + await context.AddBackgroundWorkerAsync(); } } diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20201013055401_Initial.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20201013055401_Initial.cs deleted file mode 100644 index aa50d3f32a..0000000000 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20201013055401_Initial.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations; - -public partial class Initial : Migration -{ - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "AbpBackgroundJobs", - columns: table => new { - Id = table.Column(nullable: false), - ExtraProperties = table.Column(nullable: true), - ConcurrencyStamp = table.Column(maxLength: 40, nullable: true), - JobName = table.Column(maxLength: 128, nullable: false), - JobArgs = table.Column(maxLength: 1048576, nullable: false), - TryCount = table.Column(nullable: false, defaultValue: (short)0), - CreationTime = table.Column(nullable: false), - NextTryTime = table.Column(nullable: false), - LastTryTime = table.Column(nullable: true), - IsAbandoned = table.Column(nullable: false, defaultValue: false), - Priority = table.Column(nullable: false, defaultValue: (byte)15) - }, - constraints: table => - { - table.PrimaryKey("PK_AbpBackgroundJobs", x => x.Id); - }); - - migrationBuilder.CreateIndex( - name: "IX_AbpBackgroundJobs_IsAbandoned_NextTryTime", - table: "AbpBackgroundJobs", - columns: new[] { "IsAbandoned", "NextTryTime" }); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "AbpBackgroundJobs"); - } -} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20201013055401_Initial.Designer.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20250222094840_Initial.Designer.cs similarity index 73% rename from modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20201013055401_Initial.Designer.cs rename to modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20250222094840_Initial.Designer.cs index 35129e8877..f64372426b 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20201013055401_Initial.Designer.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20250222094840_Initial.Designer.cs @@ -8,20 +8,24 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Volo.Abp.BackgroundJobs.DemoApp.Db; using Volo.Abp.EntityFrameworkCore; +#nullable disable + namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations { [DbContext(typeof(DemoAppDbContext))] - [Migration("20201013055401_Initial")] + [Migration("20250222094840_Initial")] partial class Initial { + /// protected override void BuildTargetModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder .HasAnnotation("_Abp_DatabaseProvider", EfCoreDatabaseProvider.SqlServer) - .HasAnnotation("ProductVersion", "3.1.8") - .HasAnnotation("Relational:MaxIdentifierLength", 128) - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); modelBuilder.Entity("Volo.Abp.BackgroundJobs.BackgroundJobRecord", b => { @@ -29,19 +33,25 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations .ValueGeneratedOnAdd() .HasColumnType("uniqueidentifier"); + b.Property("ApplicationName") + .HasMaxLength(96) + .HasColumnType("nvarchar(96)"); + b.Property("ConcurrencyStamp") .IsConcurrencyToken() - .HasColumnName("ConcurrencyStamp") + .IsRequired() + .HasMaxLength(40) .HasColumnType("nvarchar(40)") - .HasMaxLength(40); + .HasColumnName("ConcurrencyStamp"); b.Property("CreationTime") - .HasColumnName("CreationTime") - .HasColumnType("datetime2"); + .HasColumnType("datetime2") + .HasColumnName("CreationTime"); b.Property("ExtraProperties") - .HasColumnName("ExtraProperties") - .HasColumnType("nvarchar(max)"); + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("ExtraProperties"); b.Property("IsAbandoned") .ValueGeneratedOnAdd() @@ -50,13 +60,13 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations b.Property("JobArgs") .IsRequired() - .HasColumnType("nvarchar(max)") - .HasMaxLength(1048576); + .HasMaxLength(1048576) + .HasColumnType("nvarchar(max)"); b.Property("JobName") .IsRequired() - .HasColumnType("nvarchar(128)") - .HasMaxLength(128); + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); b.Property("LastTryTime") .HasColumnType("datetime2"); @@ -78,7 +88,7 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations b.HasIndex("IsAbandoned", "NextTryTime"); - b.ToTable("AbpBackgroundJobs"); + b.ToTable("AbpBackgroundJobs", (string)null); }); #pragma warning restore 612, 618 } diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20250222094840_Initial.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20250222094840_Initial.cs new file mode 100644 index 0000000000..ab0f0d7f37 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/20250222094840_Initial.cs @@ -0,0 +1,49 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AbpBackgroundJobs", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + ApplicationName = table.Column(type: "nvarchar(96)", maxLength: 96, nullable: true), + JobName = table.Column(type: "nvarchar(128)", maxLength: 128, nullable: false), + JobArgs = table.Column(type: "nvarchar(max)", maxLength: 1048576, nullable: false), + TryCount = table.Column(type: "smallint", nullable: false, defaultValue: (short)0), + CreationTime = table.Column(type: "datetime2", nullable: false), + NextTryTime = table.Column(type: "datetime2", nullable: false), + LastTryTime = table.Column(type: "datetime2", nullable: true), + IsAbandoned = table.Column(type: "bit", nullable: false, defaultValue: false), + Priority = table.Column(type: "tinyint", nullable: false, defaultValue: (byte)15), + ExtraProperties = table.Column(type: "nvarchar(max)", nullable: false), + ConcurrencyStamp = table.Column(type: "nvarchar(40)", maxLength: 40, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AbpBackgroundJobs", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_AbpBackgroundJobs_IsAbandoned_NextTryTime", + table: "AbpBackgroundJobs", + columns: new[] { "IsAbandoned", "NextTryTime" }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AbpBackgroundJobs"); + } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/DemoAppDbContextModelSnapshot.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/DemoAppDbContextModelSnapshot.cs index 47ee56f4bb..5752f2ff2d 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/DemoAppDbContextModelSnapshot.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Migrations/DemoAppDbContextModelSnapshot.cs @@ -7,6 +7,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Volo.Abp.BackgroundJobs.DemoApp.Db; using Volo.Abp.EntityFrameworkCore; +#nullable disable + namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations { [DbContext(typeof(DemoAppDbContext))] @@ -17,9 +19,10 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations #pragma warning disable 612, 618 modelBuilder .HasAnnotation("_Abp_DatabaseProvider", EfCoreDatabaseProvider.SqlServer) - .HasAnnotation("ProductVersion", "3.1.8") - .HasAnnotation("Relational:MaxIdentifierLength", 128) - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); modelBuilder.Entity("Volo.Abp.BackgroundJobs.BackgroundJobRecord", b => { @@ -27,19 +30,25 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations .ValueGeneratedOnAdd() .HasColumnType("uniqueidentifier"); + b.Property("ApplicationName") + .HasMaxLength(96) + .HasColumnType("nvarchar(96)"); + b.Property("ConcurrencyStamp") .IsConcurrencyToken() - .HasColumnName("ConcurrencyStamp") + .IsRequired() + .HasMaxLength(40) .HasColumnType("nvarchar(40)") - .HasMaxLength(40); + .HasColumnName("ConcurrencyStamp"); b.Property("CreationTime") - .HasColumnName("CreationTime") - .HasColumnType("datetime2"); + .HasColumnType("datetime2") + .HasColumnName("CreationTime"); b.Property("ExtraProperties") - .HasColumnName("ExtraProperties") - .HasColumnType("nvarchar(max)"); + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("ExtraProperties"); b.Property("IsAbandoned") .ValueGeneratedOnAdd() @@ -48,13 +57,13 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations b.Property("JobArgs") .IsRequired() - .HasColumnType("nvarchar(max)") - .HasMaxLength(1048576); + .HasMaxLength(1048576) + .HasColumnType("nvarchar(max)"); b.Property("JobName") .IsRequired() - .HasColumnType("nvarchar(128)") - .HasMaxLength(128); + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); b.Property("LastTryTime") .HasColumnType("datetime2"); @@ -76,7 +85,7 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations b.HasIndex("IsAbandoned", "NextTryTime"); - b.ToTable("AbpBackgroundJobs"); + b.ToTable("AbpBackgroundJobs", (string)null); }); #pragma warning restore 612, 618 } diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/MyWorker.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/MyWorker.cs new file mode 100644 index 0000000000..1be9111532 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/MyWorker.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Volo.Abp.BackgroundWorkers; + +namespace Volo.Abp.BackgroundJobs.DemoApp; + +public class MyWorker : BackgroundWorkerBase +{ + public override Task StartAsync(CancellationToken cancellationToken = default) + { + Console.WriteLine("MyWorker started..!"); + return Task.CompletedTask; + } + + public override Task StopAsync(CancellationToken cancellationToken = default) + { + Console.WriteLine("MyWorker stopped..!"); + return Task.CompletedTask; + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/PassiveUserCheckerWorker.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/PassiveUserCheckerWorker.cs new file mode 100644 index 0000000000..c185d7cee1 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/PassiveUserCheckerWorker.cs @@ -0,0 +1,24 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Volo.Abp.BackgroundWorkers; +using Volo.Abp.Threading; + +namespace Volo.Abp.BackgroundJobs.DemoApp; + +public class PassiveUserCheckerWorker : AsyncPeriodicBackgroundWorkerBase +{ + public PassiveUserCheckerWorker(AbpAsyncTimer timer, IServiceScopeFactory serviceScopeFactory) + : base(timer, serviceScopeFactory) + { + Timer.Period = 3000; //3 seconds + } + + protected override Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) + { + Console.WriteLine("Starting: Setting status of inactive users..."); + Console.WriteLine("Completed: Setting status of inactive users..."); + return Task.CompletedTask; + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Volo.Abp.BackgroundJobs.DemoApp.csproj b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Volo.Abp.BackgroundJobs.DemoApp.csproj index 4e377ce512..0ba5f2d834 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Volo.Abp.BackgroundJobs.DemoApp.csproj +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Volo.Abp.BackgroundJobs.DemoApp.csproj @@ -3,6 +3,7 @@ Exe net9.0 + Volo.Abp.BackgroundJobs.DemoApp diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/appsettings.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/appsettings.json index f80742fd70..372c9a4d9b 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/appsettings.json +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/appsettings.json @@ -1,5 +1,5 @@ { "ConnectionStrings": { - "Default": "Server=localhost;Database=BackgroundJobsDemoApp;Trusted_Connection=True;TrustServerCertificate=True" + "Default": "Server=localhost;Database=BackgroundJobsDemoApp;User Id=sa;Password=1q2w3E***;TrustServerCertificate=True" } -} \ No newline at end of file +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Db/DemoAppDbContext.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Db/DemoAppDbContext.cs new file mode 100644 index 0000000000..f66d128ca3 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Db/DemoAppDbContext.cs @@ -0,0 +1,21 @@ +using Microsoft.EntityFrameworkCore; +using Volo.Abp.BackgroundJobs.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; + +namespace Volo.Abp.BackgroundJobs.DemoApp2.Db; + +public class DemoAppDbContext : AbpDbContext +{ + public DemoAppDbContext(DbContextOptions options) + : base(options) + { + + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.ConfigureBackgroundJobs(); + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Db/DemoAppDbContextFactory.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Db/DemoAppDbContextFactory.cs new file mode 100644 index 0000000000..1729a3d489 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Db/DemoAppDbContextFactory.cs @@ -0,0 +1,28 @@ +using System.IO; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using Microsoft.Extensions.Configuration; + +namespace Volo.Abp.BackgroundJobs.DemoApp2.Db; + +public class DemoAppDbContextFactory : IDesignTimeDbContextFactory +{ + public DemoAppDbContext CreateDbContext(string[] args) + { + var configuration = BuildConfiguration(); + + var builder = new DbContextOptionsBuilder() + .UseSqlServer(configuration.GetConnectionString("Default")); + + return new DemoAppDbContext(builder.Options); + } + + private static IConfigurationRoot BuildConfiguration() + { + var builder = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false); + + return builder.Build(); + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/DemoAppModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/DemoAppModule.cs new file mode 100644 index 0000000000..baba74ce07 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/DemoAppModule.cs @@ -0,0 +1,60 @@ +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; +using Volo.Abp.BackgroundJobs.DemoApp.Shared; +using Volo.Abp.BackgroundJobs.EntityFrameworkCore; +using Volo.Abp.BackgroundWorkers; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore.SqlServer; +using Volo.Abp.Modularity; + +namespace Volo.Abp.BackgroundJobs.DemoApp2; + +[DependsOn( + typeof(DemoAppSharedModule), + typeof(AbpBackgroundJobsEntityFrameworkCoreModule), + typeof(AbpAutofacModule), + typeof(AbpEntityFrameworkCoreSqlServerModule) + )] +public class DemoAppModule : AbpModule +{ + public override void PreConfigureServices(ServiceConfigurationContext context) + { + PreConfigure(options => + { + options.ApplicationName = context.Services.GetApplicationName()!; + }); + } + + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.Configure(opts => + { + opts.UseSqlServer(); + }); + }); + + Configure(options => + { + //Configure for fast running + options.ApplicationName = context.Services.GetApplicationName()!; + options.JobPollPeriod = 1000; + options.DefaultFirstWaitDuration = 1; + options.DefaultWaitFactor = 1; + }); + } + + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + //TODO: Configure console logging + //context + // .ServiceProvider + // .GetRequiredService() + // .AddConsole(LogLevel.Debug); + + await context.AddBackgroundWorkerAsync(); + await context.AddBackgroundWorkerAsync(); + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Migrations/20250222094817_Initial.Designer.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Migrations/20250222094817_Initial.Designer.cs new file mode 100644 index 0000000000..4c4e8e2ea3 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Migrations/20250222094817_Initial.Designer.cs @@ -0,0 +1,96 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Volo.Abp.BackgroundJobs.DemoApp2.Db; +using Volo.Abp.EntityFrameworkCore; + +#nullable disable + +namespace Volo.Abp.BackgroundJobs.DemoApp2.Migrations +{ + [DbContext(typeof(DemoAppDbContext))] + [Migration("20250222094817_Initial")] + partial class Initial + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("_Abp_DatabaseProvider", EfCoreDatabaseProvider.SqlServer) + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Volo.Abp.BackgroundJobs.BackgroundJobRecord", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("ApplicationName") + .HasMaxLength(96) + .HasColumnType("nvarchar(96)"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .IsRequired() + .HasMaxLength(40) + .HasColumnType("nvarchar(40)") + .HasColumnName("ConcurrencyStamp"); + + b.Property("CreationTime") + .HasColumnType("datetime2") + .HasColumnName("CreationTime"); + + b.Property("ExtraProperties") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("ExtraProperties"); + + b.Property("IsAbandoned") + .ValueGeneratedOnAdd() + .HasColumnType("bit") + .HasDefaultValue(false); + + b.Property("JobArgs") + .IsRequired() + .HasMaxLength(1048576) + .HasColumnType("nvarchar(max)"); + + b.Property("JobName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); + + b.Property("LastTryTime") + .HasColumnType("datetime2"); + + b.Property("NextTryTime") + .HasColumnType("datetime2"); + + b.Property("Priority") + .ValueGeneratedOnAdd() + .HasColumnType("tinyint") + .HasDefaultValue((byte)15); + + b.Property("TryCount") + .ValueGeneratedOnAdd() + .HasColumnType("smallint") + .HasDefaultValue((short)0); + + b.HasKey("Id"); + + b.HasIndex("IsAbandoned", "NextTryTime"); + + b.ToTable("AbpBackgroundJobs", (string)null); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Migrations/20250222094817_Initial.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Migrations/20250222094817_Initial.cs new file mode 100644 index 0000000000..03cc5521b4 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Migrations/20250222094817_Initial.cs @@ -0,0 +1,49 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Volo.Abp.BackgroundJobs.DemoApp2.Migrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AbpBackgroundJobs", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + ApplicationName = table.Column(type: "nvarchar(96)", maxLength: 96, nullable: true), + JobName = table.Column(type: "nvarchar(128)", maxLength: 128, nullable: false), + JobArgs = table.Column(type: "nvarchar(max)", maxLength: 1048576, nullable: false), + TryCount = table.Column(type: "smallint", nullable: false, defaultValue: (short)0), + CreationTime = table.Column(type: "datetime2", nullable: false), + NextTryTime = table.Column(type: "datetime2", nullable: false), + LastTryTime = table.Column(type: "datetime2", nullable: true), + IsAbandoned = table.Column(type: "bit", nullable: false, defaultValue: false), + Priority = table.Column(type: "tinyint", nullable: false, defaultValue: (byte)15), + ExtraProperties = table.Column(type: "nvarchar(max)", nullable: false), + ConcurrencyStamp = table.Column(type: "nvarchar(40)", maxLength: 40, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AbpBackgroundJobs", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_AbpBackgroundJobs_IsAbandoned_NextTryTime", + table: "AbpBackgroundJobs", + columns: new[] { "IsAbandoned", "NextTryTime" }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AbpBackgroundJobs"); + } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Migrations/DemoAppDbContextModelSnapshot.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Migrations/DemoAppDbContextModelSnapshot.cs new file mode 100644 index 0000000000..58d76721c0 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Migrations/DemoAppDbContextModelSnapshot.cs @@ -0,0 +1,93 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Volo.Abp.BackgroundJobs.DemoApp2.Db; +using Volo.Abp.EntityFrameworkCore; + +#nullable disable + +namespace Volo.Abp.BackgroundJobs.DemoApp2.Migrations +{ + [DbContext(typeof(DemoAppDbContext))] + partial class DemoAppDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("_Abp_DatabaseProvider", EfCoreDatabaseProvider.SqlServer) + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Volo.Abp.BackgroundJobs.BackgroundJobRecord", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("ApplicationName") + .HasMaxLength(96) + .HasColumnType("nvarchar(96)"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .IsRequired() + .HasMaxLength(40) + .HasColumnType("nvarchar(40)") + .HasColumnName("ConcurrencyStamp"); + + b.Property("CreationTime") + .HasColumnType("datetime2") + .HasColumnName("CreationTime"); + + b.Property("ExtraProperties") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("ExtraProperties"); + + b.Property("IsAbandoned") + .ValueGeneratedOnAdd() + .HasColumnType("bit") + .HasDefaultValue(false); + + b.Property("JobArgs") + .IsRequired() + .HasMaxLength(1048576) + .HasColumnType("nvarchar(max)"); + + b.Property("JobName") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); + + b.Property("LastTryTime") + .HasColumnType("datetime2"); + + b.Property("NextTryTime") + .HasColumnType("datetime2"); + + b.Property("Priority") + .ValueGeneratedOnAdd() + .HasColumnType("tinyint") + .HasDefaultValue((byte)15); + + b.Property("TryCount") + .ValueGeneratedOnAdd() + .HasColumnType("smallint") + .HasDefaultValue((short)0); + + b.HasKey("Id"); + + b.HasIndex("IsAbandoned", "NextTryTime"); + + b.ToTable("AbpBackgroundJobs", (string)null); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/MyWorker.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/MyWorker.cs new file mode 100644 index 0000000000..41f2fb7efe --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/MyWorker.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Volo.Abp.BackgroundWorkers; + +namespace Volo.Abp.BackgroundJobs.DemoApp2; + +public class MyWorker : BackgroundWorkerBase +{ + public override Task StartAsync(CancellationToken cancellationToken = default) + { + Console.WriteLine("MyWorker started..!"); + return Task.CompletedTask; + } + + public override Task StopAsync(CancellationToken cancellationToken = default) + { + Console.WriteLine("MyWorker stopped..!"); + return Task.CompletedTask; + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/PassiveUserCheckerWorker.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/PassiveUserCheckerWorker.cs new file mode 100644 index 0000000000..e82da31431 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/PassiveUserCheckerWorker.cs @@ -0,0 +1,23 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.BackgroundWorkers; +using Volo.Abp.Threading; + +namespace Volo.Abp.BackgroundJobs.DemoApp2; + +public class PassiveUserCheckerWorker : AsyncPeriodicBackgroundWorkerBase +{ + public PassiveUserCheckerWorker(AbpAsyncTimer timer, IServiceScopeFactory serviceScopeFactory) + : base(timer, serviceScopeFactory) + { + Timer.Period = 3000; //3 seconds + } + + protected override Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) + { + Console.WriteLine("Starting: Setting status of inactive users..."); + Console.WriteLine("Completed: Setting status of inactive users..."); + return Task.CompletedTask; + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Program.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Program.cs new file mode 100644 index 0000000000..69a8d553f7 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Threading.Tasks; + +namespace Volo.Abp.BackgroundJobs.DemoApp2; + +class Program +{ + async static Task Main(string[] args) + { + using (var application = await AbpApplicationFactory.CreateAsync(options => + { + options.UseAutofac(); + })) + { + await application.InitializeAsync(); + + Console.WriteLine("Started: " + typeof(Program).Namespace); + Console.WriteLine("Press ENTER to stop the application..!"); + Console.ReadLine(); + + await application.ShutdownAsync(); + } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Volo.Abp.BackgroundJobs.DemoApp.abppkg b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Volo.Abp.BackgroundJobs.DemoApp.abppkg new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Volo.Abp.BackgroundJobs.DemoApp.abppkg @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Volo.Abp.BackgroundJobs.DemoApp.abppkg.analyze.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Volo.Abp.BackgroundJobs.DemoApp.abppkg.analyze.json new file mode 100644 index 0000000000..32ae62b201 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Volo.Abp.BackgroundJobs.DemoApp.abppkg.analyze.json @@ -0,0 +1,78 @@ +{ + "name": "Volo.Abp.BackgroundJobs.DemoApp", + "hash": "9ef6206e41d1a33e5255974ae2b57ad0", + "contents": [ + { + "namespace": "Volo.Abp.BackgroundJobs.DemoApp", + "dependsOnModules": [ + { + "declaringAssemblyName": "Volo.Abp.BackgroundJobs.DemoApp.Shared", + "namespace": "Volo.Abp.BackgroundJobs.DemoApp.Shared", + "name": "DemoAppSharedModule" + }, + { + "declaringAssemblyName": "Volo.Abp.BackgroundJobs.EntityFrameworkCore", + "namespace": "Volo.Abp.BackgroundJobs.EntityFrameworkCore", + "name": "AbpBackgroundJobsEntityFrameworkCoreModule" + }, + { + "declaringAssemblyName": "Volo.Abp.Autofac", + "namespace": "Volo.Abp.Autofac", + "name": "AbpAutofacModule" + }, + { + "declaringAssemblyName": "Volo.Abp.EntityFrameworkCore.SqlServer", + "namespace": "Volo.Abp.EntityFrameworkCore.SqlServer", + "name": "AbpEntityFrameworkCoreSqlServerModule" + } + ], + "implementingInterfaces": [ + { + "name": "IAbpModule", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IAbpModule" + }, + { + "name": "IOnPreApplicationInitialization", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IOnPreApplicationInitialization" + }, + { + "name": "IOnApplicationInitialization", + "namespace": "Volo.Abp", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.IOnApplicationInitialization" + }, + { + "name": "IOnPostApplicationInitialization", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IOnPostApplicationInitialization" + }, + { + "name": "IOnApplicationShutdown", + "namespace": "Volo.Abp", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.IOnApplicationShutdown" + }, + { + "name": "IPreConfigureServices", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IPreConfigureServices" + }, + { + "name": "IPostConfigureServices", + "namespace": "Volo.Abp.Modularity", + "declaringAssemblyName": "Volo.Abp.Core", + "fullName": "Volo.Abp.Modularity.IPostConfigureServices" + } + ], + "contentType": "abpModule", + "name": "DemoAppModule", + "summary": null + } + ] +} \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Volo.Abp.BackgroundJobs.DemoApp2.csproj b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Volo.Abp.BackgroundJobs.DemoApp2.csproj new file mode 100644 index 0000000000..ea1e1cae3e --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/Volo.Abp.BackgroundJobs.DemoApp2.csproj @@ -0,0 +1,26 @@ + + + + Exe + net9.0 + Volo.Abp.BackgroundJobs.DemoApp2 + + + + + all + runtime; build; native; contentfiles; analyzers + + + + + + + + + + Always + + + + diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/appsettings.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/appsettings.json new file mode 100644 index 0000000000..372c9a4d9b --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp2/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "Default": "Server=localhost;Database=BackgroundJobsDemoApp;User Id=sa;Password=1q2w3E***;TrustServerCertificate=True" + } +}