Files
geo/apps/desktop-client/src/main/lease-manager.ts
T
root a617d39a4a feat(desktop): drop parked review flow, add publish management
SaaS 侧人工审核后才创建 publish job,desktop 不再做二次审核:移除
manual/waiting_user/parked/from_parked 状态机与 LeaseFromParked 查询,
desktop client 只执行发布并新增"发布管理"页(待发布队列 / 历史 / 再次
发送)。同时抽离 @geo/publisher-platforms 共享适配器包、新增 Redis-based
desktop_presence 与 publish_record_support,刷新 admin-web 发布弹窗与
媒体库;plan A / spec 文档同步口径。
2026-04-20 09:52:48 +08:00

91 lines
2.6 KiB
TypeScript

interface LeaseSnapshot {
activeLeaseId: string | null;
activeTaskId: string | null;
attemptId: string | null;
startedAt: number | null;
leaseExpiresAt: number | null;
lastExtendedAt: number | null;
lastReleasedAt: number | null;
lastOutcome: "leased" | "extended" | "completed" | "failed" | "cleared" | null;
}
interface ActiveLeaseState {
taskId: string;
attemptId: string | null;
leaseExpiresAt: number | null;
}
const leaseState: LeaseSnapshot = {
activeLeaseId: null,
activeTaskId: null,
attemptId: null,
startedAt: null,
leaseExpiresAt: null,
lastExtendedAt: null,
lastReleasedAt: null,
lastOutcome: null,
};
function normalizeLeaseExpiresAt(value: number | string | null | undefined): number | null {
if (typeof value === "number") {
return Number.isFinite(value) ? value : null;
}
if (typeof value !== "string" || !value.trim()) {
return null;
}
const timestamp = Date.parse(value);
return Number.isNaN(timestamp) ? null : timestamp;
}
export function setActiveLease(input: {
taskId: string;
attemptId?: string | null;
leaseExpiresAt?: number | string | null;
} | null): void {
if (!input) {
leaseState.activeLeaseId = null;
leaseState.activeTaskId = null;
leaseState.attemptId = null;
leaseState.startedAt = null;
leaseState.leaseExpiresAt = null;
leaseState.lastOutcome = "cleared";
leaseState.lastReleasedAt = Date.now();
return;
}
const normalized: ActiveLeaseState = {
taskId: input.taskId,
attemptId: input.attemptId ?? null,
leaseExpiresAt: normalizeLeaseExpiresAt(input.leaseExpiresAt),
};
leaseState.activeLeaseId = normalized.taskId;
leaseState.activeTaskId = normalized.taskId;
leaseState.attemptId = normalized.attemptId;
leaseState.startedAt = Date.now();
leaseState.leaseExpiresAt = normalized.leaseExpiresAt;
leaseState.lastOutcome = "leased";
}
export function noteLeaseExtended(leaseExpiresAt?: number | string | null): void {
leaseState.lastExtendedAt = Date.now();
leaseState.leaseExpiresAt = normalizeLeaseExpiresAt(leaseExpiresAt) ?? leaseState.leaseExpiresAt;
leaseState.lastOutcome = "extended";
}
export function noteLeaseReleased(reason: Exclude<LeaseSnapshot["lastOutcome"], "leased" | "extended" | null>): void {
leaseState.activeLeaseId = null;
leaseState.activeTaskId = null;
leaseState.attemptId = null;
leaseState.startedAt = null;
leaseState.leaseExpiresAt = null;
leaseState.lastReleasedAt = Date.now();
leaseState.lastOutcome = reason;
}
export function getLeaseManagerSnapshot(): LeaseSnapshot {
return { ...leaseState };
}