* Acquire the cross-process migration lock, failing loudly after the deadline * instead of blocking forever behind a wedged runner.
()
| 153 | * instead of blocking forever behind a wedged runner. |
| 154 | */ |
| 155 | async 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 |