mirror of https://github.com/abpframework/abp.git
19 changed files with 303 additions and 48 deletions
@ -0,0 +1,24 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<AssemblyName>Volo.Abp.HangFire</AssemblyName> |
|||
<PackageId>Volo.Abp.HangFire</PackageId> |
|||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Hangfire.AspNetCore" Version="1.6.19" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,35 @@ |
|||
using Hangfire; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.Hangfire |
|||
{ |
|||
public class AbpHangfireModule : AbpModule |
|||
{ |
|||
private BackgroundJobServer _backgroundJobServer; |
|||
|
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddHangfire(configuration => |
|||
{ |
|||
context.Services.ExecutePreConfiguredActions(configuration); |
|||
}); |
|||
|
|||
context.Services.AddAssemblyOf<AbpHangfireModule>(); |
|||
} |
|||
|
|||
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
|||
{ |
|||
var options = context.ServiceProvider.GetRequiredService<IOptions<AbpHangfireOptions>>().Value; |
|||
_backgroundJobServer = options.BackgroundJobServerFactory.Invoke(context.ServiceProvider); |
|||
} |
|||
|
|||
public override void OnApplicationShutdown(ApplicationShutdownContext context) |
|||
{ |
|||
//TODO: ABP may provide two methods for application shutdown: OnPreApplicationShutdown & OnApplicationShutdown
|
|||
_backgroundJobServer.SendStop(); |
|||
_backgroundJobServer.Dispose(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Hangfire; |
|||
using Hangfire.Client; |
|||
using Hangfire.Common; |
|||
using Hangfire.Server; |
|||
using Hangfire.States; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Hangfire |
|||
{ |
|||
public class AbpHangfireOptions |
|||
{ |
|||
[CanBeNull] |
|||
public BackgroundJobServerOptions ServerOptions { get; set; } |
|||
|
|||
[CanBeNull] |
|||
public IEnumerable<IBackgroundProcess> AdditionalProcesses { get; set; } |
|||
|
|||
[CanBeNull] |
|||
public JobStorage Storage { get; set; } |
|||
|
|||
[NotNull] |
|||
public Func<IServiceProvider, BackgroundJobServer> BackgroundJobServerFactory |
|||
{ |
|||
get => _backgroundJobServerFactory; |
|||
set => _backgroundJobServerFactory = Check.NotNull(value, nameof(value)); |
|||
} |
|||
private Func<IServiceProvider, BackgroundJobServer> _backgroundJobServerFactory; |
|||
|
|||
public AbpHangfireOptions() |
|||
{ |
|||
_backgroundJobServerFactory = CreateJobServer; |
|||
} |
|||
|
|||
private BackgroundJobServer CreateJobServer(IServiceProvider serviceProvider) |
|||
{ |
|||
Storage = Storage ?? serviceProvider.GetRequiredService<JobStorage>(); |
|||
ServerOptions = ServerOptions ?? serviceProvider.GetService<BackgroundJobServerOptions>() ?? new BackgroundJobServerOptions(); |
|||
AdditionalProcesses = AdditionalProcesses ?? serviceProvider.GetServices<IBackgroundProcess>(); |
|||
|
|||
return new BackgroundJobServer(ServerOptions, Storage, AdditionalProcesses, |
|||
ServerOptions.FilterProvider ?? serviceProvider.GetRequiredService<IJobFilterProvider>(), |
|||
ServerOptions.Activator ?? serviceProvider.GetRequiredService<JobActivator>(), |
|||
serviceProvider.GetService<IBackgroundJobFactory>(), |
|||
serviceProvider.GetService<IBackgroundJobPerformer>(), |
|||
serviceProvider.GetService<IBackgroundJobStateChanger>() |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
using Hangfire; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Autofac; |
|||
using Volo.Abp.BackgroundJobs.DemoApp.Shared; |
|||
using Volo.Abp.Hangfire; |
|||
using Volo.Abp.Modularity; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.HangFire |
|||
{ |
|||
[DependsOn( |
|||
typeof(DemoAppSharedModule), |
|||
typeof(AbpAutofacModule), |
|||
typeof(AbpHangfireModule) |
|||
)] |
|||
public class DemoAppHangfireModule : AbpModule |
|||
{ |
|||
public override void PreConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
var configuration = ConfigurationHelper.BuildConfiguration(); |
|||
context.Services.AddConfiguration(configuration); |
|||
|
|||
context.Services.PreConfigure<IGlobalConfiguration>(hangfireConfiguration => |
|||
{ |
|||
hangfireConfiguration.UseSqlServerStorage(configuration.GetConnectionString("Default")); |
|||
}); |
|||
} |
|||
|
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddAssemblyOf<DemoAppHangfireModule>(); |
|||
} |
|||
|
|||
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
|||
{ |
|||
context |
|||
.ServiceProvider |
|||
.GetRequiredService<ILoggerFactory>() |
|||
.AddConsole(LogLevel.Debug); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.HangFire |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
using (var application = AbpApplicationFactory.Create<DemoAppHangfireModule>(options => |
|||
{ |
|||
options.UseAutofac(); |
|||
})) |
|||
{ |
|||
application.Initialize(); |
|||
|
|||
Console.WriteLine("Started: " + typeof(Program).Namespace); |
|||
Console.WriteLine("Press ENTER to stop the application..!"); |
|||
Console.ReadLine(); |
|||
|
|||
application.Shutdown(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<OutputType>Exe</OutputType> |
|||
<TargetFramework>netcoreapp2.1</TargetFramework> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Hangfire.SqlServer" Version="1.6.19" /> |
|||
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
|||
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.HangFire\Volo.Abp.HangFire.csproj" /> |
|||
<ProjectReference Include="..\Volo.Abp.BackgroundJobs.DemoApp.Shared\Volo.Abp.BackgroundJobs.DemoApp.Shared.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,37 @@ |
|||
using System.IO; |
|||
using Microsoft.Extensions.Configuration; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared |
|||
{ |
|||
public static class ConfigurationHelper |
|||
{ |
|||
public static IConfigurationRoot BuildConfiguration() |
|||
{ |
|||
const string fileName = "appsettings.json"; |
|||
|
|||
var directory = Directory.GetCurrentDirectory(); |
|||
|
|||
while (!File.Exists(Path.Combine(directory, fileName))) |
|||
{ |
|||
var parentDirectory = new DirectoryInfo(directory).Parent; |
|||
if (parentDirectory == null) |
|||
{ |
|||
break; |
|||
} |
|||
|
|||
directory = parentDirectory.FullName; |
|||
} |
|||
|
|||
if (File.Exists(Path.Combine(directory, fileName))) |
|||
{ |
|||
var builder = new ConfigurationBuilder() |
|||
.SetBasePath(directory) |
|||
.AddJsonFile(fileName, optional: false); |
|||
|
|||
return builder.Build(); |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpBackgroundJobsAbstractionsModule) |
|||
)] |
|||
public class DemoAppSharedModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddAssemblyOf<DemoAppSharedModule>(); |
|||
} |
|||
|
|||
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
|||
{ |
|||
context.ServiceProvider |
|||
.GetRequiredService<SampleJobCreator>() |
|||
.CreateJobs(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,6 +1,6 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
{ |
|||
public class SampleJobCreator : ITransientDependency |
|||
{ |
|||
@ -1,13 +1,13 @@ |
|||
using System; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
{ |
|||
public class WriteToConsoleGreenJob : BackgroundJob<WriteToConsoleGreenJobArgs>, ITransientDependency |
|||
{ |
|||
public override void Execute(WriteToConsoleGreenJobArgs args) |
|||
{ |
|||
if (RandomHelper.GetRandom(0,100) < 70) |
|||
if (RandomHelper.GetRandom(0, 100) < 70) |
|||
{ |
|||
throw new ApplicationException("A sample exception from the WriteToConsoleGreenJob!"); |
|||
} |
|||
@ -1,4 +1,4 @@ |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
{ |
|||
[BackgroundJobName("GreenJob")] |
|||
public class WriteToConsoleGreenJobArgs |
|||
@ -1,7 +1,7 @@ |
|||
using System; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
{ |
|||
public class WriteToConsoleYellowJob : BackgroundJob<WriteToConsoleYellowJobArgs>, ITransientDependency |
|||
{ |
|||
@ -1,4 +1,4 @@ |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs |
|||
namespace Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs |
|||
{ |
|||
[BackgroundJobName("YellowJob")] |
|||
public class WriteToConsoleYellowJobArgs |
|||
@ -0,0 +1,14 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.0" /> |
|||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" /> |
|||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.0" /> |
|||
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.BackgroundJobs.Abstractions\Volo.Abp.BackgroundJobs.Abstractions.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
Loading…
Reference in new issue