feat: 收口小程序构建上传阶段

This commit is contained in:
que01 committed 2026-10-17 18:01:38 +08:00
1 parent 6420b85019
commit ec16fff70e
23 files changed
+1185 -104

No files matched your search

@@ -0,0 +1,36 @@
CREATE TYPE "MiniAppUploadStatus" AS ENUM ('idle', 'uploading', 'uploaded', 'failed', 'timed_out');
CREATE TYPE "MiniAppReviewStatus" AS ENUM ('not_submitted', 'under_review', 'approved', 'rejected');
CREATE TYPE "MiniAppAttemptPhase" AS ENUM ('build', 'upload');
CREATE TYPE "MiniAppAttemptStatus" AS ENUM ('running', 'succeeded', 'failed', 'timed_out');
ALTER TABLE "MiniAppReleaseTask"
ADD COLUMN "uploadStatus" "MiniAppUploadStatus" NOT NULL DEFAULT 'idle',
ADD COLUMN "reviewStatus" "MiniAppReviewStatus" NOT NULL DEFAULT 'not_submitted';
CREATE TABLE "MiniAppReleaseAttempt" (
"id" TEXT NOT NULL,
"taskId" TEXT NOT NULL,
"brandId" TEXT NOT NULL,
"actorId" TEXT NOT NULL,
"phase" "MiniAppAttemptPhase" NOT NULL,
"attempt" INTEGER NOT NULL,
"status" "MiniAppAttemptStatus" NOT NULL DEFAULT 'running',
"leaseToken" TEXT NOT NULL,
"leaseExpiresAt" TIMESTAMP(3) NOT NULL,
"startedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"heartbeatAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"finishedAt" TIMESTAMP(3),
"artifactPath" TEXT,
"externalId" TEXT,
"error" TEXT,
"logs" JSONB,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "MiniAppReleaseAttempt_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "MiniAppReleaseAttempt_leaseToken_key" ON "MiniAppReleaseAttempt"("leaseToken");
CREATE UNIQUE INDEX "MiniAppReleaseAttempt_taskId_phase_attempt_key" ON "MiniAppReleaseAttempt"("taskId", "phase", "attempt");
CREATE INDEX "MiniAppReleaseAttempt_brandId_taskId_phase_status_idx" ON "MiniAppReleaseAttempt"("brandId", "taskId", "phase", "status");
ALTER TABLE "MiniAppReleaseAttempt" ADD CONSTRAINT "MiniAppReleaseAttempt_taskId_fkey" FOREIGN KEY ("taskId") REFERENCES "MiniAppReleaseTask"("id") ON DELETE CASCADE ON UPDATE CASCADE;
@@ -0,0 +1,11 @@
UPDATE "MiniAppReleaseTask"
SET "uploadStatus" = 'uploaded'
WHERE "status" IN ('uploaded', 'under_review', 'approved', 'published');
UPDATE "MiniAppReleaseTask"
SET "reviewStatus" = CASE
WHEN "status" = 'under_review' THEN 'under_review'::"MiniAppReviewStatus"
WHEN "status" IN ('approved', 'published') THEN 'approved'::"MiniAppReviewStatus"
WHEN "status" = 'rejected' THEN 'rejected'::"MiniAppReviewStatus"
ELSE 'not_submitted'::"MiniAppReviewStatus"
END;
+55
View File
@@ -122,10 +122,13 @@ model MiniAppReleaseTask {
artifactPath String?
buildLogs Json?
buildStatus MiniAppBuildStatus @default(idle)
uploadStatus MiniAppUploadStatus @default(idle)
reviewStatus MiniAppReviewStatus @default(not_submitted)
actorId String
error String?
weappUploadId String?
audit Json?
attempts MiniAppReleaseAttempt[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@ -140,6 +143,58 @@ enum MiniAppBuildStatus {
failed
}
enum MiniAppUploadStatus {
idle
uploading
uploaded
failed
timed_out
}
enum MiniAppReviewStatus {
not_submitted
under_review
approved
rejected
}
enum MiniAppAttemptPhase {
build
upload
}
enum MiniAppAttemptStatus {
running
succeeded
failed
timed_out
}
model MiniAppReleaseAttempt {
id String @id @default(uuid())
taskId String
task MiniAppReleaseTask @relation(fields: [taskId], references: [id], onDelete: Cascade)
brandId String
actorId String
phase MiniAppAttemptPhase
attempt Int
status MiniAppAttemptStatus @default(running)
leaseToken String @unique
leaseExpiresAt DateTime
startedAt DateTime @default(now())
heartbeatAt DateTime @default(now())
finishedAt DateTime?
artifactPath String?
externalId String?
error String?
logs Json?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([taskId, phase, attempt])
@@index([brandId, taskId, phase, status])
}
enum PlatformTemplateRole {
none
template_admin
@@ -1,20 +1,22 @@
import { Injectable } from "@nestjs/common";
import { spawn } from "node:child_process";
import { REPOSITORY_ROOT } from "./miniapp-artifact.js";
import { miniappBuildTimeoutMs } from "./miniapp-build-status.js";
interface BuildProcess {
stdout: NodeJS.ReadableStream;
stderr: NodeJS.ReadableStream;
on: (event: "close" | "error", listener: (code: number | null) => void) => void;
kill: (signal?: NodeJS.Signals) => boolean;
}
@Injectable()
export class AdminMiniAppBuildService {
run(
templateKey: string,
buildConfig: { apiBaseUrl: string; appId: string },
buildConfig: { apiBaseUrl: string; appId: string; outputRoot: string },
onLog: (message: string, stream: "stdout" | "stderr") => void,
): Promise<{ exitCode: number }> {
): Promise<{ exitCode: number; timedOut?: boolean }> {
return new Promise((resolvePromise, rejectPromise) => {
const child = spawn("pnpm", ["--filter", "@personal-brand/taro", "build:weapp"], {
cwd: REPOSITORY_ROOT,
@@ -24,6 +26,7 @@ export class AdminMiniAppBuildService {
TARO_ENV: "weapp",
API_BASE_URL: buildConfig.apiBaseUrl,
WEAPP_APP_ID: buildConfig.appId,
MINIAPP_OUTPUT_ROOT: buildConfig.outputRoot,
},
shell: process.platform === "win32",
}) as BuildProcess;
@@ -40,8 +43,26 @@ export class AdminMiniAppBuildService {
};
readStream(child.stdout, "stdout");
readStream(child.stderr, "stderr");
child.on("close", (code) => resolvePromise({ exitCode: code ?? 1 }));
child.on("error", () => rejectPromise(new Error("小程序构建进程启动失败")));
let settled = false;
const timer = setTimeout(() => {
if (settled) return;
settled = true;
onLog("构建超过允许时长,已终止构建进程", "stderr");
child.kill("SIGKILL");
resolvePromise({ exitCode: 124, timedOut: true });
}, miniappBuildTimeoutMs());
child.on("close", (code) => {
if (settled) return;
settled = true;
clearTimeout(timer);
resolvePromise({ exitCode: code ?? 1 });
});
child.on("error", () => {
if (settled) return;
settled = true;
clearTimeout(timer);
rejectPromise(new Error("小程序构建进程启动失败"));
});
});
}
}
@@ -5,8 +5,33 @@ import {
NotFoundException,
} from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { randomBytes } from "node:crypto";
import { PrismaService } from "../../prisma.service.js";
const DEFAULT_ATTEMPT_LEASE_MS = 30 * 60 * 1000;
type AttemptPhase = "build" | "upload";
type AttemptStatus = "running" | "succeeded" | "failed" | "timed_out";
const attemptResponseSelect = {
id: true,
phase: true,
attempt: true,
status: true,
leaseExpiresAt: true,
startedAt: true,
heartbeatAt: true,
finishedAt: true,
artifactPath: true,
externalId: true,
error: true,
createdAt: true,
updatedAt: true,
} as const;
export function miniAppAttemptLeaseMs(configured = process.env.MINIAPP_ATTEMPT_LEASE_MS): number {
const parsed = Number(configured);
return Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_ATTEMPT_LEASE_MS;
}
const transitions: Record<string, string[]> = {
built: ["uploaded", "under_review", "failed"],
uploaded: ["under_review", "failed"],
@@ -24,10 +49,22 @@ export class AdminMiniAppReleaseService {
async latest(brandId: string) {
return this.prisma.miniAppReleaseTask.findFirst({
where: { brandId },
include: {
attempts: { select: attemptResponseSelect, orderBy: { createdAt: "asc" } },
},
orderBy: { createdAt: "desc" },
});
}
async get(brandId: string, taskId: string) {
return this.prisma.miniAppReleaseTask.findFirst({
where: { id: taskId, brandId },
include: {
attempts: { select: attemptResponseSelect, orderBy: { createdAt: "asc" } },
},
});
}
async settings(brandId: string) {
const brand = await this.prisma.brandProfile.findUnique({
where: { id: brandId },
@@ -41,7 +78,7 @@ export class AdminMiniAppReleaseService {
this.prisma.miniAppReleaseTask.findFirst({ where: { id: taskId, brandId } }),
this.prisma.brandProfile.findUniqueOrThrow({
where: { id: brandId },
select: { miniAppSettings: true, templateKey: true },
select: { miniAppSettings: true, templateKey: true, slug: true },
}),
]);
if (!task) throw new BadRequestException("小程序发布任务不存在");
@@ -58,6 +95,9 @@ export class AdminMiniAppReleaseService {
async list(brandId: string): Promise<unknown[]> {
return this.prisma.miniAppReleaseTask.findMany({
where: { brandId },
include: {
attempts: { select: attemptResponseSelect, orderBy: { createdAt: "asc" } },
},
orderBy: { createdAt: "desc" },
take: 50,
});
@@ -129,6 +169,10 @@ export class AdminMiniAppReleaseService {
? { buildLogs: input.buildLogs as Prisma.InputJsonValue }
: {}),
...(input.buildStatus ? { buildStatus: input.buildStatus } : {}),
...(input.status === "uploaded" ? { uploadStatus: "uploaded" } : {}),
...(input.status === "under_review" ? { reviewStatus: "under_review" } : {}),
...(input.status === "approved" ? { reviewStatus: "approved" } : {}),
...(input.status === "rejected" ? { reviewStatus: "rejected" } : {}),
audit,
},
});
@@ -149,6 +193,7 @@ export class AdminMiniAppReleaseService {
buildStatus?: "idle" | "building" | "success" | "failed";
error?: string | null;
buildLogs?: unknown;
artifactPath?: string | null;
},
): Promise<unknown> {
const task = await this.prisma.miniAppReleaseTask.findFirst({
@@ -164,6 +209,7 @@ export class AdminMiniAppReleaseService {
...(input.buildLogs !== undefined
? { buildLogs: input.buildLogs as Prisma.InputJsonValue }
: {}),
...(input.artifactPath !== undefined ? { artifactPath: input.artifactPath } : {}),
...(input.buildStatus ? { buildStatus: input.buildStatus } : {}),
...(buildStatusChanged
? {
@@ -180,6 +226,222 @@ export class AdminMiniAppReleaseService {
});
}
async acquireAttempt(
brandId: string,
actorId: string,
taskId: string,
phase: AttemptPhase,
): Promise<{ id: string; leaseToken: string; attempt: number; leaseExpiresAt: Date }> {
try {
return await this.prisma.$transaction(
async (tx) => {
const task = await tx.miniAppReleaseTask.findFirst({
where: { id: taskId, brandId },
});
if (!task) throw new NotFoundException("小程序发布任务不存在");
const now = new Date();
const active = await tx.miniAppReleaseAttempt.findFirst({
where: { taskId, phase, status: "running" },
orderBy: { attempt: "desc" },
});
if (active) {
if (active.leaseExpiresAt > now)
throw new ConflictException("该小程序操作正在进行中,请稍后");
await tx.miniAppReleaseAttempt.update({
where: { id: active.id },
data: {
status: "timed_out",
error: "操作租约已过期,按中断处理",
finishedAt: now,
},
});
await tx.miniAppReleaseTask.update({
where: { id: taskId },
data:
phase === "build"
? { buildStatus: "failed", error: "操作租约已过期,按中断处理" }
: { uploadStatus: "timed_out", error: "操作租约已过期,按中断处理" },
});
}
const previous = await tx.miniAppReleaseAttempt.findFirst({
where: { taskId, phase },
orderBy: { attempt: "desc" },
select: { attempt: true },
});
const leaseExpiresAt = new Date(now.getTime() + miniAppAttemptLeaseMs());
const created = await tx.miniAppReleaseAttempt.create({
data: {
taskId,
brandId,
actorId,
phase,
attempt: (previous?.attempt ?? 0) + 1,
leaseToken: randomBytes(32).toString("hex"),
leaseExpiresAt,
},
});
await tx.miniAppReleaseTask.update({
where: { id: taskId },
data:
phase === "build"
? { buildStatus: "building", error: null }
: { uploadStatus: "uploading", error: null },
});
return {
id: created.id,
leaseToken: created.leaseToken,
attempt: created.attempt,
leaseExpiresAt: created.leaseExpiresAt,
};
},
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
);
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2034")
throw new ConflictException("该小程序操作刚被其他请求占用,请刷新后重试");
throw error;
}
}
async heartbeatAttempt(
brandId: string,
attemptId: string,
leaseToken: string,
input: { logs?: unknown; artifactPath?: string },
): Promise<unknown> {
const attempt = await this.prisma.miniAppReleaseAttempt.findFirst({
where: { id: attemptId, brandId },
});
if (!attempt) throw new NotFoundException("小程序操作记录不存在");
const now = new Date();
if (attempt.status !== "running" || attempt.leaseToken !== leaseToken)
throw new ConflictException("小程序操作租约已失效");
if (attempt.leaseExpiresAt <= now) {
await this.prisma.$transaction(async (tx) => {
await tx.miniAppReleaseAttempt.updateMany({
where: { id: attempt.id, brandId, status: "running", leaseToken },
data: { status: "timed_out", finishedAt: now, error: "操作租约已过期" },
});
await tx.miniAppReleaseTask.update({
where: { id: attempt.taskId },
data:
attempt.phase === "build"
? { buildStatus: "failed", error: "操作租约已过期" }
: { uploadStatus: "timed_out", error: "操作租约已过期" },
});
});
throw new ConflictException("小程序操作租约已过期");
}
const updateResult = await this.prisma.miniAppReleaseAttempt.updateMany({
where: {
id: attempt.id,
brandId,
status: "running",
leaseToken,
leaseExpiresAt: { gt: now },
},
data: {
heartbeatAt: now,
leaseExpiresAt: new Date(now.getTime() + miniAppAttemptLeaseMs()),
...(input.logs !== undefined ? { logs: input.logs as Prisma.InputJsonValue } : {}),
...(input.artifactPath ? { artifactPath: input.artifactPath } : {}),
},
});
if (updateResult.count !== 1) throw new ConflictException("小程序操作租约已失效");
const updated = await this.prisma.miniAppReleaseAttempt.findFirst({
where: { id: attempt.id, brandId },
});
if (input.logs !== undefined && attempt.phase === "build") {
await this.prisma.miniAppReleaseTask.update({
where: { id: attempt.taskId },
data: { buildLogs: input.logs as Prisma.InputJsonValue },
});
}
return updated;
}
async finishAttempt(
brandId: string,
attemptId: string,
leaseToken: string,
input: {
status: Exclude<AttemptStatus, "running">;
error?: string | null;
logs?: unknown;
artifactPath?: string | null;
externalId?: string | null;
},
): Promise<unknown> {
const result = await this.prisma.$transaction(
async (tx) => {
const attempt = await tx.miniAppReleaseAttempt.findFirst({
where: { id: attemptId, brandId },
});
if (!attempt) throw new NotFoundException("小程序操作记录不存在");
const now = new Date();
if (attempt.status !== "running" || attempt.leaseToken !== leaseToken)
throw new ConflictException("小程序操作租约已失效");
if (attempt.leaseExpiresAt <= now && input.status !== "timed_out") {
await tx.miniAppReleaseAttempt.update({
where: { id: attempt.id },
data: { status: "timed_out", finishedAt: now, error: "操作租约已过期" },
});
await tx.miniAppReleaseTask.update({
where: { id: attempt.taskId },
data:
attempt.phase === "build"
? { buildStatus: "failed", error: "操作租约已过期" }
: { uploadStatus: "timed_out", error: "操作租约已过期" },
});
return { expired: true as const, finished: null };
}
const finished = await tx.miniAppReleaseAttempt.update({
where: { id: attempt.id },
data: {
status: input.status,
finishedAt: now,
heartbeatAt: now,
...(input.error !== undefined ? { error: input.error } : {}),
...(input.logs !== undefined ? { logs: input.logs as Prisma.InputJsonValue } : {}),
...(input.artifactPath !== undefined ? { artifactPath: input.artifactPath } : {}),
...(input.externalId !== undefined ? { externalId: input.externalId } : {}),
},
});
const taskData: Prisma.MiniAppReleaseTaskUpdateInput =
attempt.phase === "build"
? {
buildStatus:
input.status === "succeeded" ? ("success" as const) : ("failed" as const),
...(input.error !== undefined ? { error: input.error } : {}),
...(input.logs !== undefined
? { buildLogs: input.logs as Prisma.InputJsonValue }
: {}),
...(input.artifactPath !== undefined ? { artifactPath: input.artifactPath } : {}),
}
: {
uploadStatus:
input.status === "succeeded"
? ("uploaded" as const)
: input.status === "timed_out"
? ("timed_out" as const)
: ("failed" as const),
...(input.error !== undefined ? { error: input.error } : {}),
...(input.externalId
? { weappUploadId: input.externalId, status: "uploaded" as const }
: {}),
...(input.status !== "succeeded" ? { status: "failed" as const } : {}),
};
await tx.miniAppReleaseTask.update({ where: { id: attempt.taskId }, data: taskData });
return { expired: false as const, finished };
},
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
);
if (result.expired) throw new ConflictException("小程序操作租约已过期");
return result.finished;
}
private audit(value: Prisma.JsonValue | null, entry: Record<string, unknown>) {
return [...(Array.isArray(value) ? value : []), entry] as Prisma.InputJsonValue[];
}
@@ -8,8 +8,15 @@ import { existsSync } from "node:fs";
import { resolve } from "node:path";
import { getTemplatePackageManifest, templatePackageRegistry } from "@personal-brand/config";
import { AdminMiniAppBuildService } from "./admin-miniapp-build.service.js";
import { AdminMiniAppReleaseService } from "./admin-miniapp-release.service.js";
import { miniappProjectPath } from "./miniapp-artifact.js";
import {
AdminMiniAppReleaseService,
miniAppAttemptLeaseMs,
} from "./admin-miniapp-release.service.js";
import {
miniappReleaseOutputRoot,
miniappReleaseProjectPath,
miniappTaroOutputRoot,
} from "./miniapp-artifact.js";
import { miniappApiBaseUrl, weappAppId } from "./miniapp-build-config.js";
import { buildingStaleMs, isStaleBuilding } from "./miniapp-build-status.js";
import { decryptMiniAppCredential } from "./miniapp-credential-crypto.js";
@@ -22,12 +29,12 @@ interface BuildLog {
stream: "stdout" | "stderr";
}
function miniAppTemplateKey(industryTemplateKey: string): string | null {
function miniAppTemplateManifest(industryTemplateKey: string) {
return (
getTemplatePackageManifest(industryTemplateKey)?.packageKey ??
getTemplatePackageManifest(industryTemplateKey) ??
templatePackageRegistry.find((item) =>
item.supportedIndustryTemplates.includes(industryTemplateKey),
)?.packageKey ??
) ??
null
);
}
@@ -68,66 +75,115 @@ export class AdminMiniAppUploadService {
`上传私钥无法解析(${keyInspection.reason ?? "PEM 格式错误"})。请在“小程序上传配置”里重新粘贴完整私钥。`,
);
const templateKey = miniAppTemplateKey(brand.templateKey);
if (!templateKey) throw new BadRequestException("当前品牌模板没有可上传的小程序包");
const templateManifest = miniAppTemplateManifest(brand.templateKey);
if (!templateManifest) throw new BadRequestException("当前品牌模板没有可上传的小程序包");
const templateKey = templateManifest.packageKey;
const buildConfig = { apiBaseUrl: miniappApiBaseUrl(settings.apiBaseUrl), appId };
const projectPath = miniappProjectPath(templateKey);
const buildLogs = [
log(`开始构建 ${templateKey} 微信小程序`, "stdout"),
log(`请求地址 ${buildConfig.apiBaseUrl},AppID ${buildConfig.appId}`, "stdout"),
];
if (staleBuilding)
buildLogs.push(
log(
`上一次构建已超过 ${Math.round(buildingStaleMs() / 60000)} 分钟没有进展,按中断处理并重新构建`,
"stderr",
),
);
let projectPath = typeof task.artifactPath === "string" ? task.artifactPath : null;
const canReuseArtifact =
(task.status === "built" || task.status === "failed") &&
task.buildStatus === "success" &&
projectPath !== null &&
existsSync(projectPath) &&
existsSync(resolve(projectPath, "app.json"));
let buildLogs = this.buildLogs(task.buildLogs);
if (task.status === "failed") {
await this.releases.updateStatus(brandId, actorId, taskId, {
status: "built",
buildLogs,
buildStatus: "building",
});
} else {
if (!canReuseArtifact) {
const buildAttempt = await this.releases.acquireAttempt(brandId, actorId, taskId, "build");
const outputRoot = miniappReleaseOutputRoot(
brand.slug ?? brandId,
templateKey,
templateManifest.version,
buildAttempt.id,
);
projectPath = miniappReleaseProjectPath(
brand.slug ?? brandId,
templateKey,
templateManifest.version,
buildAttempt.id,
);
buildLogs = [
log(`开始构建 ${templateKey}@${templateManifest.version} 微信小程序`, "stdout"),
log(`请求地址 ${buildConfig.apiBaseUrl},AppID ${buildConfig.appId}`, "stdout"),
];
if (staleBuilding)
buildLogs.push(
log(
`上一次构建已超过 ${Math.round(buildingStaleMs() / 60000)} 分钟没有进展,按中断处理并重新构建`,
"stderr",
),
);
await this.releases.updateBuild(brandId, actorId, taskId, {
buildLogs,
buildStatus: "building",
artifactPath: projectPath,
error: null,
});
const result = await this.builds.run(
templateKey,
{ ...buildConfig, outputRoot: miniappTaroOutputRoot(projectPath) },
(message, stream) => {
buildLogs.push(log(message, stream));
const heartbeat = { logs: buildLogs } as { logs: BuildLog[]; artifactPath?: string };
if (projectPath) heartbeat.artifactPath = projectPath;
void this.releases
.heartbeatAttempt(brandId, buildAttempt.id, buildAttempt.leaseToken, heartbeat)
.catch(() => undefined);
},
);
if (result.exitCode !== 0) {
buildLogs.push(
log(
result.timedOut ? "构建超时,已终止构建进程" : `构建失败,退出码 ${result.exitCode}`,
"stderr",
),
);
await this.releases.finishAttempt(brandId, buildAttempt.id, buildAttempt.leaseToken, {
status: result.timedOut ? "timed_out" : "failed",
error: result.timedOut ? "小程序构建超时,请稍后重试" : "小程序构建失败,请查看构建日志",
logs: buildLogs,
artifactPath: projectPath,
});
throw new BadRequestException(
result.timedOut ? "小程序构建超时,请稍后重试" : "小程序构建失败,请查看构建日志",
);
}
if (
!projectPath ||
!existsSync(projectPath) ||
!existsSync(resolve(projectPath, "app.json"))
) {
buildLogs.push(log(`构建完成但缺少产物:${projectPath ?? "unknown"}/app.json`, "stderr"));
await this.releases.finishAttempt(brandId, buildAttempt.id, buildAttempt.leaseToken, {
status: "failed",
error: "小程序构建产物缺少 app.json",
logs: buildLogs,
artifactPath: projectPath,
});
throw new BadRequestException(`小程序构建产物缺少 app.json:${projectPath}`);
}
buildLogs.push(log("构建完成,开始上传微信", "stdout"));
await this.releases.finishAttempt(brandId, buildAttempt.id, buildAttempt.leaseToken, {
status: "succeeded",
logs: buildLogs,
artifactPath: projectPath,
});
}
const result = await this.builds.run(templateKey, buildConfig, (message, stream) => {
buildLogs.push(log(message, stream));
void this.releases
.updateBuild(brandId, actorId, taskId, { buildLogs, buildStatus: "building" })
.catch(() => undefined);
});
if (result.exitCode !== 0) {
buildLogs.push(log(`构建失败,退出码 ${result.exitCode}`, "stderr"));
await this.releases.updateBuild(brandId, actorId, taskId, {
error: "小程序构建失败,请查看构建日志",
buildLogs,
buildStatus: "failed",
});
throw new BadRequestException("小程序构建失败,请查看构建日志");
}
if (!existsSync(projectPath) || !existsSync(resolve(projectPath, "app.json"))) {
buildLogs.push(log(`构建完成但缺少产物:${projectPath}/app.json`, "stderr"));
await this.releases.updateBuild(brandId, actorId, taskId, {
error: "小程序构建产物缺少 app.json",
buildLogs,
buildStatus: "failed",
});
throw new BadRequestException(`小程序构建产物缺少 app.json:${projectPath}`);
}
buildLogs.push(log("构建完成,开始上传微信", "stdout"));
await this.releases.updateBuild(brandId, actorId, taskId, {
buildLogs,
buildStatus: "success",
});
if (!projectPath) throw new BadRequestException("小程序构建产物不存在,请重新构建");
const uploadAttempt = await this.releases.acquireAttempt(brandId, actorId, taskId, "upload");
const uploadHeartbeat = setInterval(
() => {
void this.releases
.heartbeatAttempt(brandId, uploadAttempt.id, uploadAttempt.leaseToken, {
artifactPath: projectPath ?? undefined,
})
.catch(() => undefined);
},
Math.max(1_000, Math.floor(miniAppAttemptLeaseMs() / 3)),
);
uploadHeartbeat.unref();
try {
const uploadResult = await runMiniAppUpload({
projectPath,
@@ -137,27 +193,42 @@ export class AdminMiniAppUploadService {
desc: input.desc ?? `brand content v${task.version}`,
robot: input.robot ?? 1,
});
return this.releases.updateStatus(brandId, actorId, taskId, {
status: "uploaded",
buildLogs,
buildStatus: "success",
weappUploadId:
typeof uploadResult.strUint64Version === "string"
? uploadResult.strUint64Version
: String(task.version),
const externalId =
typeof uploadResult.strUint64Version === "string"
? uploadResult.strUint64Version
: String(task.version);
await this.releases.finishAttempt(brandId, uploadAttempt.id, uploadAttempt.leaseToken, {
status: "succeeded",
externalId,
artifactPath: projectPath,
});
return this.releases.get(brandId, taskId);
} catch (error) {
const message = error instanceof Error ? error.message : "微信代码上传失败";
await this.releases.updateStatus(brandId, actorId, taskId, {
status: "failed",
buildLogs,
buildStatus: "failed",
await this.releases.finishAttempt(brandId, uploadAttempt.id, uploadAttempt.leaseToken, {
status: message.includes("超时") ? "timed_out" : "failed",
error: message,
artifactPath: projectPath,
});
throw new BadGatewayException(message);
} finally {
clearInterval(uploadHeartbeat);
}
}
private buildLogs(value: unknown): BuildLog[] {
return Array.isArray(value)
? value.filter(
(item): item is BuildLog =>
Boolean(item) &&
typeof item === "object" &&
typeof (item as BuildLog).at === "string" &&
typeof (item as BuildLog).message === "string" &&
((item as BuildLog).stream === "stdout" || (item as BuildLog).stream === "stderr"),
)
: [];
}
private requiredEnv(name: string): string {
const value = process.env[name];
if (!value) throw new BadRequestException(`${name} 未配置`);
+41 -1
View File
@@ -1,4 +1,4 @@
import { resolve } from "node:path";
import { relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
export const REPOSITORY_ROOT = fileURLToPath(new URL("../../../../..", import.meta.url));
@@ -12,3 +12,43 @@ export function miniappArtifactRoot(configured = process.env.MINIAPP_ARTIFACT_RO
export function miniappProjectPath(templatePackageKey: string): string {
return resolve(miniappArtifactRoot(), templatePackageKey, "weapp");
}
export function miniappReleaseOutputRoot(
brandSlug: string,
templateKey: string,
templateVersion: string,
attemptId: string,
): string {
for (const [label, value] of [
["brand slug", brandSlug],
["template key", templateKey],
["template version", templateVersion],
["attempt id", attemptId],
] as const) {
if (!/^[a-zA-Z0-9._-]+$/.test(value))
throw new Error(`Invalid ${label} for miniapp artifact path`);
}
return resolve(
miniappArtifactRoot(),
"releases",
brandSlug,
`${templateKey}@${templateVersion}`,
attemptId,
);
}
export function miniappReleaseProjectPath(
brandSlug: string,
templateKey: string,
templateVersion: string,
attemptId: string,
): string {
return resolve(
miniappReleaseOutputRoot(brandSlug, templateKey, templateVersion, attemptId),
"weapp",
);
}
export function miniappTaroOutputRoot(outputRoot: string): string {
return relative(resolve(REPOSITORY_ROOT, "apps/taro"), outputRoot);
}
@@ -1,4 +1,5 @@
const DEFAULT_STALE_MS = 15 * 60 * 1000;
const DEFAULT_STALE_MS = 30 * 60 * 1000;
const DEFAULT_BUILD_TIMEOUT_MS = 20 * 60 * 1000;
/**
* 构建进程可能被系统杀掉或随 API 一起退出,此时任务会永久停在 `building`。
@@ -18,3 +19,8 @@ export function isStaleBuilding(
if (Number.isNaN(updatedAt)) return false;
return now - updatedAt >= buildingStaleMs();
}
export function miniappBuildTimeoutMs(configured = process.env.MINIAPP_BUILD_TIMEOUT_MS): number {
const parsed = Number(configured);
return Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_BUILD_TIMEOUT_MS;
}
@@ -0,0 +1,56 @@
import assert from "node:assert/strict";
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
import { AdminMiniAppBuildService } from "../src/modules/admin/admin-miniapp-build.service.js";
function fakePnpm(script: string) {
const directory = mkdtempSync(join(tmpdir(), "miniapp-build-worker-"));
const executable = join(directory, "pnpm");
writeFileSync(executable, `#!${process.execPath}\n${script}\n`);
chmodSync(executable, 0o755);
return { directory, executable };
}
test("构建 worker 异常退出时返回明确非零结果", async () => {
const worker = fakePnpm("process.exit(23);");
const previousPath = process.env.PATH;
process.env.PATH = worker.directory;
try {
const result = await new AdminMiniAppBuildService().run(
"template-key",
{ apiBaseUrl: "https://api.example.com", appId: "wx-test", outputRoot: "dist/test" },
() => undefined,
);
assert.deepEqual(result, { exitCode: 23 });
} finally {
if (previousPath === undefined) delete process.env.PATH;
else process.env.PATH = previousPath;
rmSync(worker.directory, { recursive: true, force: true });
}
});
test("构建 worker watchdog 超时会终止子进程并返回 timedOut", async () => {
const worker = fakePnpm("setTimeout(() => {}, 30_000);");
const previousPath = process.env.PATH;
const previousTimeout = process.env.MINIAPP_BUILD_TIMEOUT_MS;
process.env.PATH = worker.directory;
process.env.MINIAPP_BUILD_TIMEOUT_MS = "30";
try {
const messages: string[] = [];
const result = await new AdminMiniAppBuildService().run(
"template-key",
{ apiBaseUrl: "https://api.example.com", appId: "wx-test", outputRoot: "dist/test" },
(message) => messages.push(message),
);
assert.deepEqual(result, { exitCode: 124, timedOut: true });
assert.ok(messages.some((message) => message.includes("终止构建进程")));
} finally {
if (previousPath === undefined) delete process.env.PATH;
else process.env.PATH = previousPath;
if (previousTimeout === undefined) delete process.env.MINIAPP_BUILD_TIMEOUT_MS;
else process.env.MINIAPP_BUILD_TIMEOUT_MS = previousTimeout;
rmSync(worker.directory, { recursive: true, force: true });
}
});
@@ -2,9 +2,10 @@ import assert from "node:assert/strict";
import { generateKeyPairSync } from "node:crypto";
import { test } from "node:test";
import { BadGatewayException, BadRequestException, ConflictException } from "@nestjs/common";
import { templatePackageRegistry } from "@personal-brand/config";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { join, resolve } from "node:path";
import { AdminMiniAppBuildService } from "../src/modules/admin/admin-miniapp-build.service.js";
import { AdminMiniAppUploadService } from "../src/modules/admin/admin-miniapp-upload.service.js";
import type { AdminMiniAppReleaseService } from "../src/modules/admin/admin-miniapp-release.service.js";
@@ -55,6 +56,7 @@ function createHarness(
: mkdtempSync(join(tmpdir(), "miniapp-upload-"));
if (options.artifact !== false) writeFileSync(join(artifactRoot, "app.json"), "{}");
const updates: Record<string, unknown>[] = [];
const buildConfigs: Array<{ apiBaseUrl: string; appId: string; outputRoot: string }> = [];
const releases = {
uploadContext: async () => ({
task: task(options.task),
@@ -63,6 +65,27 @@ function createHarness(
miniAppSettings: options.settings ?? validBrandSettings(),
},
}),
acquireAttempt: async (
_brandId: string,
_actorId: string,
_taskId: string,
phase: "build" | "upload",
) => ({
id: `${phase}-attempt`,
leaseToken: `${phase}-lease`,
attempt: 1,
leaseExpiresAt: new Date(Date.now() + 60_000),
}),
heartbeatAttempt: async () => ({}),
finishAttempt: async (
_brandId: string,
attemptId: string,
_leaseToken: string,
input: object,
) => {
updates.push({ kind: "attempt", attemptId, input });
return input;
},
updateBuild: async (_brandId: string, _actorId: string, _taskId: string, input: object) => {
updates.push({ kind: "build", input });
return input;
@@ -75,15 +98,16 @@ function createHarness(
const builds = {
run: async (
_templateKey: string,
_config: { apiBaseUrl: string; appId: string },
config: { apiBaseUrl: string; appId: string; outputRoot: string },
onLog: (message: string, stream: "stdout" | "stderr") => void,
) => {
buildConfigs.push(config);
onLog("build log", "stdout");
return { exitCode: options.exitCode ?? 0 };
},
} as unknown as AdminMiniAppBuildService;
const service = new AdminMiniAppUploadService(releases, builds);
return { service, updates, artifactRoot };
return { service, updates, artifactRoot, buildConfigs };
}
function createHarnessWithCredential(
@@ -99,6 +123,7 @@ function createHarnessWithCredential(
: mkdtempSync(join(tmpdir(), "miniapp-upload-"));
if (options.artifact !== false) writeFileSync(join(artifactRoot, "app.json"), "{}");
const updates: Record<string, unknown>[] = [];
const buildConfigs: Array<{ apiBaseUrl: string; appId: string; outputRoot: string }> = [];
const releases = {
uploadContext: async () => ({
task: task(options.task),
@@ -110,6 +135,27 @@ function createHarnessWithCredential(
},
},
}),
acquireAttempt: async (
_brandId: string,
_actorId: string,
_taskId: string,
phase: "build" | "upload",
) => ({
id: `${phase}-attempt`,
leaseToken: `${phase}-lease`,
attempt: 1,
leaseExpiresAt: new Date(Date.now() + 60_000),
}),
heartbeatAttempt: async () => ({}),
finishAttempt: async (
_brandId: string,
attemptId: string,
_leaseToken: string,
input: object,
) => {
updates.push({ kind: "attempt", attemptId, input });
return input;
},
updateBuild: async (_brandId: string, _actorId: string, _taskId: string, input: object) => {
updates.push({ kind: "build", input });
return input;
@@ -122,14 +168,20 @@ function createHarnessWithCredential(
const builds = {
run: async (
_templateKey: string,
_config: { apiBaseUrl: string; appId: string },
config: { apiBaseUrl: string; appId: string; outputRoot: string },
onLog: (message: string, stream: "stdout" | "stderr") => void,
) => {
buildConfigs.push(config);
onLog("build log", "stdout");
return { exitCode: options.exitCode ?? 0 };
},
} as unknown as AdminMiniAppBuildService;
return { service: new AdminMiniAppUploadService(releases, builds), updates, artifactRoot };
return {
service: new AdminMiniAppUploadService(releases, builds),
updates,
artifactRoot,
buildConfigs,
};
}
test("upload blocks a currently building task before credentials are read", async () => {
@@ -159,17 +211,23 @@ test("failed build records logs and build failure state", async () => {
() => service.run(brandId, actorId, "task-1", {}),
(error: unknown) => {
assert.ok(error instanceof BadRequestException);
const logs = (updates.at(-1)?.input as { buildLogs: Array<{ message: string }> }).buildLogs;
const failedAttempt = updates.find(
(entry) => entry.kind === "attempt" && entry.attemptId === "build-attempt",
);
const logs = (failedAttempt?.input as { logs: Array<{ message: string }> }).logs;
assert.ok(logs.some((entry) => entry.message.includes("build log")));
assert.ok(logs.some((entry) => entry.message.includes("退出码 2")));
return true;
},
);
assert.equal((updates.at(-1)?.input as { buildStatus: string }).buildStatus, "failed");
assert.equal(
(updates.find((entry) => entry.kind === "attempt")?.input as { status: string }).status,
"failed",
);
});
test("missing artifact records build failure without invoking upload", async () => {
const { service, updates } = createHarnessWithCredential({ artifact: false });
const { service, updates, buildConfigs } = createHarnessWithCredential({ artifact: false });
const previousProjectPath = process.env.MINIAPP_ARTIFACT_ROOT;
process.env.MINIAPP_ARTIFACT_ROOT = join(tmpdir(), "missing-miniapp-artifacts");
try {
@@ -181,7 +239,21 @@ test("missing artifact records build failure without invoking upload", async ()
return true;
},
);
assert.equal((updates.at(-1)?.input as { buildStatus: string }).buildStatus, "failed");
assert.equal(
(updates.find((entry) => entry.kind === "attempt")?.input as { status: string }).status,
"failed",
);
assert.equal(
resolve(process.cwd(), "../taro", buildConfigs[0].outputRoot),
resolve(
process.env.MINIAPP_ARTIFACT_ROOT,
"releases",
brandId,
`acheng-campus-food@${templatePackageRegistry.find((item) => item.supportedIndustryTemplates.includes("food"))?.version}`,
"build-attempt",
"weapp",
),
);
} finally {
if (previousProjectPath === undefined) delete process.env.MINIAPP_ARTIFACT_ROOT;
else process.env.MINIAPP_ARTIFACT_ROOT = previousProjectPath;
+73
View File
@@ -10,6 +10,7 @@ import { getVisualTemplateManifest } from "@personal-brand/config";
import { createApp } from "../dist/main.js";
import { readIdentity, signIdentity } from "../dist/auth.js";
import { WechatAuthService } from "../dist/wechat-auth.js";
import { AdminMiniAppReleaseService } from "../dist/modules/admin/admin-miniapp-release.service.js";
let app;
const prisma = new PrismaClient();
@@ -571,6 +572,21 @@ test("miniapp release tasks are authorized, idempotent, and state checked", asyn
});
assert.equal(created.statusCode, 201);
assert.equal(JSON.parse(created.body).version, current.version);
await prisma.miniAppReleaseAttempt.create({
data: {
taskId: JSON.parse(created.body).id,
brandId,
actorId: "integration-test",
phase: "build",
attempt: 1,
leaseToken: "integration-secret-lease-token",
leaseExpiresAt: new Date(Date.now() + 60_000),
},
});
const listedAttempts = await request("/admin/miniapp-releases", { headers });
assert.equal(listedAttempts.statusCode, 200);
assert.equal(listedAttempts.body.includes("integration-secret-lease-token"), false);
assert.equal(JSON.parse(listedAttempts.body)[0].attempts[0].phase, "build");
const repeated = await request("/admin/miniapp-releases", {
method: "POST",
headers,
@@ -591,18 +607,21 @@ test("miniapp release tasks are authorized, idempotent, and state checked", asyn
});
assert.equal(uploaded.statusCode, 200);
assert.equal(JSON.parse(uploaded.body).status, "uploaded");
assert.equal(JSON.parse(uploaded.body).uploadStatus, "uploaded");
const underReview = await request(`/admin/miniapp-releases/${taskId}/status`, {
method: "PATCH",
headers,
payload: { status: "under_review" },
});
assert.equal(JSON.parse(underReview.body).status, "under_review");
assert.equal(JSON.parse(underReview.body).reviewStatus, "under_review");
const approved = await request(`/admin/miniapp-releases/${taskId}/status`, {
method: "PATCH",
headers,
payload: { status: "approved" },
});
assert.equal(JSON.parse(approved.body).status, "approved");
assert.equal(JSON.parse(approved.body).reviewStatus, "approved");
const published = await request(`/admin/miniapp-releases/${taskId}/status`, {
method: "PATCH",
headers,
@@ -628,6 +647,60 @@ test("miniapp release tasks are authorized, idempotent, and state checked", asyn
await prisma.miniAppReleaseTask.delete({ where: { id: buildingTask.id } });
});
test("API restart recovers an expired persisted miniapp build attempt", async () => {
const task = await prisma.miniAppReleaseTask.create({
data: {
brandId,
version: 900002,
actorId: "integration-restart-test",
status: "built",
buildStatus: "idle",
},
});
try {
const runningApi = app.get(AdminMiniAppReleaseService);
const first = await runningApi.acquireAttempt(
brandId,
"integration-restart-test",
task.id,
"build",
);
assert.equal(
(await prisma.miniAppReleaseAttempt.findUniqueOrThrow({ where: { id: first.id } })).status,
"running",
);
await app.close();
await prisma.miniAppReleaseAttempt.update({
where: { id: first.id },
data: { leaseExpiresAt: new Date(Date.now() - 1_000) },
});
app = await createApp();
await app.init();
const restartedApi = app.get(AdminMiniAppReleaseService);
const recovered = await restartedApi.acquireAttempt(
brandId,
"integration-restart-test",
task.id,
"build",
);
const attempts = await prisma.miniAppReleaseAttempt.findMany({
where: { taskId: task.id, phase: "build" },
orderBy: { attempt: "asc" },
});
assert.equal(recovered.attempt, 2);
assert.equal(attempts[0].status, "timed_out");
assert.equal(attempts[1].status, "running");
assert.equal(
(await prisma.miniAppReleaseTask.findUniqueOrThrow({ where: { id: task.id } })).buildStatus,
"building",
);
} finally {
await prisma.miniAppReleaseTask.delete({ where: { id: task.id } });
}
});
test("miniapp release settings never expose or persist plaintext private keys", async () => {
const login = await request("/admin/session/login", {
method: "POST",
+21
View File
@@ -5,6 +5,8 @@ import { test } from "node:test";
import {
miniappArtifactRoot,
miniappProjectPath,
miniappReleaseProjectPath,
miniappReleaseOutputRoot,
REPOSITORY_ROOT,
} from "../src/modules/admin/miniapp-artifact.js";
@@ -40,3 +42,22 @@ test("显式配置的产物根目录按仓库根目录解析相对路径", () =>
);
assert.equal(miniappArtifactRoot("/srv/miniapp"), resolve("/srv/miniapp"));
});
test("发布产物路径隔离品牌、模板版本和 attempt", () => {
const root = miniappReleaseOutputRoot("brand-a", "linjing-editorial", "1.0.4", "attempt-1");
assert.equal(
root,
resolve(REPOSITORY_ROOT, "apps/taro/dist/releases/brand-a/linjing-editorial@1.0.4/attempt-1"),
);
assert.equal(
miniappReleaseProjectPath("brand-a", "linjing-editorial", "1.0.4", "attempt-1"),
`${root}/weapp`,
);
});
test("发布产物路径拒绝可能越界的品牌或模板字段", () => {
assert.throws(
() => miniappReleaseOutputRoot("../brand", "food", "1.0.0", "attempt-1"),
/Invalid brand slug/,
);
});
+14 -4
View File
@@ -1,6 +1,10 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { buildingStaleMs, isStaleBuilding } from "../src/modules/admin/miniapp-build-status.js";
import {
buildingStaleMs,
isStaleBuilding,
miniappBuildTimeoutMs,
} from "../src/modules/admin/miniapp-build-status.js";
const now = Date.UTC(2026, 9, 13, 12, 0, 0);
@@ -10,7 +14,8 @@ function task(buildStatus: string, minutesAgo: number) {
test("构建中的任务在超过阈值未更新时视为已中断", () => {
assert.equal(isStaleBuilding(task("building", 1), now), false);
assert.equal(isStaleBuilding(task("building", 16), now), true);
assert.equal(isStaleBuilding(task("building", 16), now), false);
assert.equal(isStaleBuilding(task("building", 31), now), true);
assert.equal(isStaleBuilding(task("building", 24 * 60), now), true);
});
@@ -24,9 +29,14 @@ test("时间戳异常时按未中断处理,宁可挡住重试也不允许并
assert.equal(isStaleBuilding({ buildStatus: "building", updatedAt: "not-a-date" }, now), false);
});
test("阈值可通过环境变量覆盖,非法值回退到默认 15 分钟", () => {
test("阈值可通过环境变量覆盖,非法值回退到默认 30 分钟", () => {
assert.equal(buildingStaleMs("60000"), 60_000);
for (const invalid of [undefined, "0", "-1", "abc"]) {
assert.equal(buildingStaleMs(invalid), 15 * 60 * 1000);
assert.equal(buildingStaleMs(invalid), 30 * 60 * 1000);
}
});
test("构建超时阈值支持环境变量并有独立默认值", () => {
assert.equal(miniappBuildTimeoutMs("60000"), 60_000);
assert.equal(miniappBuildTimeoutMs("0"), 20 * 60 * 1000);
});
@@ -0,0 +1,239 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { ConflictException } from "@nestjs/common";
import { AdminMiniAppReleaseService } from "../src/modules/admin/admin-miniapp-release.service.js";
import type { PrismaService } from "../src/prisma.service.js";
const brandId = "brand-1";
const taskId = "task-1";
const actorId = "admin-1";
function createHarness(initialAttempts: Array<Record<string, unknown>> = []) {
const task = {
id: taskId,
brandId,
status: "built",
buildStatus: "idle",
uploadStatus: "idle",
reviewStatus: "not_submitted",
};
const attempts = [...initialAttempts];
const findAttempt = (where: Record<string, unknown>) =>
attempts
.filter((item) => Object.entries(where).every(([key, value]) => item[key] === value))
.sort((left, right) => Number(right.attempt) - Number(left.attempt))[0] ?? null;
let sequence = attempts.length + 1;
const tx = {
miniAppReleaseTask: {
findFirst: async () => task,
update: async ({ data }: { data: Record<string, unknown> }) => {
Object.assign(task, data);
return task;
},
},
miniAppReleaseAttempt: {
findFirst: async ({ where }: { where: Record<string, unknown> }) => findAttempt(where),
create: async ({ data }: { data: Record<string, unknown> }) => {
const created = { id: `attempt-${sequence++}`, status: "running", ...data };
attempts.push(created);
return created;
},
update: async ({ where, data }: { where: { id: string }; data: Record<string, unknown> }) => {
const item = attempts.find((candidate) => candidate.id === where.id);
if (!item) throw new Error("attempt missing");
Object.assign(item, data);
return item;
},
updateMany: async ({
where,
data,
}: {
where: {
id: string;
status: string;
leaseToken: string;
leaseExpiresAt?: { gt: Date };
};
data: Record<string, unknown>;
}) => {
const item = attempts.find(
(candidate) =>
candidate.id === where.id &&
candidate.status === where.status &&
candidate.leaseToken === where.leaseToken &&
(!where.leaseExpiresAt || (candidate.leaseExpiresAt as Date) > where.leaseExpiresAt.gt),
);
if (!item) return { count: 0 };
Object.assign(item, data);
return { count: 1 };
},
},
};
const prisma = {
$transaction: async (callback: (value: typeof tx) => unknown) => callback(tx),
miniAppReleaseAttempt: tx.miniAppReleaseAttempt,
miniAppReleaseTask: tx.miniAppReleaseTask,
} as unknown as PrismaService;
return { service: new AdminMiniAppReleaseService(prisma), attempts, task };
}
test("同一任务同一阶段的有效 lease 拒绝并发操作", async () => {
const { service } = createHarness();
const first = await service.acquireAttempt(brandId, actorId, taskId, "build");
assert.equal(first.attempt, 1);
await assert.rejects(
() => service.acquireAttempt(brandId, actorId, taskId, "build"),
ConflictException,
);
});
test("过期 lease 会先收敛为 timed_out,再允许新的 attempt 接管", async () => {
const expiredAt = new Date(Date.now() - 1_000);
const { service, attempts } = createHarness([
{
id: "expired-attempt",
taskId,
brandId,
phase: "upload",
attempt: 1,
status: "running",
leaseToken: "expired-token",
leaseExpiresAt: expiredAt,
},
]);
const next = await service.acquireAttempt(brandId, actorId, taskId, "upload");
assert.equal(next.attempt, 2);
assert.equal(attempts[0].status, "timed_out");
assert.equal(attempts[0].finishedAt instanceof Date, true);
});
test("服务进程重建后从持久化记录恢复,并接管已过期的 build attempt", async () => {
const expiredAt = new Date(Date.now() - 1_000);
const { attempts } = createHarness([
{
id: "worker-before-restart",
taskId,
brandId,
phase: "build",
attempt: 1,
status: "running",
leaseToken: "old-worker-token",
leaseExpiresAt: expiredAt,
},
]);
const persistentTask = {
id: taskId,
brandId,
status: "built",
buildStatus: "building",
uploadStatus: "idle",
reviewStatus: "not_submitted",
};
const persistentTx = {
miniAppReleaseTask: {
findFirst: async () => persistentTask,
update: async ({ data }: { data: Record<string, unknown> }) => {
Object.assign(persistentTask, data);
return persistentTask;
},
},
miniAppReleaseAttempt: {
findFirst: async ({ where }: { where: Record<string, unknown> }) =>
attempts
.filter((item) => Object.entries(where).every(([key, value]) => item[key] === value))
.sort((left, right) => Number(right.attempt) - Number(left.attempt))[0] ?? null,
create: async ({ data }: { data: Record<string, unknown> }) => {
const created = {
id: "worker-after-restart",
status: "running",
...data,
};
attempts.push(created);
return created;
},
update: async ({ where, data }: { where: { id: string }; data: Record<string, unknown> }) => {
const item = attempts.find((candidate) => candidate.id === where.id);
if (!item) throw new Error("attempt missing");
Object.assign(item, data);
return item;
},
},
};
const prisma = {
$transaction: async (callback: (value: typeof persistentTx) => unknown) =>
callback(persistentTx),
...persistentTx,
} as unknown as PrismaService;
const restartedService = new AdminMiniAppReleaseService(prisma);
const recovered = await restartedService.acquireAttempt(brandId, actorId, taskId, "build");
assert.equal(recovered.attempt, 2);
assert.equal(attempts[0].status, "timed_out");
assert.equal(persistentTask.buildStatus, "building");
});
test("heartbeat 遇到过期上传 lease 时将任务阶段收敛为 timed_out", async () => {
const expiredAt = new Date(Date.now() - 1_000);
const { service, attempts, task } = createHarness([
{
id: "expired-attempt",
taskId,
brandId,
phase: "upload",
attempt: 1,
status: "running",
leaseToken: "expired-token",
leaseExpiresAt: expiredAt,
},
]);
task.uploadStatus = "uploading";
await assert.rejects(
() => service.heartbeatAttempt(brandId, "expired-attempt", "expired-token", {}),
ConflictException,
);
assert.equal(attempts[0].status, "timed_out");
assert.equal(task.uploadStatus, "timed_out");
});
test("错误 lease 不能结束操作,正确 lease 结束上传后更新阶段状态", async () => {
const { service, task, attempts } = createHarness();
const attempt = await service.acquireAttempt(brandId, actorId, taskId, "upload");
await assert.rejects(
() => service.finishAttempt(brandId, attempt.id, "wrong-token", { status: "succeeded" }),
ConflictException,
);
await service.finishAttempt(brandId, attempt.id, attempt.leaseToken, {
status: "succeeded",
externalId: "wechat-upload-1",
});
assert.equal(attempts[0].status, "succeeded");
assert.equal(task.uploadStatus, "uploaded");
assert.equal(task.status, "uploaded");
});
test("heartbeat 续租并更新日志,结束后的 attempt 拒绝迟到 heartbeat", async () => {
const { service, attempts } = createHarness();
const attempt = await service.acquireAttempt(brandId, actorId, taskId, "build");
await service.heartbeatAttempt(brandId, attempt.id, attempt.leaseToken, {
logs: [{ message: "compile" }],
});
assert.deepEqual(attempts[0].logs, [{ message: "compile" }]);
await service.finishAttempt(brandId, attempt.id, attempt.leaseToken, { status: "succeeded" });
await assert.rejects(
() => service.heartbeatAttempt(brandId, attempt.id, attempt.leaseToken, { logs: [] }),
ConflictException,
);
assert.equal(attempts[0].status, "succeeded");
});
test("执行结束时 lease 已过期会持久化 timed_out,而不是回滚为 running", async () => {
const { service, attempts } = createHarness();
const attempt = await service.acquireAttempt(brandId, actorId, taskId, "upload");
attempts[0].leaseExpiresAt = new Date(Date.now() - 1);
await assert.rejects(
() => service.finishAttempt(brandId, attempt.id, attempt.leaseToken, { status: "succeeded" }),
ConflictException,
);
assert.equal(attempts[0].status, "timed_out");
});