2025-12-29 15:20:32 +08:00
|
|
|
|
using Quartz;
|
2026-01-18 22:04:56 +08:00
|
|
|
|
using Service.Jobs;
|
2025-12-29 15:20:32 +08:00
|
|
|
|
|
|
|
|
|
|
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");
|
2026-01-18 22:04:56 +08:00
|
|
|
|
q.AddJob<EmailSyncJob>(opts => opts
|
2026-01-09 14:03:01 +08:00
|
|
|
|
.WithIdentity(emailJobKey)
|
|
|
|
|
|
.WithDescription("邮件同步任务"));
|
2025-12-29 15:20:32 +08:00
|
|
|
|
q.AddTrigger(opts => opts
|
|
|
|
|
|
.ForJob(emailJobKey)
|
|
|
|
|
|
.WithIdentity("EmailSyncTrigger")
|
2026-01-01 11:58:21 +08:00
|
|
|
|
.WithCronSchedule("0 0/30 * * * ?") // 每30分钟执行
|
|
|
|
|
|
.WithDescription("每30分钟同步一次邮件"));
|
2025-12-29 15:20:32 +08:00
|
|
|
|
|
|
|
|
|
|
// 配置周期性账单任务 - 每天早上6点执行
|
|
|
|
|
|
var periodicBillJobKey = new JobKey("PeriodicBillJob");
|
2026-01-18 22:04:56 +08:00
|
|
|
|
q.AddJob<PeriodicBillJob>(opts => opts
|
2026-01-09 14:03:01 +08:00
|
|
|
|
.WithIdentity(periodicBillJobKey)
|
|
|
|
|
|
.WithDescription("周期性账单任务"));
|
2025-12-29 15:20:32 +08:00
|
|
|
|
q.AddTrigger(opts => opts
|
|
|
|
|
|
.ForJob(periodicBillJobKey)
|
|
|
|
|
|
.WithIdentity("PeriodicBillTrigger")
|
|
|
|
|
|
.WithCronSchedule("0 0 6 * * ?") // 每天早上6点执行
|
2026-01-09 14:03:01 +08:00
|
|
|
|
.WithDescription("每天早上6点执行周期性账单任务"));
|
|
|
|
|
|
|
|
|
|
|
|
// 配置预算归档任务 - 每个月1号晚11点执行
|
|
|
|
|
|
var budgetArchiveJobKey = new JobKey("BudgetArchiveJob");
|
2026-01-18 22:04:56 +08:00
|
|
|
|
q.AddJob<BudgetArchiveJob>(opts => opts
|
2026-01-09 14:03:01 +08:00
|
|
|
|
.WithIdentity(budgetArchiveJobKey)
|
|
|
|
|
|
.WithDescription("预算归档任务"));
|
|
|
|
|
|
q.AddTrigger(opts => opts
|
|
|
|
|
|
.ForJob(budgetArchiveJobKey)
|
|
|
|
|
|
.WithIdentity("BudgetArchiveTrigger")
|
|
|
|
|
|
.WithCronSchedule("0 0 23 1 * ?") // 每个月1号晚11点执行
|
|
|
|
|
|
.WithDescription("每个月1号晚11点执行预算归档"));
|
2025-12-29 15:20:32 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 添加 Quartz Hosted Service
|
|
|
|
|
|
builder.Services.AddQuartzHostedService(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 等待任务完成后再关闭
|
|
|
|
|
|
options.WaitForJobsToComplete = true;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|