-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10292 from abpframework/liangshiwei/hangfire
Implement Hangfire Background Worker Manager
- Loading branch information
Showing
16 changed files
with
476 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Hangfire Background Worker Manager | ||
|
||
[Hangfire](https://https://www.hangfire.io/) is an advanced background jobs and worker manager. You can integrate Hangfire with the ABP Framework to use it instead of the [default background worker manager](Background-Workers.md). | ||
|
||
The major advantage is that you can use the same server farm to manage your Background Jobs and Workers, as well as leverage the advanced scheduling that is available from Hangfire for [Recurring Jobs](https://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html?highlight=recurring), aka Background Workers. | ||
|
||
## Installation | ||
|
||
It is suggested to use the [ABP CLI](CLI.md) to install this package. | ||
|
||
### Using the ABP CLI | ||
|
||
Open a command line window in the folder of the project (.csproj file) and type the following command: | ||
|
||
````bash | ||
abp add-package Volo.Abp.BackgroundWorkers.Hangfire | ||
```` | ||
|
||
### Manual Installation | ||
|
||
If you want to manually install; | ||
|
||
1. Add the [Volo.Abp.BackgroundWorkers.Hangfire](https://www.nuget.org/packages/Volo.Abp.BackgroundWorkers.Hangfire) NuGet package to your project: | ||
|
||
```` | ||
Install-Package Volo.Abp.BackgroundWorkers.Hangfire | ||
```` | ||
|
||
2. Add the `AbpBackgroundWorkersHangfireModule` to the dependency list of your module: | ||
|
||
````csharp | ||
[DependsOn( | ||
//...other dependencies | ||
typeof(AbpBackgroundWorkersHangfireModule) //Add the new module dependency | ||
)] | ||
public class YourModule : AbpModule | ||
{ | ||
} | ||
```` | ||
|
||
> Hangfire background worker integration provides an adapter `HangfirePeriodicBackgroundWorkerAdapter` to automatically load any `PeriodicBackgroundWorkerBase` and `AsyncPeriodicBackgroundWorkerBase` derived classes as `IHangfireBackgroundWorker` instances. This allows you to still to easily switch over to use Hangfire as the background manager even you have existing background workers that are based on the [default background workers implementation](Background-Workers.md). | ||
## Create a Background Worker | ||
|
||
`HangfireBackgroundWorkerBase` is an easy way to create a background worker. | ||
|
||
```` csharp | ||
public class MyLogWorker : HangfireBackgroundWorkerBase | ||
{ | ||
public MyLogWorker() | ||
{ | ||
RecurringJobId = nameof(MyLogWorker); | ||
CronExpression = Cron.Daily(); | ||
} | ||
|
||
public override Task DoWorkAsync() | ||
{ | ||
Logger.LogInformation("Executed MyLogWorker..!"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
```` | ||
|
||
* **RecurringJobId** Is an optional parameter, see [Hangfire document](https://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html) | ||
* **CronExpression** Is a CRON expression, see [CRON expression](https://en.wikipedia.org/wiki/Cron#CRON_expression) | ||
|
||
> You can directly implement the `IHangfireBackgroundWorker`, but `HangfireBackgroundWorkerBase` provides some useful properties like Logger. | ||
## Register BackgroundWorkerManager | ||
|
||
After creating a background worker class, you should add it to the `IBackgroundWorkerManager`. The most common place is the `OnApplicationInitialization` method of your module class: | ||
|
||
```` csharp | ||
[DependsOn(typeof(AbpBackgroundWorkersModule))] | ||
public class MyModule : AbpModule | ||
{ | ||
public override void OnApplicationInitialization( | ||
ApplicationInitializationContext context) | ||
{ | ||
context.AddBackgroundWorker<MyLogWorker>(); | ||
} | ||
} | ||
```` | ||
|
||
`context.AddBackgroundWorker(...)` is a shortcut extension method for the expression below: | ||
|
||
```` csharp | ||
context.ServiceProvider | ||
.GetRequiredService<IBackgroundWorkerManager>() | ||
.Add( | ||
context | ||
.ServiceProvider | ||
.GetRequiredService<MyLogWorker>() | ||
); | ||
```` | ||
|
||
So, it resolves the given background worker and adds to the `IBackgroundWorkerManager`. | ||
|
||
While we generally add workers in OnApplicationInitialization, there are no restrictions on that. You can inject IBackgroundWorkerManager anywhere and add workers at runtime. Background worker manager will stop and release all the registered workers when your application is being shut down. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Hangfire Background Worker Manager | ||
|
||
[Hangfire](https://https://www.hangfire.io/)是一个高级的后台工作者管理. 你可以用ABP框架Hangfire集成代替[默认后台工作者管理](Background-Workers.md). | ||
|
||
主要优点是你可以使用相同的服务器群来管理你的后台作业和工作线程以及利用 Hangfire 提供的[Recurring Jobs](https://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html?highlight=recurring)高级调度功能. | ||
|
||
## 安装 | ||
|
||
建议使用[ABP CLI](CLI.md)安装包. | ||
|
||
|
||
### 使用ABP CLI | ||
|
||
在项目的文件夹(.csproj文件)中打开命令行窗口输入以下命令: | ||
|
||
````bash | ||
abp add-package Volo.Abp.BackgroundWorkers.Hangfire | ||
```` | ||
|
||
### 手动安装 | ||
|
||
如果你想手动安装; | ||
|
||
1. 添加 [Volo.Abp.BackgroundWorkers.Hangfire](https://www.nuget.org/packages/Volo.Abp.BackgroundWorkers.Hangfire) NuGet包添加到你的项目: | ||
|
||
```` | ||
Install-Package Volo.Abp.BackgroundWorkers.Hangfire | ||
```` | ||
|
||
2. 添加 `AbpBackgroundWorkersHangfireModule` 到你的模块的依赖列表: | ||
|
||
````csharp | ||
[DependsOn( | ||
//...other dependencies | ||
typeof(AbpBackgroundWorkersHangfireModule) //Add the new module dependency | ||
)] | ||
public class YourModule : AbpModule | ||
{ | ||
} | ||
```` | ||
|
||
> Hangfire后台工作者集成提供了 `HangfirePeriodicBackgroundWorkerAdapter` 来适配 `PeriodicBackgroundWorkerBase` 和 `AsyncPeriodicBackgroundWorkerBase` 派生类. 所以你依然可以按照[后台工作者文档](Background-Workers.md)来定义后台作业. | ||
## 创建后台工作者 | ||
|
||
`HangfireBackgroundWorkerBase` 是创建一个后台工作者简单的方法. | ||
|
||
```` csharp | ||
public class MyLogWorker : HangfireBackgroundWorkerBase | ||
{ | ||
public MyLogWorker() | ||
{ | ||
RecurringJobId = nameof(MyLogWorker); | ||
CronExpression = Cron.Daily(); | ||
} | ||
|
||
public override Task DoWorkAsync() | ||
{ | ||
Logger.LogInformation("Executed MyLogWorker..!"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
```` | ||
|
||
* **RecurringJobId** 是一个可选参数, 参阅 [Hangfire文档](https://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html) | ||
* **CronExpression** 是CRON表达式, 参阅 [CRON 表达式](https://en.wikipedia.org/wiki/Cron#CRON_expression) | ||
|
||
> 你可以直接实现 `IHangfireBackgroundWorker`, 但是 `HangfireBackgroundWorkerBase` 提供了一些有用的属性,例如 `Logger`. | ||
## 注册到后台工作者管理器 | ||
|
||
创建一个后台工作者后, 你应该添加到 `IBackgroundWorkerManager`, 最常用的地方是在你模块类的 `OnApplicationInitialization` 方法中: | ||
|
||
```` csharp | ||
[DependsOn(typeof(AbpBackgroundWorkersModule))] | ||
public class MyModule : AbpModule | ||
{ | ||
public override void OnApplicationInitialization( | ||
ApplicationInitializationContext context) | ||
{ | ||
context.AddBackgroundWorker<MyLogWorker>(); | ||
} | ||
} | ||
```` | ||
|
||
`context.AddBackgroundWorker(...)` 是一个是以下代码快捷的扩展方法: | ||
|
||
```` csharp | ||
context.ServiceProvider | ||
.GetRequiredService<IBackgroundWorkerManager>() | ||
.Add( | ||
context | ||
.ServiceProvider | ||
.GetRequiredService<MyLogWorker>() | ||
); | ||
```` | ||
|
||
它解析给定的后台工作者并添加到 `IBackgroundWorkerManager`. | ||
|
||
虽然我们通常在 `OnApplicationInitialization` 中添加后台工作者, 但对此没有限制. 你可以在任何地方注入 `IBackgroundWorkerManager` 并在运行时添加后台工作者. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
framework/src/Volo.Abp.BackgroundWorkers.Hangfire/FodyWeavers.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> | ||
<ConfigureAwait ContinueOnCapturedContext="false" /> | ||
</Weavers> |
30 changes: 30 additions & 0 deletions
30
framework/src/Volo.Abp.BackgroundWorkers.Hangfire/FodyWeavers.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> | ||
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> | ||
<xs:element name="Weavers"> | ||
<xs:complexType> | ||
<xs:all> | ||
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> | ||
<xs:complexType> | ||
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:all> | ||
<xs:attribute name="VerifyAssembly" type="xs:boolean"> | ||
<xs:annotation> | ||
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> | ||
</xs:annotation> | ||
</xs:attribute> | ||
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> | ||
<xs:annotation> | ||
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> | ||
</xs:annotation> | ||
</xs:attribute> | ||
<xs:attribute name="GenerateXsd" type="xs:boolean"> | ||
<xs:annotation> | ||
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> | ||
</xs:annotation> | ||
</xs:attribute> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:schema> |
22 changes: 22 additions & 0 deletions
22
framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo.Abp.BackgroundWorkers.Hangfire.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\..\..\configureawait.props" /> | ||
<Import Project="..\..\..\common.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<AssemblyName>Volo.Abp.BackgroundWorkers.Hangfire</AssemblyName> | ||
<PackageId>Volo.Abp.BackgroundWorkers.Hangfire</PackageId> | ||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
<RootNamespace /> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Volo.Abp.BackgroundWorkers\Volo.Abp.BackgroundWorkers.csproj" /> | ||
<ProjectReference Include="..\Volo.Abp.Hangfire\Volo.Abp.Hangfire.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
36 changes: 36 additions & 0 deletions
36
...orkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/AbpBackgroundWorkersHangfireModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using Hangfire; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp.Hangfire; | ||
using Volo.Abp.Modularity; | ||
|
||
namespace Volo.Abp.BackgroundWorkers.Hangfire | ||
{ | ||
[DependsOn( | ||
typeof(AbpBackgroundWorkersModule), | ||
typeof(AbpHangfireModule))] | ||
public class AbpBackgroundWorkerHangfireModule : AbpModule | ||
{ | ||
public override void OnPreApplicationInitialization(ApplicationInitializationContext context) | ||
{ | ||
var options = context.ServiceProvider.GetRequiredService<IOptions<AbpBackgroundWorkerOptions>>().Value; | ||
if (!options.IsEnabled) | ||
{ | ||
var hangfireOptions = context.ServiceProvider.GetRequiredService<IOptions<AbpHangfireOptions>>().Value; | ||
hangfireOptions.BackgroundJobServerFactory = CreateOnlyEnqueueJobServer; | ||
} | ||
} | ||
|
||
public override void ConfigureServices(ServiceConfigurationContext context) | ||
{ | ||
context.Services.AddSingleton(typeof(HangfirePeriodicBackgroundWorkerAdapter<>)); | ||
} | ||
|
||
private BackgroundJobServer CreateOnlyEnqueueJobServer(IServiceProvider serviceProvider) | ||
{ | ||
serviceProvider.GetRequiredService<JobStorage>(); | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.