From cd43abc7ebd3b9f6914b7229c5b4fa3d9f7b0038 Mon Sep 17 00:00:00 2001 From: Seany <17074267@qq.com> Date: Fri, 12 Dec 2025 12:56:22 +0800 Subject: [PATCH] chore --- Atomx.Admin/Atomx.Admin/Program.cs | 35 ++++++++++++++- Atomx.Admin/Atomx.Admin/appsettings.json | 46 ++++++++++++-------- Atomx.Core/Atomx.Core.csproj | 1 - Atomx.Core/Class1.cs | 7 --- Atomx.Core/Jos/LocalizationJob.cs | 26 +++++++++++ Atomx.Core/Jos/LocalizationJobs.cs | 13 ------ Atomx.Core/Services/BackgroundJobsService.cs | 14 ++++++ 7 files changed, 100 insertions(+), 42 deletions(-) delete mode 100644 Atomx.Core/Class1.cs create mode 100644 Atomx.Core/Jos/LocalizationJob.cs delete mode 100644 Atomx.Core/Jos/LocalizationJobs.cs create mode 100644 Atomx.Core/Services/BackgroundJobsService.cs diff --git a/Atomx.Admin/Atomx.Admin/Program.cs b/Atomx.Admin/Atomx.Admin/Program.cs index d021fee..c3df378 100644 --- a/Atomx.Admin/Atomx.Admin/Program.cs +++ b/Atomx.Admin/Atomx.Admin/Program.cs @@ -12,6 +12,9 @@ using Atomx.Data.Services; using Atomx.Utils.Json.Converts; using Blazored.LocalStorage; using FluentValidation; +using Hangfire; +using Hangfire.PostgreSql; +using Hangfire.PostgreSql.Factories; using Mapster; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components.Authorization; @@ -19,7 +22,9 @@ using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Localization; using Microsoft.AspNetCore.ResponseCompression; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Localization; +using Npgsql; using Scalar.AspNetCore; using Serilog; using System.Globalization; @@ -62,7 +67,6 @@ builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpClientApiService(builder.Configuration["WebApi:ServerUrl"] ?? "http://localhost"); // 注入 LocalizationProvider 供 Server 模式下使用 -// Use Scoped to avoid consuming scoped services from singleton builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddTransient(typeof(IStringLocalizer<>), typeof(JsonStringLocalizer<>)); @@ -97,13 +101,36 @@ var connection = builder.Configuration.GetConnectionString("DefaultConnection") builder.Services.AddDbContext(options => options.UseNpgsql(connection, p => p.MigrationsHistoryTable("__DbMigrationsHistory"))); // Redis 缓存 -var redisConnection = builder.Configuration.GetConnectionString("cache"); +var redisConnection = builder.Configuration.GetConnectionString("RedisConnection"); builder.Services.AddStackExchangeRedisCache(options => { options.Configuration = redisConnection; options.InstanceName = builder.Configuration["RedisCache:InstanceName"]; }); +// Hangfire 配置 +var hangfireConnection = builder.Configuration.GetConnectionString("HangfireConnection") + ?? throw new InvalidOperationException("Connection string 'HangfireConnection' not found."); + +// 添加hangfire服务并配置PostgreSQL存储 +builder.Services.AddHangfire(config => +{ + config.UsePostgreSqlStorage( + options => + { + options.UseNpgsqlConnection(hangfireConnection); + }, + new PostgreSqlStorageOptions + { + // Additional PostgreSQL storage options + PrepareSchemaIfNecessary = true, + QueuePollInterval = TimeSpan.FromSeconds(10) // 设置队列轮询间隔 + }).UseFilter(new AutomaticRetryAttribute { Attempts = 3 }); +}); + +// 启用 Hangfire 仪表盘和服务器 +builder.Services.AddHangfireServer(); + // 响应压缩(保留) builder.Services.AddResponseCompression(options => { @@ -222,6 +249,10 @@ app.UseRequestLocalization(new RequestLocalizationOptions // app.MapHub("/hubs/chat"); // app.MapHub("/hubs/notification"); +app.UseHangfireDashboard("/hangfire", new DashboardOptions +{ +}); + app.MapControllers(); // Blazor 配置(Server + WASM render modes) diff --git a/Atomx.Admin/Atomx.Admin/appsettings.json b/Atomx.Admin/Atomx.Admin/appsettings.json index 96bb03d..eb440ee 100644 --- a/Atomx.Admin/Atomx.Admin/appsettings.json +++ b/Atomx.Admin/Atomx.Admin/appsettings.json @@ -1,25 +1,37 @@ { "ConnectionStrings": { "DefaultConnection": "Host=8.129.21.55;Port=6543;Username=postgres;Password=docker-7788@postgres.com;Database=dev_atomx;", - "cache": "8.129.21.55:6999,password=docker-7788@redis.com" + "HangfireConnection": "Host=8.129.21.55;Port=6543;Username=postgres;Password=docker-7788@postgres.com;Database=dev_hangfire;", + "RedisConnection": "8.129.21.55:6999,password=docker-7788@redis.com" }, "RedisCache": { - "IPAddress": "8.129.21.55", - "Port": "6999", - "Password": "docker-7788@redis.com", "InstanceName": "Atomx.Redis." }, + "Orleans": { + + }, + + "Hangfire": { + "WorkerCount": 2, //骞跺彂宸ヤ綔绾跨▼鏁 + "Queues": [ "default" ], //闃熷垪鍚嶇О + "RetryAttempts": 3 //澶辫触閲嶈瘯娆℃暟 + }, + + "WebApi": { + "ServerUrl": "http://localhost:5259" + }, + "Authentication": { "JwtBearer": { - "Issuer": "http://api.sampleapi.com", - "Audience": "SampleApi", - "SecurityKey": "SecurityKey23456SecurityKey23456", - "ClockSkew": "10", - "AccessTokenExpirationMinutes": "60", - "RefreshTokenExpirationMinutes": "60", - "MaxRefreshTokensPerUser": "3" + "Issuer": "http://jwt.atomlust.com", //璋侀鍙戠殑token + "Audience": "atomx", //token鐨勫彈浼 + "SecurityKey": "SecurityKey23456SecurityKey23456", //瀵嗛挜 + "ClockSkew": "10", //鍏佽鐨勬湇鍔″櫒鏃堕棿鍋忓樊锛屽崟浣嶄负鍒嗛挓 + "AccessTokenExpirationMinutes": "60", //璁块棶浠ょ墝杩囨湡鏃堕棿锛屽崟浣嶄负鍒嗛挓 + "RefreshTokenExpirationMinutes": "60", //鍒锋柊浠ょ墝杩囨湡鏃堕棿锛屽崟浣嶄负鍒嗛挓 + "MaxRefreshTokensPerUser": "3" //姣忎釜鐢ㄦ埛鍏佽鐨勬渶澶у埛鏂颁护鐗屾暟閲 }, "Cookie": { "Name": ".Atomx.Auth", @@ -31,10 +43,6 @@ } }, - "WebApi": { - "ServerUrl": "http://localhost:5259" - }, - "Monitoring": { "Enabled": true, "ExcludeHealthChecks": true, @@ -70,16 +78,16 @@ "Seq": { "ServerUrl": "http://log.atomlust.com/", "ApiKey": "bBWmvSE2LJh4KsMeidvF", - "MinimumLevel": "Warning", + "MinimumLevel": "Information", "LevelOverride": { - "Microsoft": "Warning" + "Microsoft": "Information" } }, "Logging": { "LogLevel": { - "Default": "Warning", - "Microsoft.AspNetCore": "Warning" + "Default": "Information", + "Microsoft.AspNetCore": "Information" } }, "AllowedHosts": "*" diff --git a/Atomx.Core/Atomx.Core.csproj b/Atomx.Core/Atomx.Core.csproj index 4457545..63341ff 100644 --- a/Atomx.Core/Atomx.Core.csproj +++ b/Atomx.Core/Atomx.Core.csproj @@ -7,7 +7,6 @@ - diff --git a/Atomx.Core/Class1.cs b/Atomx.Core/Class1.cs deleted file mode 100644 index 6fdbcd0..0000000 --- a/Atomx.Core/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -锘縩amespace Atomx.Core -{ - public class Class1 - { - - } -} diff --git a/Atomx.Core/Jos/LocalizationJob.cs b/Atomx.Core/Jos/LocalizationJob.cs new file mode 100644 index 0000000..9b8d037 --- /dev/null +++ b/Atomx.Core/Jos/LocalizationJob.cs @@ -0,0 +1,26 @@ +锘縰sing Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Atomx.Core.Jos +{ + + /// + /// 澶氳瑷鏈湴鍖栦换鍔 + /// + public partial class LocalizationJob + { + readonly ILogger _logger; + public LocalizationJob(ILogger logger) + { + _logger = logger; + } + + public void Execute() + { + _logger.LogInformation("LocalizationJob executed at: {time}", DateTimeOffset.Now); + // 鍦ㄨ繖閲屾坊鍔犲璇█鏈湴鍖栫殑鍏蜂綋浠诲姟閫昏緫 + } + } +} diff --git a/Atomx.Core/Jos/LocalizationJobs.cs b/Atomx.Core/Jos/LocalizationJobs.cs deleted file mode 100644 index 4f0226c..0000000 --- a/Atomx.Core/Jos/LocalizationJobs.cs +++ /dev/null @@ -1,13 +0,0 @@ -锘縰sing System; -using System.Collections.Generic; -using System.Text; - -namespace Atomx.Core.Jos -{ - /// - /// 澶氳瑷鏈湴鍖栦换鍔 - /// - public class LocalizationJobs - { - } -} diff --git a/Atomx.Core/Services/BackgroundJobsService.cs b/Atomx.Core/Services/BackgroundJobsService.cs new file mode 100644 index 0000000..e117c44 --- /dev/null +++ b/Atomx.Core/Services/BackgroundJobsService.cs @@ -0,0 +1,14 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Text; + +namespace Atomx.Core.Jos +{ + public partial interface IBackgroundJobsService + { + } + + public partial class BackgroundJobsService : IBackgroundJobsService + { + } +}