feat: 优化定时任务体验

This commit is contained in:
陈淳
2024-01-09 16:59:20 +08:00
parent 5357c813e4
commit 8c122b7e09
3 changed files with 30 additions and 7 deletions

View File

@@ -46,7 +46,10 @@ namespace Yi.Framework.SqlSugarCore.Uow
var unitOfWork = UnitOfWorkManager.Current; var unitOfWork = UnitOfWorkManager.Current;
if (unitOfWork == null) if (unitOfWork == null)
{ {
throw new AbpException("A DbContext can only be created inside a unit of work!"); UnitOfWorkManager.Begin(true);
unitOfWork=UnitOfWorkManager.Current;
//取消工作单元强制性
//throw new AbpException("A DbContext can only be created inside a unit of work!");
} }
//var sss= unitOfWork.ServiceProvider.GetRequiredService<TDbContext>(); //var sss= unitOfWork.ServiceProvider.GetRequiredService<TDbContext>();
//Console.WriteLine("反户的:"+sss.SqlSugarClient.ContextID); //Console.WriteLine("反户的:"+sss.SqlSugarClient.ContextID);

View File

@@ -1,20 +1,36 @@
using Quartz; using Quartz;
using SqlSugar;
using Volo.Abp.BackgroundWorkers.Quartz; using Volo.Abp.BackgroundWorkers.Quartz;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Jobs namespace Yi.Framework.Rbac.Application.Jobs
{ {
/// <summary>
/// 定时任务
/// </summary>
public class TestJob : QuartzBackgroundWorkerBase public class TestJob : QuartzBackgroundWorkerBase
{ {
public TestJob() private ISqlSugarRepository<UserEntity> _repository;
public TestJob(ISqlSugarRepository<UserEntity> repository)
{ {
_repository = repository;
JobDetail = JobBuilder.Create<TestJob>().WithIdentity(nameof(TestJob)).Build(); JobDetail = JobBuilder.Create<TestJob>().WithIdentity(nameof(TestJob)).Build();
Trigger = TriggerBuilder.Create().WithIdentity(nameof(TestJob)).WithCronSchedule("* * * * * ? *").Build(); Trigger = TriggerBuilder.Create().WithIdentity(nameof(TestJob)).StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(1000 * 60)
.RepeatForever())
.Build();
} }
public override Task Execute(IJobExecutionContext context) public override async Task Execute(IJobExecutionContext context)
{ {
//定时任务,非常简单 //定时任务,非常简单
//Console.WriteLine("你好,世界"); Console.WriteLine("你好,世界");
return Task.CompletedTask; // var eneities= await _repository.GetListAsync();
//var entities= await _sqlSugarClient.Queryable<UserEntity>().ToListAsync();
//await Console.Out.WriteLineAsync(entities.Count().ToString());
} }
} }
} }

View File

@@ -1,10 +1,12 @@
using Volo.Abp.Modularity; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Yi.Abp.Domain; using Yi.Abp.Domain;
using Yi.Abp.SqlSugarCore; using Yi.Abp.SqlSugarCore;
using Yi.Framework.Bbs.SqlSugarCore; using Yi.Framework.Bbs.SqlSugarCore;
using Yi.Framework.Mapster; using Yi.Framework.Mapster;
using Yi.Framework.Rbac.SqlSugarCore; using Yi.Framework.Rbac.SqlSugarCore;
using Yi.Framework.SqlSugarCore; using Yi.Framework.SqlSugarCore;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Abp.SqlsugarCore namespace Yi.Abp.SqlsugarCore
{ {
@@ -22,6 +24,8 @@ namespace Yi.Abp.SqlsugarCore
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
context.Services.AddYiDbContext<YiDbContext>(); context.Services.AddYiDbContext<YiDbContext>();
//默认不开放可根据项目需要是否Db直接对外开放
//context.Services.AddTransient(x => x.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient);
} }
} }
} }