排课计算、课表发布、补考自动编排三类任务统一接入消息系统。
业务任务与 Outbox 消息同一事务落库,避免“数据库成功但消息没发出去”。 RabbitMQ 使用持久化消息、发布确认、手动 ACK、Quorum Queue、死信队列。 增加处理租约、心跳、异常重试、最大重试次数和幂等状态控制。 保留 InMemory 模式,开发环境无需安装 RabbitMQ。 支持多实例竞争消费,后续可以横向扩容。 新增 /health/messaging 消息系统健康检查。
This commit is contained in:
@@ -98,6 +98,8 @@ public sealed class AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
public DbSet<OfficialDocumentDownload> OfficialDocumentDownloads =>
|
||||
Set<OfficialDocumentDownload>();
|
||||
public DbSet<AuditLog> AuditLogs => Set<AuditLog>();
|
||||
public DbSet<BackgroundJobOutboxMessage> BackgroundJobOutboxMessages =>
|
||||
Set<BackgroundJobOutboxMessage>();
|
||||
|
||||
protected override void ConfigureConventions(
|
||||
ModelConfigurationBuilder configurationBuilder)
|
||||
@@ -993,6 +995,14 @@ public sealed class AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
entity.HasIndex(x => x.CreatedAt);
|
||||
});
|
||||
|
||||
builder.Entity<BackgroundJobOutboxMessage>(entity =>
|
||||
{
|
||||
entity.Property(x => x.LastError).HasMaxLength(2000);
|
||||
entity.HasIndex(x => new { x.JobKind, x.JobId }).IsUnique();
|
||||
entity.HasIndex(x => new { x.State, x.CreatedAt });
|
||||
entity.HasIndex(x => x.LeaseExpiresAt);
|
||||
});
|
||||
|
||||
builder.Entity<OfficialDocument>(entity =>
|
||||
{
|
||||
entity.Property(x => x.DocumentNumber).HasMaxLength(50);
|
||||
|
||||
@@ -58,6 +58,8 @@ public sealed class DevelopmentSqliteMigrator(
|
||||
"20260726_31_unified_message_center";
|
||||
private const string ClassroomReservationsMigration =
|
||||
"20260726_32_classroom_reservations";
|
||||
private const string BackgroundJobOutboxMigration =
|
||||
"20260726_33_background_job_outbox";
|
||||
|
||||
public async Task MigrateAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -421,6 +423,19 @@ public sealed class DevelopmentSqliteMigrator(
|
||||
ClassroomReservationsMigration,
|
||||
classroomReservationsExist ? [] : ClassroomReservationStatements,
|
||||
cancellationToken);
|
||||
|
||||
var backgroundJobOutboxExists = await db.Database
|
||||
.SqlQueryRaw<int>(
|
||||
"""
|
||||
SELECT COUNT(*) AS "Value"
|
||||
FROM sqlite_master
|
||||
WHERE type = 'table' AND name = 'BackgroundJobOutboxMessages'
|
||||
""")
|
||||
.AnyAsync(value => value > 0, cancellationToken);
|
||||
await ApplyMigrationAsync(
|
||||
BackgroundJobOutboxMigration,
|
||||
backgroundJobOutboxExists ? [] : BackgroundJobOutboxStatements,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
private async Task ApplyMigrationAsync(
|
||||
@@ -2004,4 +2019,37 @@ public sealed class DevelopmentSqliteMigrator(
|
||||
ON "ClassroomReservations" ("ReviewedByUserId");
|
||||
"""
|
||||
];
|
||||
|
||||
private static readonly string[] BackgroundJobOutboxStatements =
|
||||
[
|
||||
"""
|
||||
CREATE TABLE "BackgroundJobOutboxMessages" (
|
||||
"Id" TEXT NOT NULL CONSTRAINT "PK_BackgroundJobOutboxMessages" PRIMARY KEY,
|
||||
"JobKind" INTEGER NOT NULL,
|
||||
"JobId" TEXT NOT NULL,
|
||||
"State" INTEGER NOT NULL,
|
||||
"PublishAttempts" INTEGER NOT NULL,
|
||||
"ProcessingAttempts" INTEGER NOT NULL,
|
||||
"PublishedAt" TEXT NULL,
|
||||
"ProcessingToken" TEXT NULL,
|
||||
"LeaseExpiresAt" TEXT NULL,
|
||||
"CompletedAt" TEXT NULL,
|
||||
"LastError" TEXT NULL,
|
||||
"CreatedAt" TEXT NOT NULL,
|
||||
"UpdatedAt" TEXT NOT NULL
|
||||
);
|
||||
""",
|
||||
"""
|
||||
CREATE UNIQUE INDEX "IX_BackgroundJobOutboxMessages_JobKind_JobId"
|
||||
ON "BackgroundJobOutboxMessages" ("JobKind", "JobId");
|
||||
""",
|
||||
"""
|
||||
CREATE INDEX "IX_BackgroundJobOutboxMessages_LeaseExpiresAt"
|
||||
ON "BackgroundJobOutboxMessages" ("LeaseExpiresAt");
|
||||
""",
|
||||
"""
|
||||
CREATE INDEX "IX_BackgroundJobOutboxMessages_State_CreatedAt"
|
||||
ON "BackgroundJobOutboxMessages" ("State", "CreatedAt");
|
||||
"""
|
||||
];
|
||||
}
|
||||
|
||||
+4845
File diff suppressed because it is too large
Load Diff
+62
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class BackgroundJobOutbox : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "BackgroundJobOutboxMessages",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "char(36)", nullable: false),
|
||||
JobKind = table.Column<int>(type: "int", nullable: false),
|
||||
JobId = table.Column<Guid>(type: "char(36)", nullable: false),
|
||||
State = table.Column<int>(type: "int", nullable: false),
|
||||
PublishAttempts = table.Column<int>(type: "int", nullable: false),
|
||||
ProcessingAttempts = table.Column<int>(type: "int", nullable: false),
|
||||
PublishedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
|
||||
ProcessingToken = table.Column<Guid>(type: "char(36)", nullable: true),
|
||||
LeaseExpiresAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
|
||||
CompletedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
|
||||
LastError = table.Column<string>(type: "varchar(2000)", maxLength: 2000, nullable: true),
|
||||
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
|
||||
UpdatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_BackgroundJobOutboxMessages", x => x.Id);
|
||||
})
|
||||
.Annotation("MySQL:Charset", "utf8mb4");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_BackgroundJobOutboxMessages_JobKind_JobId",
|
||||
table: "BackgroundJobOutboxMessages",
|
||||
columns: new[] { "JobKind", "JobId" },
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_BackgroundJobOutboxMessages_LeaseExpiresAt",
|
||||
table: "BackgroundJobOutboxMessages",
|
||||
column: "LeaseExpiresAt");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_BackgroundJobOutboxMessages_State_CreatedAt",
|
||||
table: "BackgroundJobOutboxMessages",
|
||||
columns: new[] { "State", "CreatedAt" });
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "BackgroundJobOutboxMessages");
|
||||
}
|
||||
}
|
||||
}
|
||||
+55
@@ -3423,6 +3423,61 @@ namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
|
||||
b.ToTable("AuditLogs");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Jiaowu.Api.Domain.System.BackgroundJobOutboxMessage", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<DateTime?>("CompletedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<Guid>("JobId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<int>("JobKind")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("LastError")
|
||||
.HasMaxLength(2000)
|
||||
.HasColumnType("varchar(2000)");
|
||||
|
||||
b.Property<DateTime?>("LeaseExpiresAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<int>("ProcessingAttempts")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<Guid?>("ProcessingToken")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<int>("PublishAttempts")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime?>("PublishedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<int>("State")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("UpdatedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("LeaseExpiresAt");
|
||||
|
||||
b.HasIndex("JobKind", "JobId")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("State", "CreatedAt");
|
||||
|
||||
b.ToTable("BackgroundJobOutboxMessages");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
|
||||
Reference in New Issue
Block a user