排课计算、课表发布、补考自动编排三类任务统一接入消息系统。
业务任务与 Outbox 消息同一事务落库,避免“数据库成功但消息没发出去”。 RabbitMQ 使用持久化消息、发布确认、手动 ACK、Quorum Queue、死信队列。 增加处理租约、心跳、异常重试、最大重试次数和幂等状态控制。 保留 InMemory 模式,开发环境无需安装 RabbitMQ。 支持多实例竞争消费,后续可以横向扩容。 新增 /health/messaging 消息系统健康检查。
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Security.Claims;
|
||||
using System.Text.Json;
|
||||
using Jiaowu.Api.Domain.Academic;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Domain.System;
|
||||
using Jiaowu.Api.Infrastructure.Persistence;
|
||||
using Jiaowu.Api.Infrastructure.Scheduling;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@@ -15,9 +16,7 @@ namespace Jiaowu.Api.Controllers;
|
||||
[Authorize(Roles = ManagementRoles)]
|
||||
[Route("api/schedules")]
|
||||
public sealed class SchedulesController(
|
||||
AppDbContext db,
|
||||
AutomaticScheduleJobQueue automaticScheduleJobQueue,
|
||||
SchedulePublishJobQueue schedulePublishJobQueue) : ControllerBase
|
||||
AppDbContext db) : ControllerBase
|
||||
{
|
||||
private const string ManagementRoles =
|
||||
SystemRoles.SuperAdmin + "," +
|
||||
@@ -241,6 +240,10 @@ public sealed class SchedulesController(
|
||||
CurrentStep = "等待后台检查"
|
||||
};
|
||||
db.SchedulePublishJobs.Add(job);
|
||||
db.BackgroundJobOutboxMessages.Add(
|
||||
BackgroundJobOutboxMessage.Create(
|
||||
BackgroundJobKind.SchedulePublish,
|
||||
job.Id));
|
||||
try
|
||||
{
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
@@ -263,7 +266,6 @@ public sealed class SchedulesController(
|
||||
ToResponse(existing));
|
||||
}
|
||||
|
||||
schedulePublishJobQueue.Enqueue(job.Id);
|
||||
return AcceptedAtAction(
|
||||
nameof(GetSchedulePublishJob),
|
||||
new { jobId = job.Id },
|
||||
@@ -321,6 +323,10 @@ public sealed class SchedulesController(
|
||||
RequestedByUserId = requestedByUserId
|
||||
};
|
||||
db.AutomaticScheduleJobs.Add(job);
|
||||
db.BackgroundJobOutboxMessages.Add(
|
||||
BackgroundJobOutboxMessage.Create(
|
||||
BackgroundJobKind.AutomaticSchedule,
|
||||
job.Id));
|
||||
try
|
||||
{
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
@@ -339,7 +345,6 @@ public sealed class SchedulesController(
|
||||
ToResponse(existing));
|
||||
}
|
||||
|
||||
automaticScheduleJobQueue.Enqueue(job.Id);
|
||||
return AcceptedAtAction(
|
||||
nameof(GetAutomaticScheduleJob),
|
||||
new { jobId = job.Id },
|
||||
|
||||
Reference in New Issue
Block a user