| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as alloydb from "@distilled.cloud/gcp/alloydb_v1"; |
| 3 | import { |
| 4 | waitForOperation as waitForLongRunning, |
| 5 | type LongRunningOperation, |
| 6 | } from "../Operation.ts"; |
| 7 | |
| 8 | /** AlloyDB cluster and instance creates take 15–30 minutes. */ |
| 9 | const OPERATION_BUDGET = "45 minutes"; |
| 10 | |
| 11 | /** |
| 12 | * Wait for a long-running operation. ALREADY_EXISTS (code 6) means a |
| 13 | * concurrent create won the race; reconcile observes the resource next. |
| 14 | */ |
| 15 | export const waitForOperation = (operation: LongRunningOperation) => |
| 16 | waitForLongRunning( |
| 17 | operation, |
| 18 | (name) => alloydb.getProjectsLocationsOperations({ name }), |
| 19 | { |
| 20 | budget: OPERATION_BUDGET, |
| 21 | }, |
| 22 | ).pipe( |
| 23 | Effect.catchIf( |
| 24 | (error) => error._tag === "GCP.OperationFailed" && error.code === 6, |
| 25 | () => Effect.succeed(operation), |
| 26 | ), |
| 27 | ); |
| 28 | |
| 29 | /** |
| 30 | * Wait for a delete operation. A vanished operation or NOT_FOUND (code 5) |
| 31 | * means the resource is already gone. |
| 32 | */ |
| 33 | export const waitForDeleteOperation = (operation: LongRunningOperation) => |
| 34 | waitForOperation(operation).pipe( |
| 35 | Effect.catchIf( |
| 36 | (error) => |
| 37 | error._tag === "NotFound" || |
| 38 | (error._tag === "GCP.OperationFailed" && error.code === 5), |
| 39 | () => Effect.succeed(operation), |
| 40 | ), |
| 41 | ); |