mirror of https://github.com/abpframework/abp.git
21 changed files with 345 additions and 61 deletions
@ -1,19 +1,83 @@ |
|||||
using Microsoft.Extensions.DependencyInjection; |
using System.IO; |
||||
|
using Microsoft.Extensions.Configuration; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Logging; |
||||
using Volo.Abp.Autofac; |
using Volo.Abp.Autofac; |
||||
using Volo.Abp.BackgroundJobs.EntityFrameworkCore; |
using Volo.Abp.BackgroundJobs.EntityFrameworkCore; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.EntityFrameworkCore; |
||||
|
using Volo.Abp.EntityFrameworkCore.SqlServer; |
||||
using Volo.Abp.Modularity; |
using Volo.Abp.Modularity; |
||||
|
|
||||
namespace Volo.Abp.BackgroundJobs.DemoApp |
namespace Volo.Abp.BackgroundJobs.DemoApp |
||||
{ |
{ |
||||
[DependsOn( |
[DependsOn( |
||||
typeof(BackgroundJobsEntityFrameworkCoreModule), |
typeof(BackgroundJobsEntityFrameworkCoreModule), |
||||
typeof(AbpAutofacModule) |
typeof(AbpAutofacModule), |
||||
|
typeof(AbpEntityFrameworkCoreSqlServerModule) |
||||
)] |
)] |
||||
public class DemoAppModule : AbpModule |
public class DemoAppModule : AbpModule |
||||
{ |
{ |
||||
public override void ConfigureServices(ServiceConfigurationContext context) |
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
{ |
{ |
||||
|
var configuration = BuildConfiguration(); |
||||
|
context.Services.AddConfiguration(configuration); |
||||
|
|
||||
|
context.Services.Configure<DbConnectionOptions>(options => |
||||
|
{ |
||||
|
options.ConnectionStrings.Default = configuration.GetConnectionString("Default"); |
||||
|
}); |
||||
|
|
||||
|
context.Services.Configure<AbpDbContextOptions>(options => |
||||
|
{ |
||||
|
options.Configure(opts => |
||||
|
{ |
||||
|
opts.UseSqlServer(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
context.Services.Configure<BackgroundJobOptions>(options => |
||||
|
{ |
||||
|
options.JobPollPeriod = 1000; |
||||
|
}); |
||||
|
|
||||
context.Services.AddAssemblyOf<DemoAppModule>(); |
context.Services.AddAssemblyOf<DemoAppModule>(); |
||||
} |
} |
||||
|
|
||||
|
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
||||
|
{ |
||||
|
context |
||||
|
.ServiceProvider |
||||
|
.GetRequiredService<ILoggerFactory>() |
||||
|
.AddConsole(LogLevel.Debug); |
||||
|
} |
||||
|
|
||||
|
private static IConfigurationRoot BuildConfiguration(string fileName = "appsettings") |
||||
|
{ |
||||
|
var fileNameWithExtension = fileName + ".json"; |
||||
|
var directory = Directory.GetCurrentDirectory(); |
||||
|
|
||||
|
while (!File.Exists(Path.Combine(directory, fileNameWithExtension))) |
||||
|
{ |
||||
|
var parentDirectory = new DirectoryInfo(directory).Parent; |
||||
|
if (parentDirectory == null) |
||||
|
{ |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
directory = parentDirectory.FullName; |
||||
|
} |
||||
|
|
||||
|
if(File.Exists(Path.Combine(directory, fileNameWithExtension))) |
||||
|
{ |
||||
|
var builder = new ConfigurationBuilder() |
||||
|
.SetBasePath(directory) |
||||
|
.AddJsonFile(fileNameWithExtension, optional: false); |
||||
|
|
||||
|
return builder.Build(); |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,21 @@ |
|||||
|
using System; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
||||
|
{ |
||||
|
public class WriteToConsoleGreenJob : BackgroundJob<WriteToConsoleGreenJobArgs>, ITransientDependency |
||||
|
{ |
||||
|
public override void Execute(WriteToConsoleGreenJobArgs args) |
||||
|
{ |
||||
|
if (RandomHelper.GetRandomOf(1, 2) == 2) |
||||
|
{ |
||||
|
throw new ApplicationException("A sample exception from the WriteToConsoleGreenJob!"); |
||||
|
} |
||||
|
|
||||
|
var oldColor = Console.ForegroundColor; |
||||
|
Console.ForegroundColor = ConsoleColor.Green; |
||||
|
Console.WriteLine($"############### WriteToConsoleGreenJob: {args.Value} ###############"); |
||||
|
Console.ForegroundColor = oldColor; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
||||
|
{ |
||||
|
[BackgroundJobName("GreenJob")] |
||||
|
public class WriteToConsoleGreenJobArgs |
||||
|
{ |
||||
|
public string Value { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -1,13 +0,0 @@ |
|||||
using System; |
|
||||
using Volo.Abp.DependencyInjection; |
|
||||
|
|
||||
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
|
||||
{ |
|
||||
public class WriteToConsoleJob : BackgroundJob<WriteToConsoleJobArgs>, ITransientDependency |
|
||||
{ |
|
||||
public override void Execute(WriteToConsoleJobArgs args) |
|
||||
{ |
|
||||
Console.WriteLine($"WriteToConsoleJob: {args.Value}"); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,21 @@ |
|||||
|
using System; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
||||
|
{ |
||||
|
public class WriteToConsoleYellowJob : BackgroundJob<WriteToConsoleYellowJobArgs>, ITransientDependency |
||||
|
{ |
||||
|
public override void Execute(WriteToConsoleYellowJobArgs args) |
||||
|
{ |
||||
|
if (RandomHelper.GetRandomOf(1, 2) == 2) |
||||
|
{ |
||||
|
throw new ApplicationException("A sample exception from the WriteToConsoleYellowJob!"); |
||||
|
} |
||||
|
|
||||
|
var oldColor = Console.ForegroundColor; |
||||
|
Console.ForegroundColor = ConsoleColor.Yellow; |
||||
|
Console.WriteLine($"############### WriteToConsoleYellowJob: {args.Value} ###############"); |
||||
|
Console.ForegroundColor = oldColor; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,6 +1,7 @@ |
|||||
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
||||
{ |
{ |
||||
public class WriteToConsoleJobArgs |
[BackgroundJobName("YellowJob")] |
||||
|
public class WriteToConsoleYellowJobArgs |
||||
{ |
{ |
||||
public string Value { get; set; } |
public string Value { get; set; } |
||||
} |
} |
||||
@ -0,0 +1,66 @@ |
|||||
|
// <auto-generated />
|
||||
|
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; |
||||
|
using Volo.Abp.BackgroundJobs.DemoApp.Db; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations |
||||
|
{ |
||||
|
[DbContext(typeof(DemoAppDbContext))] |
||||
|
[Migration("20180724105416_BackgroundJobs_Initial")] |
||||
|
partial class BackgroundJobs_Initial |
||||
|
{ |
||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder) |
||||
|
{ |
||||
|
#pragma warning disable 612, 618
|
||||
|
modelBuilder |
||||
|
.HasAnnotation("ProductVersion", "2.1.0-rtm-30799") |
||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128) |
||||
|
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); |
||||
|
|
||||
|
modelBuilder.Entity("Volo.Abp.BackgroundJobs.BackgroundJobRecord", b => |
||||
|
{ |
||||
|
b.Property<Guid>("Id") |
||||
|
.ValueGeneratedOnAdd(); |
||||
|
|
||||
|
b.Property<DateTime>("CreationTime") |
||||
|
.HasColumnName("CreationTime"); |
||||
|
|
||||
|
b.Property<bool>("IsAbandoned") |
||||
|
.ValueGeneratedOnAdd() |
||||
|
.HasDefaultValue(false); |
||||
|
|
||||
|
b.Property<string>("JobArgs") |
||||
|
.IsRequired() |
||||
|
.HasMaxLength(1048576); |
||||
|
|
||||
|
b.Property<string>("JobName") |
||||
|
.IsRequired() |
||||
|
.HasMaxLength(128); |
||||
|
|
||||
|
b.Property<DateTime?>("LastTryTime"); |
||||
|
|
||||
|
b.Property<DateTime>("NextTryTime"); |
||||
|
|
||||
|
b.Property<byte>("Priority") |
||||
|
.ValueGeneratedOnAdd() |
||||
|
.HasDefaultValue((byte)15); |
||||
|
|
||||
|
b.Property<short>("TryCount") |
||||
|
.ValueGeneratedOnAdd() |
||||
|
.HasDefaultValue((short)0); |
||||
|
|
||||
|
b.HasKey("Id"); |
||||
|
|
||||
|
b.HasIndex("IsAbandoned", "NextTryTime"); |
||||
|
|
||||
|
b.ToTable("AbpBackgroundJobs"); |
||||
|
}); |
||||
|
#pragma warning restore 612, 618
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
using System; |
||||
|
using Microsoft.EntityFrameworkCore.Migrations; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations |
||||
|
{ |
||||
|
public partial class BackgroundJobs_Initial : Migration |
||||
|
{ |
||||
|
protected override void Up(MigrationBuilder migrationBuilder) |
||||
|
{ |
||||
|
migrationBuilder.CreateTable( |
||||
|
name: "AbpBackgroundJobs", |
||||
|
columns: table => new |
||||
|
{ |
||||
|
Id = table.Column<Guid>(nullable: false), |
||||
|
JobName = table.Column<string>(maxLength: 128, nullable: false), |
||||
|
JobArgs = table.Column<string>(maxLength: 1048576, nullable: false), |
||||
|
TryCount = table.Column<short>(nullable: false, defaultValue: (short)0), |
||||
|
CreationTime = table.Column<DateTime>(nullable: false), |
||||
|
NextTryTime = table.Column<DateTime>(nullable: false), |
||||
|
LastTryTime = table.Column<DateTime>(nullable: true), |
||||
|
IsAbandoned = table.Column<bool>(nullable: false, defaultValue: false), |
||||
|
Priority = table.Column<byte>(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"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
// <auto-generated />
|
||||
|
using System; |
||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
using Microsoft.EntityFrameworkCore.Infrastructure; |
||||
|
using Microsoft.EntityFrameworkCore.Metadata; |
||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
||||
|
using Volo.Abp.BackgroundJobs; |
||||
|
using Volo.Abp.BackgroundJobs.DemoApp.Db; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundJobs.DemoApp.Migrations |
||||
|
{ |
||||
|
[DbContext(typeof(DemoAppDbContext))] |
||||
|
partial class DemoAppDbContextModelSnapshot : ModelSnapshot |
||||
|
{ |
||||
|
protected override void BuildModel(ModelBuilder modelBuilder) |
||||
|
{ |
||||
|
#pragma warning disable 612, 618
|
||||
|
modelBuilder |
||||
|
.HasAnnotation("ProductVersion", "2.1.0-rtm-30799") |
||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128) |
||||
|
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); |
||||
|
|
||||
|
modelBuilder.Entity("Volo.Abp.BackgroundJobs.BackgroundJobRecord", b => |
||||
|
{ |
||||
|
b.Property<Guid>("Id") |
||||
|
.ValueGeneratedOnAdd(); |
||||
|
|
||||
|
b.Property<DateTime>("CreationTime") |
||||
|
.HasColumnName("CreationTime"); |
||||
|
|
||||
|
b.Property<bool>("IsAbandoned") |
||||
|
.ValueGeneratedOnAdd() |
||||
|
.HasDefaultValue(false); |
||||
|
|
||||
|
b.Property<string>("JobArgs") |
||||
|
.IsRequired() |
||||
|
.HasMaxLength(1048576); |
||||
|
|
||||
|
b.Property<string>("JobName") |
||||
|
.IsRequired() |
||||
|
.HasMaxLength(128); |
||||
|
|
||||
|
b.Property<DateTime?>("LastTryTime"); |
||||
|
|
||||
|
b.Property<DateTime>("NextTryTime"); |
||||
|
|
||||
|
b.Property<byte>("Priority") |
||||
|
.ValueGeneratedOnAdd() |
||||
|
.HasDefaultValue((byte)15); |
||||
|
|
||||
|
b.Property<short>("TryCount") |
||||
|
.ValueGeneratedOnAdd() |
||||
|
.HasDefaultValue((short)0); |
||||
|
|
||||
|
b.HasKey("Id"); |
||||
|
|
||||
|
b.HasIndex("IsAbandoned", "NextTryTime"); |
||||
|
|
||||
|
b.ToTable("AbpBackgroundJobs"); |
||||
|
}); |
||||
|
#pragma warning restore 612, 618
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue