From 49a9eb54600aba0a1be2583e97431a87442c50e5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com>
Date: Sun, 27 Nov 2022 15:05:27 +0800
Subject: [PATCH] =?UTF-8?q?hangfire=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Config/SwaggerDoc.xml | 12 +++++++
.../Controllers/System/UserController.cs | 4 +--
.../Controllers/TestController.cs | 31 ++++++++++++++++++-
.../Yi.Framework.ApiMicroservice/Program.cs | 19 +++++++++++-
.../AbstractConsulDispatcher.cs | 0
.../AverageDispatcher.cs | 0
.../PollingDispatcher.cs | 0
.../WeightDispatcher.cs | 0
.../Yi.Framework.Core.csproj | 6 ++++
.../Yi.Framework.Task/HttpJob.cs | 24 ++++++++++++++
10 files changed, 92 insertions(+), 4 deletions(-)
rename Yi.Framework.Net6/Yi.Framework.Core/{ConsulExtend => Consul}/AbstractConsulDispatcher.cs (100%)
rename Yi.Framework.Net6/Yi.Framework.Core/{ConsulExtend => Consul}/AverageDispatcher.cs (100%)
rename Yi.Framework.Net6/Yi.Framework.Core/{ConsulExtend => Consul}/PollingDispatcher.cs (100%)
rename Yi.Framework.Net6/Yi.Framework.Core/{ConsulExtend => Consul}/WeightDispatcher.cs (100%)
diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml
index 726f6ee9..0431a866 100644
--- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml
+++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml
@@ -602,6 +602,18 @@
+
+
+ hangfireJob测试
+
+
+
+
+
+ hangfireJob测试
+
+
+
树形结构构建测试
diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/System/UserController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/System/UserController.cs
index 8801ce1e..a4753bf9 100644
--- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/System/UserController.cs
+++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/System/UserController.cs
@@ -101,7 +101,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
[Log("用户模块", OperEnum.Update)]
public async Task Update(UserInfoDto userDto)
{
- if (await _repository.IsAnyAsync(u => userDto.User.UserName.Equals(u.UserName) && !userDto.User.Id.Equals(u.Id)))
+ if (await _repository.IsAnyAsync(u => userDto.User.UserName!.Equals(u.UserName) && !userDto.User.Id.Equals(u.Id)))
{
return Result.Error("用户名已存在,修改失败!");
}
@@ -138,7 +138,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{
return Result.Error("密码为空,添加失败!");
}
- if (await _repository.IsAnyAsync(u => userDto.User.UserName.Equals(u.UserName)))
+ if (await _repository.IsAnyAsync(u => userDto.User.UserName!.Equals(u.UserName)))
{
return Result.Error("用户已经存在,添加失败!");
}
diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs
index 390ef8a8..5d53e956 100644
--- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs
+++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs
@@ -1,4 +1,5 @@
-using Microsoft.AspNetCore.Authorization;
+using Hangfire;
+using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Localization;
@@ -12,6 +13,7 @@ using Yi.Framework.Common.Const;
using Yi.Framework.Common.Models;
using Yi.Framework.Core;
using Yi.Framework.Interface;
+using Yi.Framework.Job;
using Yi.Framework.Language;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
@@ -216,9 +218,36 @@ namespace Yi.Framework.ApiMicroservice.Controllers
public async Task StopJob()
{
await _quartzInvoker.StopAsync(new Quartz.JobKey("test", "my"));
+ return Result.Success("http://localhost:19001/hangfire");
+ }
+
+ ///
+ /// hangfireJob测试
+ ///
+ ///
+ [HttpGet]
+ public Result HangfireStratJobTest()
+ {
+ Dictionary data = new Dictionary()
+ {
+ {JobConst.method,"get" },
+ {JobConst.url,"https://www.baidu.com" }
+ };
+ RecurringJob.AddOrUpdate(nameof(HttpJob),(Job)=>Job.Execute2(data), "*/5 * * * * ?");
+ return Result.Success("http://localhost:19001/hangfire");
+ }
+ ///
+ /// hangfireJob测试
+ ///
+ ///
+ [HttpGet]
+ public Result HangfireStopJobTest()
+ {
+ RecurringJob.RemoveIfExists(nameof(HttpJob));
return Result.Success();
}
+
///
/// 树形结构构建测试
///
diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs
index facff1e8..ce453b9e 100644
--- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs
+++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs
@@ -18,6 +18,8 @@ using Yi.Framework.WebCore.LogExtend;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AspNetCore.Mvc.Controllers;
using Yi.Framework.WebCore.AutoFacExtend;
+using Hangfire;
+using Hangfire.MemoryStorage;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddCommandLine(args);
@@ -78,6 +80,15 @@ builder.Services.AddSqlsugarServer(DbFiterExtend.Data);
//Quartz
#endregion
builder.Services.AddQuartzService();
+builder.Services.AddHangfire(option =>
+{
+ option.SetDataCompatibilityLevel(CompatibilityLevel.Version_170);//ȫüݰ汾¼
+ option.UseColouredConsoleLogProvider();//־
+ option.UseSimpleAssemblyNameTypeSerializer();//ʹüл
+ option.UseRecommendedSerializerSettings();//ʹƼл
+ option.UseMemoryStorage();
+});
+builder.Services.AddHangfireServer();
#region
//AutoMapperע
#endregion
@@ -85,7 +96,8 @@ builder.Services.AddAutoMapperService();
#region
//+
#endregion
-builder.Services.AddControllers(optios => {
+builder.Services.AddControllers(optios =>
+{
//עȫ
optios.Filters.Add();
}).AddJsonFileService();
@@ -228,6 +240,10 @@ app.UseDbSeedInitService();
#endregion
app.UseRedisSeedInitService();
+app.UseHangfireDashboard();
+
+
+
app.UseEndpoints(endpoints =>
{
#region
@@ -235,6 +251,7 @@ app.UseEndpoints(endpoints =>
#endregion
endpoints.MapHub("/api/hub/main");
endpoints.MapControllers();
+ endpoints.MapHangfireDashboard();
});
//Ӷ⻧
diff --git a/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/AbstractConsulDispatcher.cs b/Yi.Framework.Net6/Yi.Framework.Core/Consul/AbstractConsulDispatcher.cs
similarity index 100%
rename from Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/AbstractConsulDispatcher.cs
rename to Yi.Framework.Net6/Yi.Framework.Core/Consul/AbstractConsulDispatcher.cs
diff --git a/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/AverageDispatcher.cs b/Yi.Framework.Net6/Yi.Framework.Core/Consul/AverageDispatcher.cs
similarity index 100%
rename from Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/AverageDispatcher.cs
rename to Yi.Framework.Net6/Yi.Framework.Core/Consul/AverageDispatcher.cs
diff --git a/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/PollingDispatcher.cs b/Yi.Framework.Net6/Yi.Framework.Core/Consul/PollingDispatcher.cs
similarity index 100%
rename from Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/PollingDispatcher.cs
rename to Yi.Framework.Net6/Yi.Framework.Core/Consul/PollingDispatcher.cs
diff --git a/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/WeightDispatcher.cs b/Yi.Framework.Net6/Yi.Framework.Core/Consul/WeightDispatcher.cs
similarity index 100%
rename from Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/WeightDispatcher.cs
rename to Yi.Framework.Net6/Yi.Framework.Core/Consul/WeightDispatcher.cs
diff --git a/Yi.Framework.Net6/Yi.Framework.Core/Yi.Framework.Core.csproj b/Yi.Framework.Net6/Yi.Framework.Core/Yi.Framework.Core.csproj
index 7eae9182..ecc2430b 100644
--- a/Yi.Framework.Net6/Yi.Framework.Core/Yi.Framework.Core.csproj
+++ b/Yi.Framework.Net6/Yi.Framework.Core/Yi.Framework.Core.csproj
@@ -8,6 +8,8 @@
+
+
@@ -22,4 +24,8 @@
+
+
+
+
diff --git a/Yi.Framework.Net6/Yi.Framework.Task/HttpJob.cs b/Yi.Framework.Net6/Yi.Framework.Task/HttpJob.cs
index 9e7c102c..1af9aa0f 100644
--- a/Yi.Framework.Net6/Yi.Framework.Task/HttpJob.cs
+++ b/Yi.Framework.Net6/Yi.Framework.Task/HttpJob.cs
@@ -36,6 +36,30 @@ namespace Yi.Framework.Job
}
+ _logger.LogWarning("定时任务开始调度:" + nameof(HttpJob) + ":" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + $":访问地址为:{url},结果为:{data}");
+ Console.WriteLine($"结果:{data}");
+ });
+ }
+
+ public Task Execute2(Dictionary dic)
+ {
+ return Task.Run(async () =>
+ {
+ //var jobData = context.JobDetail.JobDataMap;
+ string method = dic[Common.Const.JobConst.method].ToString();
+ string url = dic[Common.Const.JobConst.url].ToString();
+ string data = "异常!";
+ switch (method)
+ {
+ case "post":
+ data = await Common.Helper.HttpHelper.Post(url);
+ break;
+ case "get":
+ data = await Common.Helper.HttpHelper.Get(url);
+ break;
+ }
+
+
_logger.LogWarning("定时任务开始调度:" + nameof(HttpJob) + ":" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + $":访问地址为:{url},结果为:{data}");
Console.WriteLine($"结果:{data}");
});