| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- import type {
- ZCodeAutomationRunOutcome,
- ZCodeAutomationTrigger,
- } from "@zcode/shared";
- export interface CronRunLifecycleRepo {
- ensureRunClaimed(params: {
- runId: string;
- automationId: string;
- workspaceKey: string;
- scheduledAt: number | null;
- trigger: ZCodeAutomationTrigger;
- }): Promise<void>;
- markRunOutcome(
- runId: string,
- outcome: ZCodeAutomationRunOutcome,
- error?: string,
- ): Promise<void>;
- markRunDispatch(params: {
- runId: string;
- dispatchStatus: "failed_to_dispatch";
- error: string;
- }): Promise<void>;
- touchManualClaim(automationId: string, workspaceKey: string): Promise<void>;
- releaseManualClaim(automationId: string, workspaceKey: string): Promise<void>;
- }
- export interface CronRunLifecycleIdentity {
- runId: string;
- automationId: string;
- workspaceKey: string;
- scheduledAt: number | null;
- trigger: ZCodeAutomationTrigger;
- }
- type LogWarn = (message: string, error: unknown) => void;
- const MANUAL_CLAIM_HEARTBEAT_MS = 60_000;
- export function startManualClaimHeartbeat(
- params: Pick<CronRunLifecycleIdentity, "automationId" | "runId" | "workspaceKey"> & {
- repo: Pick<CronRunLifecycleRepo, "touchManualClaim">;
- logWarn: LogWarn;
- intervalMs?: number;
- },
- ): { dispose(): void } {
- const timer = setInterval(() => {
- void params.repo
- .touchManualClaim(params.automationId, params.workspaceKey)
- .catch((error) =>
- params.logWarn(
- `续租 manual automation claim 失败 automation=${params.automationId} runId=${params.runId}`,
- error,
- ),
- );
- }, params.intervalMs ?? MANUAL_CLAIM_HEARTBEAT_MS);
- return { dispose: () => clearInterval(timer) };
- }
- export async function recordCronRunOutcomeBestEffort(
- params: CronRunLifecycleIdentity & {
- repo: CronRunLifecycleRepo;
- outcome: ZCodeAutomationRunOutcome;
- error?: string;
- logWarn: LogWarn;
- },
- ): Promise<void> {
- try {
- await params.repo.ensureRunClaimed(params);
- await params.repo.markRunOutcome(params.runId, params.outcome, params.error);
- } catch (error) {
- params.logWarn(
- `回写定时任务运行结果失败 automation=${params.automationId} runId=${params.runId}`,
- error,
- );
- }
- }
- export async function releaseManualClaimBestEffort(
- params: Pick<CronRunLifecycleIdentity, "automationId" | "runId" | "workspaceKey"> & {
- repo: Pick<CronRunLifecycleRepo, "releaseManualClaim">;
- logWarn: LogWarn;
- },
- ): Promise<void> {
- try {
- await params.repo.releaseManualClaim(params.automationId, params.workspaceKey);
- } catch (error) {
- params.logWarn(
- `释放 manual automation claim 失败 automation=${params.automationId} runId=${params.runId}`,
- error,
- );
- }
- }
- /** 派发失败清理永不覆盖调用方持有的原始 dispatch error。 */
- export async function settleManualDispatchFailureBestEffort(
- params: CronRunLifecycleIdentity & {
- repo: CronRunLifecycleRepo;
- dispatchError: unknown;
- logWarn: LogWarn;
- },
- ): Promise<void> {
- const errorMessage =
- params.dispatchError instanceof Error
- ? params.dispatchError.message
- : String(params.dispatchError);
- try {
- await params.repo.markRunDispatch({
- runId: params.runId,
- dispatchStatus: "failed_to_dispatch",
- error: errorMessage,
- });
- } catch (error) {
- params.logWarn(
- `回写 manual automation 派发失败状态失败 automation=${params.automationId} runId=${params.runId}`,
- error,
- );
- }
- await releaseManualClaimBestEffort(params);
- }
- /** manual claim 覆盖 queue 等待和 turn 执行,只能在真实终态后释放。 */
- export async function settleCronRunTerminalOutcome(
- params: CronRunLifecycleIdentity & {
- repo: CronRunLifecycleRepo;
- outcome: Exclude<ZCodeAutomationRunOutcome, "running">;
- error?: string;
- logWarn: LogWarn;
- },
- ): Promise<void> {
- await recordCronRunOutcomeBestEffort(params);
- if (params.trigger !== "manual") return;
- await releaseManualClaimBestEffort(params);
- }
|