feat: 完成job模块优化

This commit is contained in:
橙子
2025-02-23 01:31:30 +08:00
parent f8a4c737ee
commit f6b19ec2a5
20 changed files with 40 additions and 66 deletions

View File

@@ -3,7 +3,6 @@
<ItemGroup>
<ProjectReference Include="..\..\framework\Yi.Framework.BackgroundWorkers.Hangfire\Yi.Framework.BackgroundWorkers.Hangfire.csproj" />
<ProjectReference Include="..\..\framework\Yi.Framework.Ddd.Application\Yi.Framework.Ddd.Application.csproj" />
<ProjectReference Include="..\..\module\bbs\Yi.Framework.Bbs.Application\Yi.Framework.Bbs.Application.csproj" />
<ProjectReference Include="..\..\module\chat-hub\Yi.Framework.ChatHub.Application\Yi.Framework.ChatHub.Application.csproj" />

View File

@@ -1,7 +1,6 @@
using Volo.Abp.SettingManagement;
using Yi.Abp.Application.Contracts;
using Yi.Abp.Domain;
using Yi.Framework.BackgroundWorkers.Hangfire;
using Yi.Framework.Bbs.Application;
using Yi.Framework.ChatHub.Application;
using Yi.Framework.CodeGen.Application;
@@ -26,8 +25,7 @@ namespace Yi.Abp.Application
typeof(YiFrameworkCodeGenApplicationModule),
typeof (YiFrameworkSettingManagementApplicationModule),
typeof(YiFrameworkDddApplicationModule),
typeof(YiFrameworkBackgroundWorkersHangfireModule)
typeof(YiFrameworkDddApplicationModule)
)]
public class YiAbpApplicationModule : AbpModule
{

View File

@@ -1,11 +1,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Volo.Abp.BackgroundWorkers.Hangfire;
using Volo.Abp.BackgroundWorkers.Hangfire;
using Volo.Abp.Data;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Abp.Application.Jobs
namespace Yi.Abp.Web.Jobs
{
public class DemoResetJob : HangfireBackgroundWorkerBase
{

View File

@@ -1,15 +1,12 @@
using Hangfire;
using SqlSugar;
using Volo.Abp.BackgroundWorkers.Hangfire;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Abp.Application.Jobs
namespace Yi.Abp.Web.Jobs
{
/// <summary>
/// 定时任务
/// 定时任务Test,Job可单独拆成一个Host单独允许
/// </summary>
public class TestJob : HangfireBackgroundWorkerBase
{

View File

@@ -0,0 +1,31 @@
using Volo.Abp.BackgroundWorkers.Hangfire;
using Volo.Abp.EventBus.Local;
using Yi.Framework.Bbs.Domain.Shared.Etos;
namespace Yi.Abp.Web.Jobs.bbs;
public class AccessLogCacheJob : HangfireBackgroundWorkerBase
{
private readonly ILocalEventBus _localEventBus;
public AccessLogCacheJob(ILocalEventBus localEventBus)
{
_localEventBus = localEventBus;
RecurringJobId = "访问日志写入缓存";
//每分钟执行一次将本地缓存转入redis防止丢数据
CronExpression = "0 * * * * ?";
//
// JobDetail = JobBuilder.Create<AccessLogCacheJob>().WithIdentity(nameof(AccessLogCacheJob))
// .Build();
//每10秒执行一次将本地缓存转入redis防止丢数据
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(AccessLogCacheJob))
// .WithSimpleSchedule((schedule) => { schedule.WithInterval(TimeSpan.FromSeconds(10)).RepeatForever();; })
// .Build();
}
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
{
await _localEventBus.PublishAsync(new AccessLogResetArgs());
}
}

View File

@@ -0,0 +1,89 @@
using FreeRedis;
using Microsoft.Extensions.Options;
using Volo.Abp.BackgroundWorkers.Hangfire;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Yi.Framework.Bbs.Domain.Entities;
using Yi.Framework.Bbs.Domain.Shared.Caches;
using Yi.Framework.Bbs.Domain.Shared.Enums;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Abp.Web.Jobs.bbs;
public class AccessLogStoreJob : HangfireBackgroundWorkerBase
{
private readonly ISqlSugarRepository<AccessLogAggregateRoot> _repository;
/// <summary>
/// 缓存前缀
/// </summary>
private string CacheKeyPrefix => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDistributedCacheOptions>>()
.Value.KeyPrefix;
/// <summary>
/// 使用懒加载防止报错
/// </summary>
private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>();
/// <summary>
/// 属性注入
/// </summary>
public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
private bool EnableRedisCache
{
get
{
var redisEnabled = LazyServiceProvider.LazyGetRequiredService<IConfiguration>()["Redis:IsEnabled"];
return redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled);
}
}
public AccessLogStoreJob(ISqlSugarRepository<AccessLogAggregateRoot> repository)
{
_repository = repository;
RecurringJobId = "访问日志写入数据库";
//每小时执行一次
CronExpression = "0 0 * * * ?";
// JobDetail = JobBuilder.Create<AccessLogStoreJob>().WithIdentity(nameof(AccessLogStoreJob))
// .Build();
// //每分钟执行一次
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(AccessLogStoreJob))
// .WithCronSchedule("0 * * * * ?")
// .Build();
}
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
{
if (EnableRedisCache)
{
//当天的访问量
var number =
await RedisClient.GetAsync<long>($"{CacheKeyPrefix}{AccessLogCacheConst.Key}:{DateTime.Now.Date:yyyyMMdd}");
var entity = await _repository._DbQueryable.Where(x => x.AccessLogType == AccessLogTypeEnum.Request)
.Where(x => x.CreationTime.Date == DateTime.Today)
.FirstAsync();
if (entity is not null)
{
entity.Number = number+1;
await _repository.UpdateAsync(entity);
}
else
{
await _repository.InsertAsync((new AccessLogAggregateRoot() { Number = number,AccessLogType = AccessLogTypeEnum.Request}));
}
//删除前一天的缓存
await RedisClient.DelAsync($"{CacheKeyPrefix}{AccessLogCacheConst.Key}:{DateTime.Now.Date.AddDays(-1):yyyyMMdd}");
}
}
}

View File

@@ -0,0 +1,31 @@
using Volo.Abp.BackgroundWorkers.Hangfire;
using Yi.Framework.Bbs.Domain.Managers;
namespace Yi.Abp.Web.Jobs.bbs;
/// <summary>
/// 每日任务job
/// </summary>
public class AssignmentExpireTimeOutJob : HangfireBackgroundWorkerBase
{
private readonly AssignmentManager _assignmentManager;
public AssignmentExpireTimeOutJob(AssignmentManager assignmentManager)
{
_assignmentManager = assignmentManager;
RecurringJobId = "每日任务系统超时检测";
//每分钟执行一次
CronExpression = "0 * * * * ?";
//
// JobDetail = JobBuilder.Create<AssignmentExpireTimeOutJob>().WithIdentity(nameof(AssignmentExpireTimeOutJob)).Build();
// //每个小时整点执行一次
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(AssignmentExpireTimeOutJob)).WithCronSchedule("0 0 * * * ?")
// .Build();
}
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
{
await _assignmentManager.ExpireTimeoutAsync();
}
}

View File

@@ -0,0 +1,37 @@
using Volo.Abp.BackgroundWorkers.Hangfire;
using Yi.Framework.Bbs.Domain.Managers;
namespace Yi.Abp.Web.Jobs.bbs
{
public class InterestRecordsJob : HangfireBackgroundWorkerBase
{
private BankManager _bankManager;
public InterestRecordsJob(BankManager bankManager)
{
_bankManager = bankManager;
RecurringJobId = "银行利息积分刷新";
//每个小时整点执行一次
CronExpression = "0 0 * * * ?";
// JobDetail = JobBuilder.Create<InterestRecordsJob>().WithIdentity(nameof(InterestRecordsJob)).Build();
//
// //每个小时整点执行一次
//
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(InterestRecordsJob)).WithCronSchedule("0 0 * * * ?").Build();
//测试
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(InterestRecordsJob))
//.WithSimpleSchedule(x => x
// .WithIntervalInSeconds(10)
// .RepeatForever())
//.Build();
}
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
{
//创建一个记录,莫得了
await _bankManager.GetCurrentInterestRate();
}
}
}

View File

@@ -0,0 +1,37 @@
using Volo.Abp.BackgroundWorkers.Hangfire;
using Yi.Framework.DigitalCollectibles.Domain.Managers;
namespace Yi.Abp.Web.Jobs.digital_collectibles;
/// <summary>
/// 自动下架商品
/// </summary>
public class AutoPassInGoodsJob: HangfireBackgroundWorkerBase
{
private readonly MarketManager _marketManager;
private readonly ILogger<AutoPassInGoodsJob> _logger;
public AutoPassInGoodsJob(MarketManager marketManager, ILogger<AutoPassInGoodsJob> logger)
{
_marketManager = marketManager;
_logger = logger;
RecurringJobId = "交易市场自动流拍";
//每小时,第10分钟执行一次
CronExpression = "0 10 * * * ?";
//
// JobDetail = JobBuilder.Create<AutoPassInGoodsJob>().WithIdentity(nameof(AutoPassInGoodsJob))
// .Build();
//
// //每小时,第10分钟执行一次
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(AutoPassInGoodsJob))
// // .WithSimpleSchedule((builer) =>
// // {
// // builer.WithIntervalInHours(10);
// // })
// // .StartNow()
// .WithCronSchedule("0 10 * * * ?")
// .Build();
}
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
{
await _marketManager.AutoPassInGoodsAsync();
}
}

View File

@@ -0,0 +1,46 @@
using Volo.Abp.BackgroundWorkers.Hangfire;
using Yi.Framework.DigitalCollectibles.Domain.Managers;
namespace Yi.Abp.Web.Jobs.digital_collectibles;
/// <summary>
/// 自动刷新填满矿池
/// </summary>
public class AutoRefreshMiningPoolJob : HangfireBackgroundWorkerBase
{
private readonly MiningPoolManager _miningPoolManager;
public AutoRefreshMiningPoolJob(MiningPoolManager miningPoolManager)
{
_miningPoolManager = miningPoolManager;
RecurringJobId = "刷新矿池和用户限制";
//每天早上10点执行一次
CronExpression = "0 0 10 * * ?";
//
// JobDetail = JobBuilder.Create<AutoRefreshMiningPoolJob>().WithIdentity(nameof(AutoRefreshMiningPoolJob))
// .Build();
//
// //每天早上10点执行一次
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(AutoRefreshMiningPoolJob))
// .WithCronSchedule("0 0 10 * * ?")
// .Build();
//
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(AutoRefreshMiningPoolJob))
// .WithSimpleSchedule((schedule) =>
// {
// schedule.WithInterval(TimeSpan.FromHours(1));
// })
// .StartNow()
// .Build();
}
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
{
//刷新矿池
await _miningPoolManager.RefreshMiningPoolAsync();
//刷新用户限制
await _miningPoolManager.RefreshMiningUserLimitAsync();
}
}

View File

@@ -0,0 +1,25 @@
using Volo.Abp.BackgroundWorkers.Hangfire;
using Yi.Framework.DigitalCollectibles.Domain.Managers;
namespace Yi.Abp.Web.Jobs.digital_collectibles;
/// <summary>
/// 自动更新藏品价值
/// </summary>
public class AutoUpdateCollectiblesValueJob : HangfireBackgroundWorkerBase
{
private readonly CollectiblesManager _collectiblesManager;
public AutoUpdateCollectiblesValueJob(CollectiblesManager collectiblesManager)
{
_collectiblesManager = collectiblesManager;
RecurringJobId = "更新藏品价值";
//每天早上9点执行一次
CronExpression = "0 0 9 * * ?";
}
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
{
await _collectiblesManager.UpdateAllValueAsync();
}
}

View File

@@ -0,0 +1,36 @@
using Volo.Abp.BackgroundWorkers.Hangfire;
using Yi.Framework.DigitalCollectibles.Domain.Managers;
namespace Yi.Abp.Web.Jobs.digital_collectibles;
/// <summary>
/// 处理挂机挖矿定时任务
/// </summary>
public class OnHookAutoMiningJob : HangfireBackgroundWorkerBase
{
private readonly MiningPoolManager _miningPoolManager;
private readonly ILogger<OnHookAutoMiningJob> _logger;
public OnHookAutoMiningJob(MiningPoolManager miningPoolManager, ILogger<OnHookAutoMiningJob> logger)
{
_miningPoolManager = miningPoolManager;
_logger = logger;
RecurringJobId = "自动挂机挖矿";
//每小时执行一次
CronExpression = "0 0 * * * ?";
//
// JobDetail = JobBuilder.Create<OnHookAutoMiningJob>().WithIdentity(nameof(OnHookAutoMiningJob))
// .Build();
//
// //每小时执行一次
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(OnHookAutoMiningJob))
// // .WithCronSchedule("10 * * * * ?")
// .WithCronSchedule("0 0 * * * ?")
// .Build();
}
public override async Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
{
await _miningPoolManager.OnHookMiningAsync();
}
}

View File

@@ -0,0 +1,34 @@
using Microsoft.Extensions.Options;
using Volo.Abp.BackgroundWorkers.Hangfire;
using Yi.Framework.Rbac.Domain.Shared.Options;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Abp.Web.Jobs.rbac
{
public class BackupDataBaseJob: HangfireBackgroundWorkerBase
{
private ISqlSugarDbContext _dbContext;
private IOptions<RbacOptions> _options;
public BackupDataBaseJob(ISqlSugarDbContext dbContext, IOptions<RbacOptions> options)
{
_options = options;
_dbContext = dbContext;
RecurringJobId = "数据库备份";
//每天00点与24点进行备份
CronExpression = "0 0 0,12 * * ? ";
}
public override Task DoWorkAsync(CancellationToken cancellationToken = new CancellationToken())
{
if (_options.Value.EnableDataBaseBackup)
{
var logger = LoggerFactory.CreateLogger<BackupDataBaseJob>();
logger.LogWarning("正在进行数据库备份");
_dbContext.BackupDataBase();
logger.LogWarning("数据库备份已完成");
}
return Task.CompletedTask;
}
}
}

View File

@@ -29,6 +29,7 @@
<ItemGroup>
<ProjectReference Include="..\..\framework\Yi.Framework.AspNetCore.Authentication.OAuth\Yi.Framework.AspNetCore.Authentication.OAuth.csproj" />
<ProjectReference Include="..\..\framework\Yi.Framework.AspNetCore\Yi.Framework.AspNetCore.csproj" />
<ProjectReference Include="..\..\framework\Yi.Framework.BackgroundWorkers.Hangfire\Yi.Framework.BackgroundWorkers.Hangfire.csproj" />
<ProjectReference Include="..\Yi.Abp.Application\Yi.Abp.Application.csproj" />
<ProjectReference Include="..\Yi.Abp.SqlSugarCore\Yi.Abp.SqlSugarCore.csproj" />
</ItemGroup>

View File

@@ -58,15 +58,16 @@ namespace Yi.Abp.Web
typeof(YiAbpApplicationModule),
typeof(AbpAspNetCoreMultiTenancyModule),
typeof(AbpAspNetCoreMvcModule),
typeof(AbpAutofacModule),
typeof(AbpSwashbuckleModule),
typeof(AbpAspNetCoreSerilogModule),
typeof(AbpAuditingModule),
typeof(YiFrameworkBackgroundWorkersHangfireModule),
typeof(AbpBackgroundJobsHangfireModule),
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
typeof(YiFrameworkAspNetCoreModule),
typeof(YiFrameworkAspNetCoreAuthenticationOAuthModule)
typeof(YiFrameworkAspNetCoreAuthenticationOAuthModule),
typeof(YiFrameworkBackgroundWorkersHangfireModule),
typeof(AbpAutofacModule)
)]
public class YiAbpWebModule : AbpModule
{