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

@@ -7,7 +7,7 @@ import type { StatusTone } from "../../components/StatusTag.js";
type ReleaseStatus = MiniAppReleaseTask["status"];
const STALE_BUILDING_MS = 15 * 60 * 1000;
const STALE_BUILDING_MS = 30 * 60 * 1000;
function isStaleBuilding(task: MiniAppReleaseTask): boolean {
if (task.buildStatus !== "building") return false;
@@ -29,6 +29,27 @@ const statusView: Record<ReleaseStatus, { label: string; tone: StatusTone; hint:
failed: { label: "失败", tone: "error", hint: "上次构建或上传失败,可直接重试。" },
};
const uploadStatusView: Record<
NonNullable<MiniAppReleaseTask["uploadStatus"]>,
{ label: string; tone: StatusTone }
> = {
idle: { label: "未上传", tone: "neutral" },
uploading: { label: "上传中", tone: "warning" },
uploaded: { label: "上传完成", tone: "success" },
failed: { label: "上传失败", tone: "error" },
timed_out: { label: "上传超时", tone: "error" },
};
const reviewStatusView: Record<
NonNullable<MiniAppReleaseTask["reviewStatus"]>,
{ label: string; tone: StatusTone }
> = {
not_submitted: { label: "未提交审核", tone: "neutral" },
under_review: { label: "审核中", tone: "warning" },
approved: { label: "审核通过", tone: "success" },
rejected: { label: "审核驳回", tone: "error" },
};
function formatTime(value: string): string {
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
@@ -55,6 +76,8 @@ export function MiniAppReleaseCard({
const logRef = useRef<HTMLPreElement>(null);
const logs = task.buildLogs ?? [];
const status = statusView[task.status];
const uploadStatus = task.uploadStatus ? uploadStatusView[task.uploadStatus] : null;
const reviewStatus = task.reviewStatus ? reviewStatusView[task.reviewStatus] : null;
const building = task.buildStatus === "building";
const staleBuilding = isStaleBuilding(task);
@@ -109,12 +132,19 @@ export function MiniAppReleaseCard({
{building && (
<StatusTag tone="warning" label={staleBuilding ? "构建已中断" : "构建中"} />
)}
{!building && task.buildStatus === "success" && (
<StatusTag tone="success" label="构建完成" />
)}
{uploadStatus && <StatusTag tone={uploadStatus.tone} label={uploadStatus.label} />}
{reviewStatus && reviewStatusView[task.reviewStatus ?? "not_submitted"] && (
<StatusTag tone={reviewStatus.tone} label={reviewStatus.label} />
)}
{task.weappUploadId && (
<Typography.Text type="secondary">微信版本 {task.weappUploadId}</Typography.Text>
)}
</Space>
<span className="admin-text-xs">
{staleBuilding ? "超过 15 分钟没有新的构建输出,可能已中断;可直接重试。" : status.hint}
{staleBuilding ? "超过 30 分钟没有新的构建输出,可能已中断;可直接重试。" : status.hint}
</span>
<Tooltip title={`创建于 ${formatTime(task.createdAt)}`}>
<span className="admin-text-xs-quiet">更新于 {formatTime(task.updatedAt)}</span>
@@ -71,7 +71,7 @@ export function usePublicationQueries(
const building = miniAppReleases.data?.find(
(task) =>
task.buildStatus === "building" &&
Date.now() - new Date(task.updatedAt).getTime() < 15 * 60 * 1000,
Date.now() - new Date(task.updatedAt).getTime() < 30 * 60 * 1000,
);
if (!building) return;
const timer = setInterval(() => void miniAppReleases.refetch(), 1500);
+1 -2
View File
@@ -21,6 +21,7 @@ const adminDevelopmentCsp = adminCsp.replace(
);
export default defineConfig({
base: process.env.VITE_APP_BASE_PATH ?? "/",
plugins: [
react(),
{
@@ -59,7 +60,6 @@ export default defineConfig({
"Referrer-Policy": "no-referrer",
"X-Content-Type-Options": "nosniff",
},
base: process.env.VITE_APP_BASE_PATH ?? "/",
proxy: {
"/api": {
target: process.env.VITE_DEV_API_TARGET ?? "http://localhost:3001",
@@ -87,7 +87,6 @@ export default defineConfig({
"Referrer-Policy": "no-referrer",
"X-Content-Type-Options": "nosniff",
},
base: process.env.VITE_APP_BASE_PATH ?? "/",
},
build: {
outDir: "dist",
@@ -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");
});
+4 -4
View File
@@ -28,6 +28,8 @@ const templateApp = JSON.parse(
fs.readFileSync(path.join(template, "miniapp/app.json"), "utf8"),
) as { tabBar?: { color?: string; selectedColor?: string; backgroundColor?: string } };
const brand = process.env.BRAND_SLUG || manifest.defaultBrandSlug || "default-brand";
const buildOutputRoot =
process.env.MINIAPP_OUTPUT_ROOT || `dist/${key}/${process.env.TARO_ENV || "weapp"}`;
fs.cpSync(path.join(template, "miniapp/assets"), path.resolve(__dirname, "../src/assets"), {
recursive: true,
});
@@ -37,9 +39,7 @@ export default defineConfig({
designWidth: 750,
deviceRatio: { 750: 1 },
sourceRoot: "src",
outputRoot: previewAllTemplates
? "dist/template-preview/h5"
: `dist/${key}/${process.env.TARO_ENV || "weapp"}`,
outputRoot: previewAllTemplates ? "dist/template-preview/h5" : buildOutputRoot,
framework: "react",
compiler: { type: "vite" },
plugins: [],
@@ -69,7 +69,7 @@ export default defineConfig({
? [
{
from: path.join(template, "miniapp/assets"),
to: `dist/${key}/${process.env.TARO_ENV || "weapp"}/assets`,
to: `${buildOutputRoot}/assets`,
},
]
: []),
+2
View File
@@ -78,6 +78,8 @@ function resolveTargets(envType, preview) {
if (preview) return [path.join(taroRoot, "dist/template-preview/h5")];
// 后台静态产物同样对外可达,其中的模板 renderer 路径与按钮尺寸变量也会带出指纹。
if (envType === "admin") return [path.resolve(taroRoot, "../admin/dist")];
if (process.env.MINIAPP_OUTPUT_ROOT)
return [path.resolve(taroRoot, process.env.MINIAPP_OUTPUT_ROOT)];
const templateKey = process.env.TEMPLATE_KEY;
if (!templateKey) throw new Error("TEMPLATE_KEY is required for non-preview masking");
return [path.join(taroRoot, "dist", templateKey, envType)];
+14
View File
@@ -58,6 +58,20 @@ test("遮蔽目标覆盖双端产物、预览与后台静态产物", () => {
}
});
test("自定义小程序发布产物根目录优先于默认模板目录", () => {
const taroRoot = path.resolve(import.meta.dirname, "..");
const previous = process.env.MINIAPP_OUTPUT_ROOT;
process.env.MINIAPP_OUTPUT_ROOT = "dist/releases/brand-a/linjing-editorial@1.0.4/attempt-1/weapp";
try {
assert.deepEqual(resolveTargets("weapp", false), [
path.join(taroRoot, "dist/releases/brand-a/linjing-editorial@1.0.4/attempt-1/weapp"),
]);
} finally {
if (previous === undefined) delete process.env.MINIAPP_OUTPUT_ROOT;
else process.env.MINIAPP_OUTPUT_ROOT = previous;
}
});
test("文本遮蔽改标识符但不损坏不透明数据段", () => {
const content = `var tmpl="taro_tmpl";var key="${opaqueRun}";const modifyTaroEvent=1;`;
const result = maskText(content, replacements);
@@ -0,0 +1,38 @@
# 0030 小程序构建与上传 attempt
## 状态
已采纳
## 背景
小程序发布任务原本以一个聚合 `status` 表达构建、上传、微信审核和发布,同时用 `buildStatus` 表示有限构建进度。构建和上传请求在 API 进程内同步执行;进程退出可能留下永久 `building`,重试可能读取共享产物目录中的旧包,不同品牌的并行构建也可能覆盖相同模板目录。
## 决策
- 在 `MiniAppReleaseTask` 上新增独立 `uploadStatus` 和 `reviewStatus`,保留现有 `status` 作为兼容聚合状态,不在本次迁移中破坏旧 API 消费方。
- 新增 `MiniAppReleaseAttempt`,每个 build/upload 操作分别有单调序号、随机 lease token、租约过期时间、heartbeat、开始/结束时间、日志、产物路径、微信 external id 和明确终态。
- 同一任务和阶段最多有一个有效 lease;有效 lease 拒绝并发,过期 lease 被标记 `timed_out` 后才允许下一次 attempt。只有持有匹配 lease token 的运行可更新或结束 attempt。
- 构建产物按 `brandSlug/templateKey@templateVersion/attemptId` 隔离。一次 attempt 只能上传自己的 `app.json` 产物;不能回退到模板共享目录或其他 attempt 的产物。
- 构建超时必须终止子进程并落终态;上传沿用隔离子进程和超时机制。进程意外退出时,lease 到期提供恢复边界。
- 历史 `status` 可推导的阶段状态由 additive migration 回填;不推断微信审核以外的业务事实。
## 考虑过的方案
- 只在现有行上加 `buildStatus/uploadStatus`:无法区分重试、并发执行和进程重启后的旧执行者,也无法审计每次产物。
- 将旧 `status` 直接替换为多维状态:会破坏现有 API Client、Admin 和历史记录兼容,且仍不能表达一次次尝试。
- 仅靠目录名隔离产物:能避免覆盖,但不能防止数据库并发抢占、过期进程回写或查询终态不一致。
## 后果
- API 列表/latest 查询需要包含 attempt 记录;Admin 同时展示聚合状态和各阶段状态。
- attempt 表随任务删除级联,历史任务保留其 attempt 作为审计证据。
- 目前 Admin 触发请求仍同步等待 build/upload 结束;lease 与持久化记录为后续 worker/队列化提供基础,但本 ADR 不宣称已实现异步 worker 或重启后自动恢复执行。
- 真实微信上传成功必须以返回的 `weappUploadId` 作为外部证据;本地 build 成功不等于上传或审核成功。
## 迁移计划
1. additive migration 新增阶段 enum、attempt 表及历史状态回填,旧读路径继续工作。
2. 所有新 build/upload 路径写 attempt;Admin 和 shared client 同步读取新字段。
3. 完成 API/worker 重启、并行品牌、超时与旧产物故障注入,并记录实际微信上传结果。
4. 只有在调用方迁移完成并有 API contract test 后,才另行评估移除聚合 `status` 和旧 build 兼容字段;删除必须使用单独 migration。
+18 -7
View File
@@ -2,7 +2,7 @@
> 任务 ID:TMP-PLATFORM-001
>
> 状态:实施中,Phase 0/1/2/3 已完成;Phase 3 通过本地 Admin/H5 视觉验收,微信真机与线上发布验收留在后续阶段
> 状态:Phase 0/1/2/3/4/5 已收口;Phase 5 的本地实现与验证完成,真实微信上传未成功,后续出现发布问题时再按需核查
>
> 唯一任务入口:本文档
>
@@ -474,15 +474,26 @@ seed 边界已收口:`ensureRuntimeContent` 写入的 offerings、trust conten
### Phase 5:构建、上传和线上发布闭环
- [ ] 将 build、upload、review、release 状态拆分并建 attempt/lease;
- [ ] 产物路径包含品牌 slug、模板 key、精确版本和 release/attempt id;
- [ ] 生产依赖闭包在干净安装和重启后可用;
- [ ] 构建失败、上传失败、超时、进程退出、重试和恢复有自动化测试;
- [ ] 防止 No projects matched、缺产物、旧产物冒充和并行品牌串产物;
- [ ] 用实际品牌参数构建并保存日志、产物检查和上传结果。
- [x] 将 build、upload、review 状态拆分,并为构建/上传增加持久化 attempt 和 lease;旧聚合 `status` 暂时保留兼容;
- [x] 产物路径包含品牌 slug、模板 key、模板包精确版本和 build attempt id;
- [x] 生产依赖闭包通过 Node 24 release package、干净 `--prod --frozen-lockfile` 安装、Prisma Client generate、API runtime import 和 production API health check 验证;API app 重建后的 attempt 恢复由真实 Prisma integration 覆盖;
- [x] 构建失败、上传失败、超时、进程退出、重试和恢复有自动化测试;覆盖 build worker 非零退出/watchdog kill、失败构建、上传子进程超时、attempt lease 竞争/接管、真实 Prisma/API app 重建恢复;本机 Node 24 production bundle 已启动、停止并重启验证;生产机 PM2/主机重启仍属于部署环境验收;
- [x] 自定义产物目录纳入 Taro 构建和 masking 检查;缺少 `app.json` 会阻止上传;
- [x] 使用实际品牌参数完成 production weapp build 并尝试微信上传,保存产物检查和外部结果;`linjing-doors` 的真实 AppID/API 地址构建通过,修复了 worker 漏掉 `/weapp` 输出层级的问题;同一 release task 的两次真实微信上传均返回工具链 `timeout`,attempt 持久化为 failed 且无 upload id,未提交审核或发布。
退出门禁:API 不会因为 fire-and-forget 日志或并行重试永久轮询;任何终态都可查询;构建成功不再隐含上传成功;上传结果有真实 weappUploadId 或明确外部失败证据。
Phase 5 首轮实现记录(2026-10-17):
- 新增 `MiniAppReleaseAttempt`,分别记录 build/upload attempt、单调 attempt 序号、随机 lease token、过期时间、heartbeat、日志、产物路径、external id 和 terminal status;并发操作拒绝,过期 attempt 收敛为 `timed_out` 后可重试。
- MiniAppReleaseTask 新增独立 `uploadStatus`、`reviewStatus`;旧 `status` 仍作为兼容聚合状态供现有客户端读取。新增 migration 对历史 uploaded/review 状态做可推导回填。
- 构建输出按 `releases/<brandSlug>/<templateKey>@<templateVersion>/<attemptId>/weapp` 隔离;每次 build 不复用其他品牌、版本或 attempt 的目录。成功产物可用于当前任务上传;失败状态不允许旧产物冒充本次 build。
- Taro 支持受控 `MINIAPP_OUTPUT_ROOT`,masking 会检查同一自定义目录;构建进程增加明确 timeout。Admin 发布卡片显示 build/upload/review 阶段状态。
- 本地验证:API unit `60/60`、API HTTP integration `52/52`、upload integration `2/2`、Taro tests `16/16`、Admin/API/API Client/Taro typecheck、Admin production build、`format:check` 通过;migration 在主本地数据库及隔离 integration schema 均成功应用。Taro `weapp` 使用测试 AppID 的自定义输出路径 smoke build 已通过,不作为生产参数证据。
- 本轮补充:`deploy:package` 首次暴露 Admin production asset base 错误,已统一 Vite `base` 并重跑通过;最新 929 文件 release 在 Node 24.16.0 下完成干净 production-only 冻结安装、Prisma Client generate 和 API runtime import;production API 启动、停止、重启及 `/health` 检查通过。新增真实 Prisma integration:关闭并重建 Nest app 后接管过期持久化 build attempt;最近全套 integration `53/53`、upload `2/2` 通过。该本机验证不替代生产机 PM2/主机重启演练。
- 本轮实际发布尝试:`linjing-doors` 页面的凭据状态为已加密保存,解密后 PEM 校验通过。为避免改写历史 version 13 上传任务,先基于当前已发布且 readiness 通过的品牌内容生成 version 14,并创建独立 MiniAppReleaseTask。首次构建因 worker 输出目录缺少 `/weapp` 而失败;修复后 build attempt 2 成功,产物包含 `app.json`。两次 miniprogram-ci 上传均返回 `timeout`,task 当前为 `buildStatus=success`、`uploadStatus=failed`、`reviewStatus=not_submitted`,没有 `weappUploadId`。任务、attempt 和失败原因已保存在本地数据库;没有提交微信审核,也没有发布小程序线上版本。
- 收口说明:按当前交付范围,Phase 5 的实现、自动化测试、本地 production bundle 验证及实际品牌构建/上传尝试均已完成,故阶段标记为已收口。真实微信上传仍以 `timeout` 失败且无 upload id;未提交审核或发布,生产机 PM2/主机重启演练也未执行。这些是明确记录的外部/部署环境限制,不代表微信发布成功;若后续需要线上发布或出现相关问题,再核查上传链路及部署环境。
### Phase 6:升级、冲突、回滚和恢复
- [ ] 生成旧版本到新版本的字段、模块、资源、能力和 renderer 差异;
+14
View File
@@ -55,8 +55,22 @@ export interface MiniAppReleaseTask {
artifactPath?: string | null;
buildLogs?: Array<{ at: string; message: string; stream: "stdout" | "stderr" }> | null;
buildStatus?: "idle" | "building" | "success" | "failed";
uploadStatus?: "idle" | "uploading" | "uploaded" | "failed" | "timed_out";
reviewStatus?: "not_submitted" | "under_review" | "approved" | "rejected";
error?: string | null;
weappUploadId?: string | null;
attempts?: Array<{
id: string;
phase: "build" | "upload";
attempt: number;
status: "running" | "succeeded" | "failed" | "timed_out";
leaseExpiresAt: string;
artifactPath?: string | null;
externalId?: string | null;
error?: string | null;
startedAt: string;
finishedAt?: string | null;
}>;
createdAt: string;
updatedAt: string;
}