mirror of https://github.com/abpframework/abp.git
10 changed files with 244 additions and 0 deletions
@ -0,0 +1,3 @@ |
|||||
|
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
||||
|
<ConfigureAwait ContinueOnCapturedContext="false" /> |
||||
|
</Weavers> |
||||
@ -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> |
||||
@ -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> |
||||
@ -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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundWorkers.Hangfire |
||||
|
{ |
||||
|
public abstract class HangfireBackgroundWorkerBase : BackgroundWorkerBase, IHangfireBackgroundWorker |
||||
|
{ |
||||
|
public string CronExpression { get; set; } |
||||
|
|
||||
|
public abstract Task ExecuteAsync(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,83 @@ |
|||||
|
using System; |
||||
|
using System.Reflection; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using Hangfire; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Threading; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundWorkers.Hangfire |
||||
|
{ |
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class HangfireBackgroundWorkerManager : IBackgroundWorkerManager, ISingletonDependency |
||||
|
{ |
||||
|
public Task StartAsync(CancellationToken cancellationToken = new CancellationToken()) |
||||
|
{ |
||||
|
return Task.CompletedTask; |
||||
|
} |
||||
|
|
||||
|
public Task StopAsync(CancellationToken cancellationToken = new CancellationToken()) |
||||
|
{ |
||||
|
return Task.CompletedTask; |
||||
|
} |
||||
|
|
||||
|
public void Add(IBackgroundWorker worker) |
||||
|
{ |
||||
|
if (worker is IHangfireBackgroundWorker hangfireBackgroundWorker) |
||||
|
{ |
||||
|
RecurringJob.AddOrUpdate(() => hangfireBackgroundWorker.ExecuteAsync(), |
||||
|
hangfireBackgroundWorker.CronExpression); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
int? period; |
||||
|
|
||||
|
if (worker is AsyncPeriodicBackgroundWorkerBase or PeriodicBackgroundWorkerBase) |
||||
|
{ |
||||
|
var timer = (AbpTimer) worker.GetType() |
||||
|
.GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker); |
||||
|
period = timer?.Period; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (period == null) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
var adapterType = typeof(HangfirePeriodicBackgroundWorkerAdapter<>).MakeGenericType(worker.GetType()); |
||||
|
var workerAdapter = Activator.CreateInstance(adapterType) as IHangfireBackgroundWorker; |
||||
|
|
||||
|
RecurringJob.AddOrUpdate(() => workerAdapter.ExecuteAsync(), GetCron(period.Value)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected virtual string GetCron(int period) |
||||
|
{ |
||||
|
var time = TimeSpan.FromMilliseconds(period); |
||||
|
string cron; |
||||
|
|
||||
|
if (time.TotalSeconds <= 59) |
||||
|
{ |
||||
|
cron = $"*/{time.TotalSeconds} * * * * *"; |
||||
|
} |
||||
|
else if (time.TotalMinutes <= 59) |
||||
|
{ |
||||
|
cron = $"*/{time.TotalMinutes} * * * *"; |
||||
|
} |
||||
|
else if (time.TotalHours <= 23) |
||||
|
{ |
||||
|
cron = $"0 */{time.TotalHours} * * *"; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
cron = $"0 0 */{time.TotalDays} * *"; |
||||
|
} |
||||
|
|
||||
|
return cron; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
using System.Reflection; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundWorkers.Hangfire |
||||
|
{ |
||||
|
public class HangfirePeriodicBackgroundWorkerAdapter<TWorker> : HangfireBackgroundWorkerBase |
||||
|
where TWorker : IBackgroundWorker |
||||
|
{ |
||||
|
private readonly MethodInfo _doWorkAsyncMethod; |
||||
|
private readonly MethodInfo _doWorkMethod; |
||||
|
|
||||
|
public HangfirePeriodicBackgroundWorkerAdapter() |
||||
|
{ |
||||
|
_doWorkAsyncMethod = |
||||
|
typeof(TWorker).GetMethod("DoWorkAsync", BindingFlags.Instance | BindingFlags.NonPublic); |
||||
|
_doWorkMethod = typeof(TWorker).GetMethod("DoWork", BindingFlags.Instance | BindingFlags.NonPublic); |
||||
|
} |
||||
|
|
||||
|
public override async Task ExecuteAsync() |
||||
|
{ |
||||
|
var workerContext = new PeriodicBackgroundWorkerContext(ServiceProvider); |
||||
|
var worker = ServiceProvider.GetRequiredService<TWorker>(); |
||||
|
|
||||
|
switch (worker) |
||||
|
{ |
||||
|
case AsyncPeriodicBackgroundWorkerBase asyncPeriodicBackgroundWorker: |
||||
|
await (Task) _doWorkAsyncMethod.Invoke(asyncPeriodicBackgroundWorker, new object[] {workerContext}); |
||||
|
break; |
||||
|
case PeriodicBackgroundWorkerBase periodicBackgroundWorker: |
||||
|
_doWorkMethod.Invoke(periodicBackgroundWorker, new object[] {workerContext}); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.BackgroundWorkers.Hangfire |
||||
|
{ |
||||
|
public interface IHangfireBackgroundWorker : IBackgroundWorker |
||||
|
{ |
||||
|
string CronExpression { get; set; } |
||||
|
|
||||
|
Task ExecuteAsync(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
Loading…
Reference in new issue