( workflowId: string, blockId: string, scopeKey: string, cooldownMs: number )
| 39 | * deployment version). |
| 40 | */ |
| 41 | export async function claimCooldown( |
| 42 | workflowId: string, |
| 43 | blockId: string, |
| 44 | scopeKey: string, |
| 45 | cooldownMs: number |
| 46 | ): Promise<boolean> { |
| 47 | const now = new Date() |
| 48 | const threshold = new Date(now.getTime() - cooldownMs) |
| 49 | |
| 50 | const rows = await db |
| 51 | .insert(simTriggerState) |
| 52 | .values({ |
| 53 | workflowId, |
| 54 | blockId, |
| 55 | scopeKey, |
| 56 | lastFiredAt: now, |
| 57 | updatedAt: now, |
| 58 | }) |
| 59 | .onConflictDoUpdate({ |
| 60 | target: [simTriggerState.workflowId, simTriggerState.blockId, simTriggerState.scopeKey], |
| 61 | set: { lastFiredAt: now, updatedAt: now }, |
| 62 | setWhere: sql`${simTriggerState.lastFiredAt} IS NULL OR ${simTriggerState.lastFiredAt} < ${threshold}`, |
| 63 | }) |
| 64 | .returning({ workflowId: simTriggerState.workflowId }) |
| 65 | |
| 66 | return rows.length > 0 |
| 67 | } |
| 68 | |
| 69 | export function isWithinCooldown(lastFiredAt: Date | null, cooldownMs: number): boolean { |
| 70 | if (!lastFiredAt) return false |
no outgoing calls
no test coverage detected