Files
TakeoutSaaS.TenantApi/src/Modules/TakeoutSaaS.Module.Scheduler/Extensions/SchedulerServiceCollectionExtensions.cs
MSuMshk 04e76cd519
All checks were successful
Build and Deploy TenantApi + SkuWorker / build-and-deploy (push) Successful in 1m50s
feat: 添加订单超时自动取消与 SMS 告警任务
- OrderTimeoutJob 替换占位实现为真实逻辑(查询超时订单 → 批量取消 → 发布事件)
- 新增 OrderEscalationJob(每 2 分钟检查超时未接单订单,预留 SMS 告警)
- 新增 OrderTimeoutOptions(AutoCancelMinutes=15, SmsEscalationMinutes=10)
- RecurringJobRegistrar 注册 OrderEscalationJob
- SchedulerServiceCollectionExtensions 注册 Job + Options
2026-02-27 13:11:01 +08:00

92 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Hangfire;
using Hangfire.PostgreSql;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using TakeoutSaaS.Module.Scheduler.Abstractions;
using TakeoutSaaS.Module.Scheduler.HostedServices;
using TakeoutSaaS.Module.Scheduler.Jobs;
using TakeoutSaaS.Module.Scheduler.Options;
using TakeoutSaaS.Module.Scheduler.Services;
namespace TakeoutSaaS.Module.Scheduler.Extensions;
/// <summary>
/// 调度模块注册扩展(默认 Hangfire
/// </summary>
public static class SchedulerServiceCollectionExtensions
{
/// <summary>
/// 注册调度模块。
/// </summary>
public static IServiceCollection AddSchedulerModule(this IServiceCollection services, IConfiguration configuration)
{
services.AddOptions<SchedulerOptions>()
.Bind(configuration.GetSection("Scheduler"))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<SubscriptionAutomationOptions>()
.Bind(configuration.GetSection("Scheduler:SubscriptionAutomation"))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<BillingAutomationOptions>()
.Bind(configuration.GetSection("Scheduler:BillingAutomation"))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<OrderTimeoutOptions>()
.Bind(configuration.GetSection("Scheduler:OrderTimeout"))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddHangfire((serviceProvider, config) =>
{
var options = serviceProvider.GetRequiredService<IOptionsMonitor<SchedulerOptions>>().CurrentValue;
config
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UsePostgreSqlStorage(storage =>
{
storage.UseNpgsqlConnection(options.ConnectionString);
});
});
services.AddHangfireServer((serviceProvider, options) =>
{
var scheduler = serviceProvider.GetRequiredService<IOptionsMonitor<SchedulerOptions>>().CurrentValue;
options.WorkerCount = scheduler.WorkerCount ?? options.WorkerCount;
});
services.AddSingleton<IRecurringJobRegistrar, RecurringJobRegistrar>();
services.AddHostedService<RecurringJobHostedService>();
services.AddScoped<OrderTimeoutJob>();
services.AddScoped<OrderEscalationJob>();
services.AddScoped<CouponExpireJob>();
services.AddScoped<LogCleanupJob>();
services.AddScoped<SubscriptionRenewalReminderJob>();
services.AddScoped<SubscriptionExpiryCheckJob>();
services.AddScoped<SubscriptionAutoRenewalJob>();
services.AddScoped<BillingOverdueProcessJob>();
return services;
}
/// <summary>
/// 启用 Hangfire Dashboard默认关闭可通过配置开启
/// </summary>
public static IApplicationBuilder UseSchedulerDashboard(this IApplicationBuilder app, IConfiguration configuration)
{
var options = configuration.GetSection("Scheduler").Get<SchedulerOptions>();
if (options is { DashboardEnabled: true })
{
app.UseHangfireDashboard(options.DashboardPath);
}
return app;
}
}