MCPcopy
hub / github.com/simstudioai/sim / acquireMigrationLock

Function acquireMigrationLock

packages/db/scripts/migrate.ts:155–172  ·  view source on GitHub ↗

* Acquire the cross-process migration lock, failing loudly after the deadline * instead of blocking forever behind a wedged runner.

()

Source from the content-addressed store, hash-verified

153 * instead of blocking forever behind a wedged runner.
154 */
155async function acquireMigrationLock(): Promise<void> {
156 const deadline = Date.now() + LOCK_ACQUIRE_DEADLINE_MS
157 for (;;) {
158 const [{ locked, pid }] =
159 await client`SELECT pg_try_advisory_lock(${MIGRATION_LOCK_KEY}) AS locked, pg_backend_pid() AS pid`
160 if (locked) {
161 lockSessionPid = pid
162 return
163 }
164 if (Date.now() >= deadline) {
165 throw new Error(
166 `Timed out after ${LOCK_ACQUIRE_DEADLINE_MS}ms waiting for the migration advisory lock; ` +
167 'another runner is likely stuck mid-migration. Investigate before retrying.'
168 )
169 }
170 await sleep(LOCK_RETRY_INTERVAL_MS)
171 }
172}
173
174/**
175 * Run pending migrations, retrying on lock timeout (55P03, found anywhere in

Callers 1

migrate.tsFile · 0.85

Calls 1

sleepFunction · 0.90

Tested by

no test coverage detected