Files
EmailBill/WebApi/Expand.cs
SunCheng 51172e8c5a
All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 4m27s
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
fix
2026-02-11 13:00:01 +08:00

100 lines
4.5 KiB
C#

using Quartz;
using Service.Jobs;
namespace WebApi;
public static class Expand
{
public static void AddScheduler(this WebApplicationBuilder builder)
{
builder.Services.AddQuartz(q =>
{
// 配置调度器
q.SchedulerId = "EmailBillScheduler";
// 配置邮件同步任务 - 每10分钟执行一次
var emailJobKey = new JobKey("EmailSyncJob");
q.AddJob<EmailSyncJob>(opts => opts
.WithIdentity(emailJobKey)
.WithDescription("邮件同步任务"));
q.AddTrigger(opts => opts
.ForJob(emailJobKey)
.WithIdentity("EmailSyncTrigger")
.WithCronSchedule("0 0/30 * * * ?") // 每30分钟执行
.WithDescription("每30分钟同步一次邮件"));
// 配置周期性账单任务 - 每天早上6点执行
var periodicBillJobKey = new JobKey("PeriodicBillJob");
q.AddJob<PeriodicBillJob>(opts => opts
.WithIdentity(periodicBillJobKey)
.WithDescription("周期性账单任务"));
q.AddTrigger(opts => opts
.ForJob(periodicBillJobKey)
.WithIdentity("PeriodicBillTrigger")
.WithCronSchedule("0 0 6 * * ?") // 每天早上6点执行
.WithDescription("每天早上6点执行周期性账单任务"));
// 配置预算归档任务 - 每个月1号晚11点执行
var budgetArchiveJobKey = new JobKey("BudgetArchiveJob");
q.AddJob<BudgetArchiveJob>(opts => opts
.WithIdentity(budgetArchiveJobKey)
.WithDescription("预算归档任务"));
q.AddTrigger(opts => opts
.ForJob(budgetArchiveJobKey)
.WithIdentity("BudgetArchiveTrigger")
.WithCronSchedule("0 0 23 1 * ?") // 每个月1号晚11点执行
.WithDescription("每个月1号晚11点执行预算归档"));
// 配置数据库备份任务 - 每天早上6点执行
var dbBackupJobKey = new JobKey("DbBackupJob");
q.AddJob<DbBackupJob>(opts => opts
.WithIdentity(dbBackupJobKey)
.WithDescription("数据库备份任务"));
q.AddTrigger(opts => opts
.ForJob(dbBackupJobKey)
.WithIdentity("DbBackupTrigger")
.WithCronSchedule("0 0 6 * * ?") // 每天早上6点执行
.WithDescription("每天早上6点执行数据库备份"));
// 配置日志清理任务 - 每天凌晨2点执行
var logCleanupJobKey = new JobKey("LogCleanupJob");
q.AddJob<LogCleanupJob>(opts => opts
.WithIdentity(logCleanupJobKey)
.WithDescription("日志清理任务"));
q.AddTrigger(opts => opts
.ForJob(logCleanupJobKey)
.WithIdentity("LogCleanupTrigger")
.WithCronSchedule("0 0 2 * * ?") // 每天凌晨2点执行
.WithDescription("每天凌晨2点执行日志清理(保留30天)"));
// 配置分类图标生成任务 - 每24小时执行一次
var categoryIconJobKey = new JobKey("CategoryIconGenerationJob");
q.AddJob<CategoryIconGenerationJob>(opts => opts
.WithIdentity(categoryIconJobKey)
.WithDescription("分类图标生成任务"));
q.AddTrigger(opts => opts
.ForJob(categoryIconJobKey)
.WithIdentity("CategoryIconGenerationTrigger")
.WithCronSchedule("0 0 0 * * ?") // 每24小时执行一次
.WithDescription("每24小时扫描并为无图标分类生成SVG图标"));
// 配置节假日同步任务 - 每10天凌晨3点执行
var holidaySyncJobKey = new JobKey("HolidaySyncJob");
q.AddJob<HolidaySyncJob>(opts => opts
.WithIdentity(holidaySyncJobKey)
.WithDescription("节假日同步任务"));
q.AddTrigger(opts => opts
.ForJob(holidaySyncJobKey)
.WithIdentity("HolidaySyncTrigger")
.WithCronSchedule("0 0 3 */10 * ?") // 每10天凌晨3点执行
.WithDescription("每10天同步节假日数据"));
});
// 添加 Quartz Hosted Service
builder.Services.AddQuartzHostedService(options =>
{
// 等待任务完成后再关闭
options.WaitForJobsToComplete = true;
});
}
}