更新Hangfire提取到 框架
This commit is contained in:
1 parent
33ef90a5cc
commit
7c29e57623
11 files changed
+151
-48
No files matched your search
@@ -1,7 +1,5 @@
|
||||
using MAX.Plugins;
|
||||
using Hangfire;
|
||||
using Hangfire.MySql;
|
||||
using HangfireBasicAuthenticationFilter;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -16,28 +14,17 @@ namespace Store.Modules.CloudProduct
|
||||
public override string Id => "CloudProduct";
|
||||
protected override void RegisterServices(IServiceCollection services)
|
||||
{
|
||||
var configuration = Context.Configuration;
|
||||
RequireConfiguration("Search:Url");
|
||||
RequireConfiguration("Search:IndexName");
|
||||
RequireConfiguration("Search:FavoritesIndexName");
|
||||
RequireConfiguration("RawData:Path");
|
||||
|
||||
// ===== Hangfire 后台定时任务(商品分词同步) =====
|
||||
var hangfireConnection = configuration.GetValue<string>("CloudProductConnection")
|
||||
?? throw new InvalidOperationException("缺少配置 CloudProductConnection,CloudProduct 模块的 Hangfire 无法启动。");
|
||||
|
||||
// 存储中的建表/查询语句依赖用户变量;与业务仓储共用 FreeSql。
|
||||
var connectionStringBuilder = new MySqlConnectionStringBuilder(hangfireConnection)
|
||||
{
|
||||
AllowUserVariables = true
|
||||
};
|
||||
|
||||
services.AddHangfire((provider, config) => config
|
||||
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
|
||||
.UseSimpleAssemblyNameTypeSerializer()
|
||||
.UseRecommendedSerializerSettings()
|
||||
.UseStorage(new MySqlStorage(provider.GetRequiredKeyedService<IFreeSql>("CloudProductConnection"), new MySqlStorageOptions() { TablesPrefix = "Hangfire_" })));
|
||||
services.AddHangfireServer();
|
||||
// 业务连接独立于宿主的 Hangfire 存储。
|
||||
var connection = RequireConfiguration("CloudProductConnection");
|
||||
var connectionStringBuilder = new MySqlConnectionStringBuilder(connection)
|
||||
{
|
||||
AllowUserVariables = true
|
||||
};
|
||||
services.AddTransient<GoodsIndexJob>();
|
||||
services.AddTransient<GoodsViolationIndexJob>();
|
||||
services.AddAuthorization(options => options.AddPolicy("CloudGoodsViolationWrite",
|
||||
@@ -57,18 +44,6 @@ namespace Store.Modules.CloudProduct
|
||||
try
|
||||
{
|
||||
var Configuration = Context.Configuration;
|
||||
// Hangfire 后台任务看板(默认地址 /hangfire)
|
||||
app.UseHangfireDashboard("/hangfire", new DashboardOptions
|
||||
{
|
||||
Authorization = new[]
|
||||
{
|
||||
new HangfireCustomBasicAuthenticationFilter(){
|
||||
User = Configuration.GetValue<string>("Hangfire:Dashboard:User") ?? "admin",
|
||||
Pass = Configuration.GetValue<string>("Hangfire:Dashboard:Pass") ?? "1234560"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 注册定时任务:把未同步(IndexStatus=0)的商品写入 OpenSearch 分词索引
|
||||
var cron = Configuration.GetValue<string>("Indexing:Cron") ?? "*/5 * * * *";
|
||||
|
||||
@@ -83,11 +58,11 @@ namespace Store.Modules.CloudProduct
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 数据库不可用/表未初始化时避免阻断应用启动(看板与定时任务都会连库),恢复后重启即可重新装配
|
||||
Console.WriteLine("[Hangfire] 看板/定时任务装配失败: " + ex.Message);
|
||||
// 定时任务注册失败时记录错误,数据库恢复后重启以重新注册。
|
||||
Console.WriteLine("[Hangfire] CloudProduct 定时任务注册失败: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user