重构: 将 LogCleanupService 转为 Quartz Job 服务
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 22s
Docker Build & Deploy / Deploy to Production (push) Successful in 7s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s

- 创建 LogCleanupJob 替代 LogCleanupService (BackgroundService)
- 在 Expand.cs 中注册 LogCleanupJob (每天凌晨2点执行, 保留30天日志)
- 从 Program.cs 移除 LogCleanupService 的 HostedService 注册
- 删除 Service/LogCleanupService.cs
- 删除 Service/PeriodicBillBackgroundService.cs (已无用的重复服务)

所有后台任务现在统一通过 Quartz.NET 管理, 支持运行时控制
This commit is contained in:
SunCheng
2026-01-28 11:19:23 +08:00
parent b71eadd4f9
commit 3ed9cf5ebd
26 changed files with 133 additions and 183 deletions

View File

@@ -0,0 +1,80 @@
namespace Service.Message;
public interface IMessageService
{
Task<(IEnumerable<MessageRecord> List, long Total)> GetPagedListAsync(int pageIndex, int pageSize);
Task<MessageRecord?> GetByIdAsync(long id);
Task<bool> AddAsync(MessageRecord message);
Task<bool> AddAsync(string title, string content, MessageType type = MessageType.Text, string? url = null);
Task<bool> MarkAsReadAsync(long id);
Task<bool> MarkAllAsReadAsync();
Task<bool> DeleteAsync(long id);
Task<long> GetUnreadCountAsync();
}
public class MessageService(IMessageRecordRepository messageRepo, INotificationService notificationService) : IMessageService
{
public async Task<(IEnumerable<MessageRecord> List, long Total)> GetPagedListAsync(int pageIndex, int pageSize)
{
return await messageRepo.GetPagedListAsync(pageIndex, pageSize);
}
public async Task<MessageRecord?> GetByIdAsync(long id)
{
return await messageRepo.GetByIdAsync(id);
}
public async Task<bool> AddAsync(MessageRecord message)
{
return await messageRepo.AddAsync(message);
}
public async Task<bool> AddAsync(
string title,
string content,
MessageType type = MessageType.Text,
string? url = null
)
{
var message = new MessageRecord
{
Title = title,
Content = content,
MessageType = type,
Url = url,
IsRead = false
};
var result = await messageRepo.AddAsync(message);
if (result)
{
await notificationService.SendNotificationAsync(title, url);
}
return result;
}
public async Task<bool> MarkAsReadAsync(long id)
{
var message = await messageRepo.GetByIdAsync(id);
if (message == null) return false;
message.IsRead = true;
message.UpdateTime = DateTime.Now;
return await messageRepo.UpdateAsync(message);
}
public async Task<bool> MarkAllAsReadAsync()
{
return await messageRepo.MarkAllAsReadAsync();
}
public async Task<bool> DeleteAsync(long id)
{
return await messageRepo.DeleteAsync(id);
}
public async Task<long> GetUnreadCountAsync()
{
var list = await messageRepo.GetAllAsync();
return list.Count(x => !x.IsRead);
}
}

View File

@@ -0,0 +1,94 @@
using WebPush;
using PushSubscription = Entity.PushSubscription;
namespace Service.Message;
public interface INotificationService
{
Task<string> GetVapidPublicKeyAsync();
Task SubscribeAsync(PushSubscription subscription);
Task SendNotificationAsync(string message, string? url = null);
}
public class NotificationService(
IPushSubscriptionRepository subscriptionRepo,
IConfiguration configuration,
ILogger<NotificationService> logger) : INotificationService
{
private NotificationSettings GetSettings()
{
var settings = configuration.GetSection("NotificationSettings").Get<NotificationSettings>();
if (settings == null)
{
// Fallback or throw. For now, let's return empty to avoid crashing if not configured,
// but logging error is better.
logger.LogWarning("NotificationSettings not configured");
return new NotificationSettings();
}
return settings;
}
public Task<string> GetVapidPublicKeyAsync()
{
return Task.FromResult(GetSettings().PublicKey);
}
public async Task SubscribeAsync(PushSubscription subscription)
{
var existing = await subscriptionRepo.GetByEndpointAsync(subscription.Endpoint);
if (existing != null)
{
existing.P256DH = subscription.P256DH;
existing.Auth = subscription.Auth;
existing.UpdateTime = DateTime.Now;
await subscriptionRepo.UpdateAsync(existing);
}
else
{
await subscriptionRepo.AddAsync(subscription);
}
}
public async Task SendNotificationAsync(string message, string? url = null)
{
var settings = GetSettings();
if (string.IsNullOrEmpty(settings.PublicKey) || string.IsNullOrEmpty(settings.PrivateKey))
{
logger.LogWarning("VAPID keys not configured, skipping notification");
return;
}
var vapidDetails = new VapidDetails(settings.Subject, settings.PublicKey, settings.PrivateKey);
var webPushClient = new WebPushClient();
var subscriptions = await subscriptionRepo.GetAllAsync();
var payload = JsonSerializer.Serialize(new
{
title = "System Notification",
body = message,
url = url ?? "/",
icon = "/pwa-192x192.png"
});
foreach (var sub in subscriptions)
{
try
{
var pushSubscription = new WebPush.PushSubscription(sub.Endpoint, sub.P256DH, sub.Auth);
await webPushClient.SendNotificationAsync(pushSubscription, payload, vapidDetails);
}
catch (WebPushException ex)
{
if (ex.StatusCode == HttpStatusCode.Gone || ex.StatusCode == HttpStatusCode.NotFound)
{
await subscriptionRepo.DeleteAsync(sub.Id);
}
logger.LogError(ex, "Error sending push notification to {Endpoint}", sub.Endpoint);
}
catch (Exception ex)
{
logger.LogError(ex, "Error sending push notification to {Endpoint}", sub.Endpoint);
}
}
}
}