chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { db } from '@sim/db'
|
||||
import { simTriggerState } from '@sim/db/schema'
|
||||
import { and, eq, sql } from 'drizzle-orm'
|
||||
|
||||
/**
|
||||
* Reads the last firing time for a subscription scope. Cheap pre-check used
|
||||
* to skip rule SQL while a subscription is cooling down; the atomic claim in
|
||||
* {@link claimCooldown} remains the source of truth.
|
||||
*/
|
||||
export async function readLastFiredAt(
|
||||
workflowId: string,
|
||||
blockId: string,
|
||||
scopeKey: string
|
||||
): Promise<Date | null> {
|
||||
const rows = await db
|
||||
.select({ lastFiredAt: simTriggerState.lastFiredAt })
|
||||
.from(simTriggerState)
|
||||
.where(
|
||||
and(
|
||||
eq(simTriggerState.workflowId, workflowId),
|
||||
eq(simTriggerState.blockId, blockId),
|
||||
eq(simTriggerState.scopeKey, scopeKey)
|
||||
)
|
||||
)
|
||||
.limit(1)
|
||||
|
||||
return rows[0]?.lastFiredAt ?? null
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically claims a cooldown slot for a subscription scope.
|
||||
*
|
||||
* Uses an upsert whose update only applies when the previous firing is
|
||||
* outside the cooldown window, so concurrent qualifying events can never
|
||||
* double-fire: exactly one caller gets a row back.
|
||||
*
|
||||
* State is keyed by (workflowId, blockId, scopeKey) rather than the webhook
|
||||
* row so cooldowns survive redeploys (webhook rows are recreated per
|
||||
* deployment version).
|
||||
*/
|
||||
export async function claimCooldown(
|
||||
workflowId: string,
|
||||
blockId: string,
|
||||
scopeKey: string,
|
||||
cooldownMs: number
|
||||
): Promise<boolean> {
|
||||
const now = new Date()
|
||||
const threshold = new Date(now.getTime() - cooldownMs)
|
||||
|
||||
const rows = await db
|
||||
.insert(simTriggerState)
|
||||
.values({
|
||||
workflowId,
|
||||
blockId,
|
||||
scopeKey,
|
||||
lastFiredAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [simTriggerState.workflowId, simTriggerState.blockId, simTriggerState.scopeKey],
|
||||
set: { lastFiredAt: now, updatedAt: now },
|
||||
setWhere: sql`${simTriggerState.lastFiredAt} IS NULL OR ${simTriggerState.lastFiredAt} < ${threshold}`,
|
||||
})
|
||||
.returning({ workflowId: simTriggerState.workflowId })
|
||||
|
||||
return rows.length > 0
|
||||
}
|
||||
|
||||
export function isWithinCooldown(lastFiredAt: Date | null, cooldownMs: number): boolean {
|
||||
if (!lastFiredAt) return false
|
||||
return Date.now() - lastFiredAt.getTime() < cooldownMs
|
||||
}
|
||||
Reference in New Issue
Block a user