Browse Source
✨ Hangfire添加自动删除过期任务 ✨ Hangfire修改自动重试次数和重试时间间隔 ✨ Hangfire添加重试最后一次处理逻辑main-auditlogging 5.1.1.5
10 changed files with 85 additions and 46 deletions
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using Hangfire.Common; |
|||
using Hangfire.States; |
|||
using Hangfire.Storage; |
|||
|
|||
namespace Lion.AbpPro.Extensions.Filters; |
|||
|
|||
public class AutoDeleteAfterSuccessAttributer : JobFilterAttribute, IApplyStateFilter |
|||
{ |
|||
private readonly TimeSpan _deleteAfter; |
|||
|
|||
public AutoDeleteAfterSuccessAttributer(TimeSpan timeSpan) |
|||
{ |
|||
_deleteAfter = timeSpan; |
|||
} |
|||
|
|||
public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction) |
|||
{ |
|||
context.JobExpirationTimeout = _deleteAfter; |
|||
} |
|||
|
|||
public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
using Hangfire.Common; |
|||
using Hangfire.States; |
|||
using Serilog; |
|||
|
|||
namespace Lion.AbpPro.Extensions.Hangfire; |
|||
|
|||
/// <summary>
|
|||
/// 重试最后一次
|
|||
/// </summary>
|
|||
public class JobRetryLastFilter : JobFilterAttribute, IElectStateFilter |
|||
{ |
|||
private int RetryCount { get; } |
|||
|
|||
public JobRetryLastFilter(int retryCount) |
|||
{ |
|||
RetryCount = retryCount; |
|||
} |
|||
|
|||
|
|||
public void OnStateElection(ElectStateContext context) |
|||
{ |
|||
int retryAttempt = context.GetJobParameter<int>("RetryCount"); |
|||
if (RetryCount == retryAttempt) |
|||
{ |
|||
Log.Error("最后一次重试"); |
|||
} |
|||
} |
|||
} |
|||
@ -1,16 +1,17 @@ |
|||
// using Hangfire;
|
|||
// using Microsoft.Extensions.DependencyInjection;
|
|||
// using System;
|
|||
// using LionAbpPro.Jobs;
|
|||
//
|
|||
// namespace LionAbpPro.Extensions
|
|||
// {
|
|||
// public static class RecurringJobsExtensions
|
|||
// {
|
|||
// public static void CreateRecurringJob(this IServiceProvider service)
|
|||
// {
|
|||
// var job = service.GetService<TestJob>();
|
|||
// RecurringJob.AddOrUpdate("测试Job", () => job.ExecuteAsync(), CronTypeHelper.Minute(1));
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
using Hangfire; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp; |
|||
|
|||
namespace Lion.AbpPro.Jobs |
|||
{ |
|||
public static class RecurringJobsExtensions |
|||
{ |
|||
public static void CreateRecurringJob(this ApplicationInitializationContext context) |
|||
{ |
|||
using var scope = context.ServiceProvider.CreateScope(); |
|||
var testJob = |
|||
scope.ServiceProvider.GetService<TestJob>(); |
|||
RecurringJob.AddOrUpdate("测试Job", () => testJob.ExecuteAsync(), CronType.Minute(1)); |
|||
} |
|||
} |
|||
} |
|||
@ -1,17 +0,0 @@ |
|||
using Hangfire; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp; |
|||
|
|||
namespace Lion.AbpPro.Jobs |
|||
{ |
|||
public static class RecurringJobsExtensions |
|||
{ |
|||
public static void CreateRecurringJob(this ApplicationInitializationContext context) |
|||
{ |
|||
using var scope = context.ServiceProvider.CreateScope(); |
|||
var testJob = |
|||
scope.ServiceProvider.GetService<TestJob>(); |
|||
RecurringJob.AddOrUpdate("测试Job", () => testJob.ExecuteAsync(), CronType.Minute(1)); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue