d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
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
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { toError } from '@sim/utils/errors'
|
|
import { sleep } from '@sim/utils/helpers'
|
|
import { generateShortId } from '@sim/utils/id'
|
|
import { acquireLock, releaseLock } from '@/lib/core/config/redis'
|
|
|
|
const logger = createLogger('LeaderLock')
|
|
|
|
const DEFAULT_TTL_SEC = 10
|
|
const DEFAULT_POLL_INTERVAL_MS = 100
|
|
const DEFAULT_MAX_WAIT_MS = 3_000
|
|
|
|
export interface LeaderLockOptions<T> {
|
|
key: string
|
|
ttlSec?: number
|
|
pollIntervalMs?: number
|
|
maxWaitMs?: number
|
|
onLeader: () => Promise<T | null>
|
|
onFollower: () => Promise<T | null>
|
|
}
|
|
|
|
export async function withLeaderLock<T>(opts: LeaderLockOptions<T>): Promise<T | null> {
|
|
const {
|
|
key,
|
|
ttlSec = DEFAULT_TTL_SEC,
|
|
pollIntervalMs = DEFAULT_POLL_INTERVAL_MS,
|
|
maxWaitMs = DEFAULT_MAX_WAIT_MS,
|
|
onLeader,
|
|
onFollower,
|
|
} = opts
|
|
|
|
const ownerToken = generateShortId()
|
|
|
|
let acquired = false
|
|
try {
|
|
acquired = await acquireLock(key, ownerToken, ttlSec)
|
|
} catch (error) {
|
|
logger.warn('Lock acquisition failed; running leader path uncoordinated', {
|
|
key,
|
|
error: toError(error).message,
|
|
})
|
|
return onLeader()
|
|
}
|
|
|
|
if (acquired) {
|
|
try {
|
|
return await onLeader()
|
|
} finally {
|
|
try {
|
|
await releaseLock(key, ownerToken)
|
|
} catch (error) {
|
|
logger.warn('Lock release failed (will expire via TTL)', {
|
|
key,
|
|
error: toError(error).message,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
const deadline = Date.now() + maxWaitMs
|
|
while (Date.now() < deadline) {
|
|
await sleep(pollIntervalMs)
|
|
const value = await onFollower()
|
|
if (value !== null) return value
|
|
}
|
|
|
|
// The leader may have persisted between our final poll and now; one last check.
|
|
const lastChance = await onFollower()
|
|
if (lastChance !== null) return lastChance
|
|
|
|
logger.warn('Follower timed out waiting for leader', { key, maxWaitMs })
|
|
return null
|
|
}
|