cronRunLifecycle.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import type {
  2. ZCodeAutomationRunOutcome,
  3. ZCodeAutomationTrigger,
  4. } from "@zcode/shared";
  5. export interface CronRunLifecycleRepo {
  6. ensureRunClaimed(params: {
  7. runId: string;
  8. automationId: string;
  9. workspaceKey: string;
  10. scheduledAt: number | null;
  11. trigger: ZCodeAutomationTrigger;
  12. }): Promise<void>;
  13. markRunOutcome(
  14. runId: string,
  15. outcome: ZCodeAutomationRunOutcome,
  16. error?: string,
  17. ): Promise<void>;
  18. markRunDispatch(params: {
  19. runId: string;
  20. dispatchStatus: "failed_to_dispatch";
  21. error: string;
  22. }): Promise<void>;
  23. touchManualClaim(automationId: string, workspaceKey: string): Promise<void>;
  24. releaseManualClaim(automationId: string, workspaceKey: string): Promise<void>;
  25. }
  26. export interface CronRunLifecycleIdentity {
  27. runId: string;
  28. automationId: string;
  29. workspaceKey: string;
  30. scheduledAt: number | null;
  31. trigger: ZCodeAutomationTrigger;
  32. }
  33. type LogWarn = (message: string, error: unknown) => void;
  34. const MANUAL_CLAIM_HEARTBEAT_MS = 60_000;
  35. export function startManualClaimHeartbeat(
  36. params: Pick<CronRunLifecycleIdentity, "automationId" | "runId" | "workspaceKey"> & {
  37. repo: Pick<CronRunLifecycleRepo, "touchManualClaim">;
  38. logWarn: LogWarn;
  39. intervalMs?: number;
  40. },
  41. ): { dispose(): void } {
  42. const timer = setInterval(() => {
  43. void params.repo
  44. .touchManualClaim(params.automationId, params.workspaceKey)
  45. .catch((error) =>
  46. params.logWarn(
  47. `续租 manual automation claim 失败 automation=${params.automationId} runId=${params.runId}`,
  48. error,
  49. ),
  50. );
  51. }, params.intervalMs ?? MANUAL_CLAIM_HEARTBEAT_MS);
  52. return { dispose: () => clearInterval(timer) };
  53. }
  54. export async function recordCronRunOutcomeBestEffort(
  55. params: CronRunLifecycleIdentity & {
  56. repo: CronRunLifecycleRepo;
  57. outcome: ZCodeAutomationRunOutcome;
  58. error?: string;
  59. logWarn: LogWarn;
  60. },
  61. ): Promise<void> {
  62. try {
  63. await params.repo.ensureRunClaimed(params);
  64. await params.repo.markRunOutcome(params.runId, params.outcome, params.error);
  65. } catch (error) {
  66. params.logWarn(
  67. `回写定时任务运行结果失败 automation=${params.automationId} runId=${params.runId}`,
  68. error,
  69. );
  70. }
  71. }
  72. export async function releaseManualClaimBestEffort(
  73. params: Pick<CronRunLifecycleIdentity, "automationId" | "runId" | "workspaceKey"> & {
  74. repo: Pick<CronRunLifecycleRepo, "releaseManualClaim">;
  75. logWarn: LogWarn;
  76. },
  77. ): Promise<void> {
  78. try {
  79. await params.repo.releaseManualClaim(params.automationId, params.workspaceKey);
  80. } catch (error) {
  81. params.logWarn(
  82. `释放 manual automation claim 失败 automation=${params.automationId} runId=${params.runId}`,
  83. error,
  84. );
  85. }
  86. }
  87. /** 派发失败清理永不覆盖调用方持有的原始 dispatch error。 */
  88. export async function settleManualDispatchFailureBestEffort(
  89. params: CronRunLifecycleIdentity & {
  90. repo: CronRunLifecycleRepo;
  91. dispatchError: unknown;
  92. logWarn: LogWarn;
  93. },
  94. ): Promise<void> {
  95. const errorMessage =
  96. params.dispatchError instanceof Error
  97. ? params.dispatchError.message
  98. : String(params.dispatchError);
  99. try {
  100. await params.repo.markRunDispatch({
  101. runId: params.runId,
  102. dispatchStatus: "failed_to_dispatch",
  103. error: errorMessage,
  104. });
  105. } catch (error) {
  106. params.logWarn(
  107. `回写 manual automation 派发失败状态失败 automation=${params.automationId} runId=${params.runId}`,
  108. error,
  109. );
  110. }
  111. await releaseManualClaimBestEffort(params);
  112. }
  113. /** manual claim 覆盖 queue 等待和 turn 执行,只能在真实终态后释放。 */
  114. export async function settleCronRunTerminalOutcome(
  115. params: CronRunLifecycleIdentity & {
  116. repo: CronRunLifecycleRepo;
  117. outcome: Exclude<ZCodeAutomationRunOutcome, "running">;
  118. error?: string;
  119. logWarn: LogWarn;
  120. },
  121. ): Promise<void> {
  122. await recordCronRunOutcomeBestEffort(params);
  123. if (params.trigger !== "manual") return;
  124. await releaseManualClaimBestEffort(params);
  125. }