From c21a3211041e8dc370365ca449fa2dba5208ab02 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Tue, 24 Jul 2018 13:48:54 +0300 Subject: [PATCH] Added sample app for the background jobs module --- .../Db/DemoAppDbContext.cs | 22 ++++++++++++++ .../Db/DemoAppDbContextFactory.cs | 29 +++++++++++++++++++ .../Jobs/SampleJobCreator.cs | 20 +++++++++++++ .../Jobs/WriteToConsoleJob.cs | 13 +++++++++ .../Jobs/WriteToConsoleJobArgs.cs | 7 +++++ .../Program.cs | 7 +++++ .../Volo.Abp.BackgroundJobs.DemoApp.csproj | 4 +++ .../appsettings.json | 5 ++++ 8 files changed, 107 insertions(+) create mode 100644 modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Db/DemoAppDbContext.cs create mode 100644 modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Db/DemoAppDbContextFactory.cs create mode 100644 modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/SampleJobCreator.cs create mode 100644 modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleJob.cs create mode 100644 modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleJobArgs.cs create mode 100644 modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/appsettings.json diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Db/DemoAppDbContext.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Db/DemoAppDbContext.cs new file mode 100644 index 0000000000..524c741dd7 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Db/DemoAppDbContext.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore; +using Volo.Abp.BackgroundJobs.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; + +namespace Volo.Abp.BackgroundJobs.DemoApp.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.DemoApp/Db/DemoAppDbContextFactory.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Db/DemoAppDbContextFactory.cs new file mode 100644 index 0000000000..95e9a7874a --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Db/DemoAppDbContextFactory.cs @@ -0,0 +1,29 @@ +using System.IO; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using Microsoft.Extensions.Configuration; + +namespace Volo.Abp.BackgroundJobs.DemoApp.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.DemoApp/Jobs/SampleJobCreator.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/SampleJobCreator.cs new file mode 100644 index 0000000000..9b0df9a0c2 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/SampleJobCreator.cs @@ -0,0 +1,20 @@ +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs +{ + public class SampleJobCreator : ITransientDependency + { + private readonly IBackgroundJobManager _backgroundJobManager; + + public SampleJobCreator(IBackgroundJobManager backgroundJobManager) + { + _backgroundJobManager = backgroundJobManager; + } + + public void CreateJobs() + { + _backgroundJobManager.Enqueue(new WriteToConsoleJobArgs { Value = "42" }); + _backgroundJobManager.Enqueue(new WriteToConsoleJobArgs { Value = "43" }); + } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleJob.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleJob.cs new file mode 100644 index 0000000000..e7c5514ee8 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleJob.cs @@ -0,0 +1,13 @@ +using System; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs +{ + public class WriteToConsoleJob : BackgroundJob, ITransientDependency + { + public override void Execute(WriteToConsoleJobArgs args) + { + Console.WriteLine($"WriteToConsoleJob: {args.Value}"); + } + } +} \ No newline at end of file diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleJobArgs.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleJobArgs.cs new file mode 100644 index 0000000000..f6cc94de8f --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleJobArgs.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs +{ + public class WriteToConsoleJobArgs + { + public string Value { get; set; } + } +} diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Program.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Program.cs index 63d8689c33..a6d4cf21c5 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Program.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Program.cs @@ -1,4 +1,6 @@ using System; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.BackgroundJobs.DemoApp.Jobs; namespace Volo.Abp.BackgroundJobs.DemoApp { @@ -13,6 +15,11 @@ namespace Volo.Abp.BackgroundJobs.DemoApp { application.Initialize(); + application + .ServiceProvider + .GetRequiredService() + .CreateJobs(); + Console.WriteLine("Press ENTER to stop the application..!"); Console.ReadLine(); } 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 c9d46445f1..06e07f2cef 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 @@ -6,8 +6,12 @@ + + + + diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/appsettings.json b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/appsettings.json new file mode 100644 index 0000000000..e1e6966497 --- /dev/null +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "Default": "Server=localhost;Database=BackgroundJobsDemoApp;Trusted_Connection=True;MultipleActiveResultSets=true" + } +} \ No newline at end of file